Created
February 11, 2014 07:44
-
-
Save wycats/8930693 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//#set_loc(1, "examples/rust/atoi.rl"); | |
//#set_loc(5, "examples/rust/atoi.rs"); | |
static _atoi_actions: &'static [i8] = &[ | |
0, 1, 0, 1, 1 | |
]; | |
static _atoi_key_offsets: &'static [i8] = &[ | |
0, 0, 4, 6, 9 | |
]; | |
static _atoi_trans_keys: &'static [u8] = &[ | |
43, 45, 48, 57, 48, 57, 10, 48, 57, 0 | |
]; | |
static _atoi_single_lengths: &'static [i8] = &[ | |
0, 2, 0, 1, 0 | |
]; | |
static _atoi_range_lengths: &'static [i8] = &[ | |
0, 1, 1, 1, 0 | |
]; | |
static _atoi_index_offsets: &'static [i8] = &[ | |
0, 0, 4, 6, 9 | |
]; | |
static _atoi_trans_targs: &'static [i8] = &[ | |
2, 2, 3, 0, 3, 0, 4, 3, 0, 0, 0 | |
]; | |
static _atoi_trans_actions: &'static [i8] = &[ | |
0, 1, 3, 0, 3, 0, 0, 3, 0, 0, 0 | |
]; | |
static atoi_start: int = 1; | |
static atoi_first_final: int = 3; | |
static atoi_error: int = 0; | |
static atoi_en_main: int = 1; | |
//#set_loc(4, "examples/rust/atoi.rl"); | |
fn atoi(data: &str) -> Option<int> { | |
let mut cs: int; | |
let mut p = 0; | |
let pe = data.len(); | |
let mut neg = false; | |
let mut res = 0; | |
//#set_loc(57, "examples/rust/atoi.rs"); | |
{ | |
cs = atoi_start; | |
} | |
//#set_loc(62, "examples/rust/atoi.rs"); | |
{ | |
let mut _klen: int; | |
let mut _trans = 0; | |
let mut _acts: int; | |
let mut _nacts: int; | |
let mut _keys: int; | |
let mut _goto_targ = 0; | |
'_goto: loop { | |
match _goto_targ { | |
0 => { | |
if p == pe { | |
_goto_targ = 4; | |
continue '_goto; | |
} | |
if cs == 0 { | |
_goto_targ = 5; | |
continue '_goto; | |
} | |
_goto_targ = 1; | |
continue '_goto; | |
} | |
1 => { | |
'_match: loop { | |
_keys = _atoi_key_offsets[cs] as int; | |
_trans = _atoi_index_offsets[cs] as int; | |
_klen = _atoi_single_lengths[cs] as int; | |
if _klen > 0 { | |
let mut _lower: int = _keys; | |
let mut _mid: int; | |
let mut _upper: int = _keys + _klen - 1; | |
loop { | |
if _upper < _lower { break; } | |
_mid = _lower + ((_upper-_lower) >> 1); | |
if data[p] < _atoi_trans_keys[_mid] { | |
_upper = _mid - 1; | |
} else if data[p] > _atoi_trans_keys[_mid] { | |
_lower = _mid + 1; | |
} else { | |
_trans += (_mid - _keys); | |
break '_match; | |
} | |
} | |
_keys += _klen; | |
_trans += _klen; | |
} | |
_klen = _atoi_range_lengths[cs] as int; | |
if _klen > 0 { | |
let mut _lower = _keys; | |
let mut _mid: int; | |
let mut _upper = _keys + (_klen<<1) - 2; | |
loop { | |
if _upper < _lower { break; } | |
_mid = _lower + (((_upper-_lower) >> 1) & !1); | |
if data[p] < _atoi_trans_keys[_mid] { | |
_upper = _mid - 2; | |
} else if data[p] > _atoi_trans_keys[_mid+1] { | |
_lower = _mid + 2; | |
} else { | |
_trans += ((_mid - _keys)>>1); | |
break '_match; | |
} | |
} | |
_trans += _klen; | |
} | |
break; | |
} | |
cs = _atoi_trans_targs[_trans] as int; | |
if _atoi_trans_actions[_trans] != 0 { | |
_acts = _atoi_trans_actions[_trans] as int; | |
_nacts = _atoi_actions[_acts] as int; | |
_acts += 1; | |
while _nacts > 0 { | |
_nacts -= 1; | |
let __acts = _acts; | |
_acts += 1; | |
match _atoi_actions[__acts] { | |
0 => { | |
//#set_loc(15, "examples/rust/atoi.rl"); | |
{ neg = true; } | |
} | |
1 => { | |
//#set_loc(16, "examples/rust/atoi.rl"); | |
{ res = res * 10 + (data[p] as int - '0' as int); } | |
} | |
//#set_loc(153, "examples/rust/atoi.rs"); | |
_ => fail!(), } | |
} | |
} | |
_goto_targ = 2; | |
continue; | |
} | |
2 => { | |
if cs == 0 { | |
_goto_targ = 5; | |
continue '_goto; | |
} | |
p += 1; | |
if p != pe { | |
_goto_targ = 1; | |
continue '_goto; | |
} | |
_goto_targ = 4; | |
continue '_goto; | |
} | |
4 => { } | |
5 => { } | |
_ => fail!(), } | |
break; | |
} | |
} | |
//#set_loc(25, "examples/rust/atoi.rl"); | |
if neg { res = -1 * res; } | |
if cs < atoi_first_final { | |
None | |
} else { | |
Some(res) | |
} | |
} | |
#[test] | |
fn test_atoi() { | |
assert_eq!(atoi("7"), Some(7)); | |
assert_eq!(atoi("666"), Some(666)); | |
assert_eq!(atoi("-666"), Some(-666)); | |
assert_eq!(atoi("+666"), Some(666)); | |
assert_eq!(atoi("123456789"), Some(123456789)); | |
assert_eq!(atoi("+123456789\n"), Some(123456789)); | |
assert_eq!(atoi("+ 1234567890"), None); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment