-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathGoogleCurrencyCalculator.ragel
61 lines (48 loc) · 1.62 KB
/
GoogleCurrencyCalculator.ragel
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
// Generated using:
// ragel -J -o src/com/appspot/btcticker/fsm/GoogleCurrencyCalculator.java GoogleCurrencyCalculator.ragel
//
package com.appspot.btcticker.fsm;
public class GoogleCurrencyCalculator {
public static final String EURO_URL = "http://www.google.com/ig/calculator?hl=en&q=1USD=?EUR";
public static final String POUND_URL = "http://www.google.com/ig/calculator?hl=en&q=1USD=?GBP";
public static final String DKK_URL = "http://www.google.com/ig/calculator?hl=en&q=1USD=?DKK";
%%{
machine currency_converter;
write data;
action clear { i = 0; f = 0.0; scale = 1.0; }
action ipart { i = i * 10 + (int)data[p] - zero; }
action fpart { scale = scale * 0.1; f = f + scale * ((int)data[p] - zero); }
number = digit+ >clear $ipart ('.' digit+ $fpart)?
;
lhs = "lhs:" space* '"' (any - '"')* '"' ','
;
rhs = "rhs:" space* '"' number space (any - '"')* '"' ','
;
error = "error:" space* '"' '"' ','
;
icc = "icc:" space* "true"
;
main := "{" lhs rhs error icc "}"
;
}%%
public double extract(String response) {
char[] data = response.toCharArray();
// Used by Ragel
int cs;
int p = 0;
int pe = data.length;
// Used to store extracted data
final int zero = (int) '0';
int i = 0;
double f = 0.0;
double scale = 1.0;
%% write init;
%% write exec;
if (cs < currency_converter_first_final) {
return -1.0;
}
else {
return i + f;
}
}
}