// ragel -J Atoi.rl && javac Atoi.java && java Atoi 123 23 " -12" 21
// output:
// value = 12323
// value = -1221
public class Atoi {
%% machine atoi;
%% write data;
int cs;
boolean neg = false;
long val = 0;
%% write init;
private void integerFound(final long val) {
System.
out.
println("value = " + val
); }
private void process(final char[] data) {
int p = 0;
int eof = 0;
final int pe;
if (data == null) {
pe = 0;
} else {
pe = data.length;
}
%%{
action set_neg {
neg = true;
}
action add_digit {
val = val * 10 + (fc - '0');
}
action readed {
if (neg) {
val *= -1;
}
integerFound(val);
val = 0;
neg = false;
}
number = ('-'@set_neg|'+')? (digit@add_digit)+ %readed;
main := number (' ' number)*;
/tmp/Atoi.rl[ragel] ascii:112[0x70] pos: 1 line: 1:1/56
:!cat Atoi.rl
public class Atoi {
%% machine atoi;
%% write data;
int cs;
boolean neg = false;
long val = 0;
%% write init;
private void integerFound(final long val) {
System.
out.
println("value = " + val
); }
private void process(final char[] data) {
int p = 0;
int eof = 0;
final int pe;
if (data == null) {
pe = 0;
} else {
pe = data.length;
}
%%{
action set_neg {
neg = true;
}
action add_digit {
val = val * 10 + (fc - '0');
}
action readed {
if (neg) {
val *= -1;
}
integerFound(val);
val = 0;
neg = false;
}
number = ('-'@set_neg|'+')? (digit@add_digit)+ %readed;
main := number (' ' number)*;
write exec;
}%%
}
public static void main
(final String[] args
) { Atoi atoi = new Atoi();
for (int i = 0; i < args.length; ++i) {
atoi.process(args[i].toCharArray());
}
atoi.process(null);
}
}
Ly8gcmFnZWwgLUogQXRvaS5ybCAmJiBqYXZhYyBBdG9pLmphdmEgJiYgamF2YSBBdG9pIDEyMyAyMyAiIC0xMiIgMjEKLy8gb3V0cHV0OgovLyB2YWx1ZSA9IDEyMzIzCi8vIHZhbHVlID0gLTEyMjEKCnB1YmxpYyBjbGFzcyBBdG9pIHsKICAgICUlIG1hY2hpbmUgYXRvaTsKICAgICUlIHdyaXRlIGRhdGE7CgogICAgaW50IGNzOwogICAgYm9vbGVhbiBuZWcgPSBmYWxzZTsKICAgIGxvbmcgdmFsID0gMDsKCiAgICAlJSB3cml0ZSBpbml0OwoKICAgIHByaXZhdGUgdm9pZCBpbnRlZ2VyRm91bmQoZmluYWwgbG9uZyB2YWwpIHsKICAgICAgICBTeXN0ZW0ub3V0LnByaW50bG4oInZhbHVlID0gIiArIHZhbCk7CiAgICB9CgogICAgcHJpdmF0ZSB2b2lkIHByb2Nlc3MoZmluYWwgY2hhcltdIGRhdGEpIHsKICAgICAgICBpbnQgcCA9IDA7CiAgICAgICAgaW50IGVvZiA9IDA7CiAgICAgICAgZmluYWwgaW50IHBlOwogICAgICAgIGlmIChkYXRhID09IG51bGwpIHsKICAgICAgICAgICAgcGUgPSAwOwogICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgIHBlID0gZGF0YS5sZW5ndGg7CiAgICAgICAgfQogICAgICAgICUlewogICAgICAgICAgICBhY3Rpb24gc2V0X25lZyB7CiAgICAgICAgICAgICAgICBuZWcgPSB0cnVlOwogICAgICAgICAgICB9CgogICAgICAgICAgICBhY3Rpb24gYWRkX2RpZ2l0IHsKICAgICAgICAgICAgICAgIHZhbCA9IHZhbCAqIDEwICsgKGZjIC0gJzAnKTsKICAgICAgICAgICAgfQoKICAgICAgICAgICAgYWN0aW9uIHJlYWRlZCB7CiAgICAgICAgICAgICAgICBpZiAobmVnKSB7CiAgICAgICAgICAgICAgICAgICAgdmFsICo9IC0xOwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgaW50ZWdlckZvdW5kKHZhbCk7CiAgICAgICAgICAgICAgICB2YWwgPSAwOwogICAgICAgICAgICAgICAgbmVnID0gZmFsc2U7CiAgICAgICAgICAgIH0KCiAgICAgICAgICAgIG51bWJlciA9ICgnLSdAc2V0X25lZ3wnKycpPyAoZGlnaXRAYWRkX2RpZ2l0KSsgJXJlYWRlZDsKICAgICAgICAgICAgbWFpbiA6PSBudW1iZXIgKCcgJyBudW1iZXIpKjsKL3RtcC9BdG9pLnJsW3JhZ2VsXSAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIGFzY2lpOjExMlsweDcwXSBwb3M6ICAgICAxIGxpbmU6ICAgMToxLzU2CjohY2F0IEF0b2kucmwKcHVibGljIGNsYXNzIEF0b2kgewogICAgJSUgbWFjaGluZSBhdG9pOwogICAgJSUgd3JpdGUgZGF0YTsKCiAgICBpbnQgY3M7CiAgICBib29sZWFuIG5lZyA9IGZhbHNlOwogICAgbG9uZyB2YWwgPSAwOwoKICAgICUlIHdyaXRlIGluaXQ7CgogICAgcHJpdmF0ZSB2b2lkIGludGVnZXJGb3VuZChmaW5hbCBsb25nIHZhbCkgewogICAgICAgIFN5c3RlbS5vdXQucHJpbnRsbigidmFsdWUgPSAiICsgdmFsKTsKICAgIH0KCiAgICBwcml2YXRlIHZvaWQgcHJvY2VzcyhmaW5hbCBjaGFyW10gZGF0YSkgewogICAgICAgIGludCBwID0gMDsKICAgICAgICBpbnQgZW9mID0gMDsKICAgICAgICBmaW5hbCBpbnQgcGU7CiAgICAgICAgaWYgKGRhdGEgPT0gbnVsbCkgewogICAgICAgICAgICBwZSA9IDA7CiAgICAgICAgfSBlbHNlIHsKICAgICAgICAgICAgcGUgPSBkYXRhLmxlbmd0aDsKICAgICAgICB9CiAgICAgICAgJSV7CiAgICAgICAgICAgIGFjdGlvbiBzZXRfbmVnIHsKICAgICAgICAgICAgICAgIG5lZyA9IHRydWU7CiAgICAgICAgICAgIH0KCiAgICAgICAgICAgIGFjdGlvbiBhZGRfZGlnaXQgewogICAgICAgICAgICAgICAgdmFsID0gdmFsICogMTAgKyAoZmMgLSAnMCcpOwogICAgICAgICAgICB9CgogICAgICAgICAgICBhY3Rpb24gcmVhZGVkIHsKICAgICAgICAgICAgICAgIGlmIChuZWcpIHsKICAgICAgICAgICAgICAgICAgICB2YWwgKj0gLTE7CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICBpbnRlZ2VyRm91bmQodmFsKTsKICAgICAgICAgICAgICAgIHZhbCA9IDA7CiAgICAgICAgICAgICAgICBuZWcgPSBmYWxzZTsKICAgICAgICAgICAgfQoKICAgICAgICAgICAgbnVtYmVyID0gKCctJ0BzZXRfbmVnfCcrJyk/IChkaWdpdEBhZGRfZGlnaXQpKyAlcmVhZGVkOwogICAgICAgICAgICBtYWluIDo9IG51bWJlciAoJyAnIG51bWJlcikqOwogICAgICAgICAgICB3cml0ZSBleGVjOwogICAgICAgIH0lJQogICAgfQoKICAgIHB1YmxpYyBzdGF0aWMgdm9pZCBtYWluKGZpbmFsIFN0cmluZ1tdIGFyZ3MpIHsKICAgICAgICBBdG9pIGF0b2kgPSBuZXcgQXRvaSgpOwogICAgICAgIGZvciAoaW50IGkgPSAwOyBpIDwgYXJncy5sZW5ndGg7ICsraSkgewogICAgICAgICAgICBhdG9pLnByb2Nlc3MoYXJnc1tpXS50b0NoYXJBcnJheSgpKTsKICAgICAgICB9CiAgICAgICAgYXRvaS5wcm9jZXNzKG51bGwpOwogICAgfQp9