diff options
| author | bors <bors@rust-lang.org> | 2014-10-10 00:07:08 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2014-10-10 00:07:08 +0000 |
| commit | f9fc49c06e5c71a8d63d6120e1a92b6445fb501d (patch) | |
| tree | 8a7c52db87b4ff8d5a7fa97dc44fb9ac10b92626 /src/libregex | |
| parent | 8b12fb326b50c40c7b5acadb4403c7021fedb272 (diff) | |
| parent | 0b517117b3ac9c4981bbe00a529e48e4019554d1 (diff) | |
| download | rust-f9fc49c06e5c71a8d63d6120e1a92b6445fb501d.tar.gz rust-f9fc49c06e5c71a8d63d6120e1a92b6445fb501d.zip | |
auto merge of #17853 : alexcrichton/rust/issue-17718, r=pcwalton
This change is an implementation of [RFC 69][rfc] which adds a third kind of
global to the language, `const`. This global is most similar to what the old
`static` was, and if you're unsure about what to use then you should use a
`const`.
The semantics of these three kinds of globals are:
* A `const` does not represent a memory location, but only a value. Constants
are translated as rvalues, which means that their values are directly inlined
at usage location (similar to a #define in C/C++). Constant values are, well,
constant, and can not be modified. Any "modification" is actually a
modification to a local value on the stack rather than the actual constant
itself.
Almost all values are allowed inside constants, whether they have interior
mutability or not. There are a few minor restrictions listed in the RFC, but
they should in general not come up too often.
* A `static` now always represents a memory location (unconditionally). Any
references to the same `static` are actually a reference to the same memory
location. Only values whose types ascribe to `Sync` are allowed in a `static`.
This restriction is in place because many threads may access a `static`
concurrently. Lifting this restriction (and allowing unsafe access) is a
future extension not implemented at this time.
* A `static mut` continues to always represent a memory location. All references
to a `static mut` continue to be `unsafe`.
This is a large breaking change, and many programs will need to be updated
accordingly. A summary of the breaking changes is:
* Statics may no longer be used in patterns. Statics now always represent a
memory location, which can sometimes be modified. To fix code, repurpose the
matched-on-`static` to a `const`.
static FOO: uint = 4;
match n {
FOO => { /* ... */ }
_ => { /* ... */ }
}
change this code to:
const FOO: uint = 4;
match n {
FOO => { /* ... */ }
_ => { /* ... */ }
}
* Statics may no longer refer to other statics by value. Due to statics being
able to change at runtime, allowing them to reference one another could
possibly lead to confusing semantics. If you are in this situation, use a
constant initializer instead. Note, however, that statics may reference other
statics by address, however.
* Statics may no longer be used in constant expressions, such as array lengths.
This is due to the same restrictions as listed above. Use a `const` instead.
[breaking-change]
Closes #17718
[rfc]: https://github.com/rust-lang/rfcs/pull/246
Diffstat (limited to 'src/libregex')
| -rw-r--r-- | src/libregex/parse.rs | 59 | ||||
| -rw-r--r-- | src/libregex/re.rs | 2 |
2 files changed, 38 insertions, 23 deletions
diff --git a/src/libregex/parse.rs b/src/libregex/parse.rs index bf576432631..1d1d1a0e9c5 100644 --- a/src/libregex/parse.rs +++ b/src/libregex/parse.rs @@ -162,12 +162,12 @@ impl BuildAst { /// expression. pub type Flags = u8; -pub static FLAG_EMPTY: u8 = 0; -pub static FLAG_NOCASE: u8 = 1 << 0; // i -pub static FLAG_MULTI: u8 = 1 << 1; // m -pub static FLAG_DOTNL: u8 = 1 << 2; // s -pub static FLAG_SWAP_GREED: u8 = 1 << 3; // U -pub static FLAG_NEGATED: u8 = 1 << 4; // char class or not word boundary +pub const FLAG_EMPTY: u8 = 0; +pub const FLAG_NOCASE: u8 = 1 << 0; // i +pub const FLAG_MULTI: u8 = 1 << 1; // m +pub const FLAG_DOTNL: u8 = 1 << 2; // s +pub const FLAG_SWAP_GREED: u8 = 1 << 3; // U +pub const FLAG_NEGATED: u8 = 1 << 4; // char class or not word boundary struct Parser<'a> { // The input, parsed only as a sequence of UTF8 code points. @@ -1025,7 +1025,7 @@ fn find_class(classes: NamedClasses, name: &str) -> Option<Vec<(char, char)>> { } type Class = &'static [(char, char)]; -type NamedClasses = &'static [(&'static str, Class)]; +type NamedClasses = &'static [(&'static str, &'static Class)]; static ASCII_CLASSES: NamedClasses = &[ // Classes must be in alphabetical order so that bsearch works. @@ -1044,19 +1044,34 @@ static ASCII_CLASSES: NamedClasses = &[ // [:word:] word characters (== [0-9A-Za-z_]) // [:xdigit:] hex digit (== [0-9A-Fa-f]) // Taken from: http://golang.org/pkg/regex/syntax/ - ("alnum", &[('0', '9'), ('A', 'Z'), ('a', 'z')]), - ("alpha", &[('A', 'Z'), ('a', 'z')]), - ("ascii", &[('\x00', '\x7F')]), - ("blank", &[(' ', ' '), ('\t', '\t')]), - ("cntrl", &[('\x00', '\x1F'), ('\x7F', '\x7F')]), - ("digit", &[('0', '9')]), - ("graph", &[('!', '~')]), - ("lower", &[('a', 'z')]), - ("print", &[(' ', '~')]), - ("punct", &[('!', '/'), (':', '@'), ('[', '`'), ('{', '~')]), - ("space", &[('\t', '\t'), ('\n', '\n'), ('\x0B', '\x0B'), ('\x0C', '\x0C'), - ('\r', '\r'), (' ', ' ')]), - ("upper", &[('A', 'Z')]), - ("word", &[('0', '9'), ('A', 'Z'), ('a', 'z'), ('_', '_')]), - ("xdigit", &[('0', '9'), ('A', 'F'), ('a', 'f')]), + ("alnum", &ALNUM), + ("alpha", &ALPHA), + ("ascii", &ASCII), + ("blank", &BLANK), + ("cntrl", &CNTRL), + ("digit", &DIGIT), + ("graph", &GRAPH), + ("lower", &LOWER), + ("print", &PRINT), + ("punct", &PUNCT), + ("space", &SPACE), + ("upper", &UPPER), + ("word", &WORD), + ("xdigit", &XDIGIT), ]; + +static ALNUM: Class = &[('0', '9'), ('A', 'Z'), ('a', 'z')]; +static ALPHA: Class = &[('A', 'Z'), ('a', 'z')]; +static ASCII: Class = &[('\x00', '\x7F')]; +static BLANK: Class = &[(' ', ' '), ('\t', '\t')]; +static CNTRL: Class = &[('\x00', '\x1F'), ('\x7F', '\x7F')]; +static DIGIT: Class = &[('0', '9')]; +static GRAPH: Class = &[('!', '~')]; +static LOWER: Class = &[('a', 'z')]; +static PRINT: Class = &[(' ', '~')]; +static PUNCT: Class = &[('!', '/'), (':', '@'), ('[', '`'), ('{', '~')]; +static SPACE: Class = &[('\t', '\t'), ('\n', '\n'), ('\x0B', '\x0B'), + ('\x0C', '\x0C'), ('\r', '\r'), (' ', ' ')]; +static UPPER: Class = &[('A', 'Z')]; +static WORD: Class = &[('0', '9'), ('A', 'Z'), ('a', 'z'), ('_', '_')]; +static XDIGIT: Class = &[('0', '9'), ('A', 'F'), ('a', 'f')]; diff --git a/src/libregex/re.rs b/src/libregex/re.rs index 0b5aeb215e6..eebe9b85e3b 100644 --- a/src/libregex/re.rs +++ b/src/libregex/re.rs @@ -128,7 +128,7 @@ pub struct ExNative { #[doc(hidden)] pub original: &'static str, #[doc(hidden)] - pub names: &'static [Option<&'static str>], + pub names: &'static &'static [Option<&'static str>], #[doc(hidden)] pub prog: fn(MatchKind, &str, uint, uint) -> Vec<Option<uint>> } |
