Libraryless. Click here for Pure Java version (3812L/26K/70K).
1 | !636 |
2 | !sf |
3 | !multi-line strings |
4 | !1000635 // stringfunc { |
5 | !723 // S, O etc. |
6 | !1000636 // compare strings with == |
7 | !1000482 // quick cast |
8 | !JavaTok |
9 | !class _javax 19 |
10 | |
11 | interface StringFunc { |
12 | S get(S s); |
13 | } |
14 | |
15 | main { |
16 | static O in; |
17 | |
18 | psvm { |
19 | if (in == null) // may have been set by hotwire client |
20 | in = "dump for table `mytable` and table `anothertable`"; |
21 | |
22 | if (args.length != 0) in = loadSnippet(args[0]); |
23 | |
24 | S s = [[ |
25 | example output: "mytable, anothertable" |
26 | javatok input // rather, mysqltok... ^^ |
27 | find "table" "`" * |
28 | fix duplicates |
29 | fix output |
30 | ]]; |
31 | L<S> ll = toLinesFullTrim(s); |
32 | //System.out.println(ll); |
33 | |
34 | ll = map(ll, stringfunc { s.replaceAll("//.*$", "").trim() }); |
35 | //System.out.println(ll); |
36 | O expectedOut = null; |
37 | |
38 | for (S c : ll) { |
39 | if (c.startsWith("example output:")) |
40 | expectedOut = unquote(c.substring("example output:".length()).trim()); |
41 | else if (c == "javatok input") |
42 | in = JavaTok.split((String) in); |
43 | else if (c.startsWith("find")) { |
44 | L<S> tok = JavaTok.split(c); |
45 | tok.remove(0); tok.remove(0); |
46 | tok = map(tok, stringfunc { unquote(s) }); |
47 | L<S> inp = cast in; |
48 | //System.out.println("inp: " + inp); |
49 | //System.out.println("pat: " + tok); |
50 | in = matchTokensList(inp, tok); |
51 | //System.out.println("Result: " + in); |
52 | } else if (c == "fix output") { |
53 | if (in instanceof List && expectedOut instanceof String) |
54 | in = join(", ", in); |
55 | } else if (c == "fix duplicates") { |
56 | new L<S> list; |
57 | L<S> l = cast in; |
58 | for (int i = 0; i < l.size(); i++) |
59 | if (i == 0 || !l.get(i).equals(l.get(i-1))) |
60 | list.add(l.get(i)); |
61 | in = list; |
62 | } |
63 | } |
64 | |
65 | if (expectedOut != null) |
66 | System.out.println("Expected: " + expectedOut); |
67 | System.out.println("Got: " + in); |
68 | if (expectedOut != null && expectedOut.equals(in)) |
69 | print "OK!" |
70 | } |
71 | |
72 | static S join(S glue, O o) { |
73 | return join(glue, (L<S>) o); |
74 | } |
75 | |
76 | !include #2000507 // original join function |
77 | |
78 | // returns a list of the matched tokens |
79 | static L<S> matchTokensList(L<S> inp, L<S> pat) { |
80 | new L<S> list; |
81 | for (int i = 1; i+pat.size()-3 < inp.size(); i += 2) { |
82 | S result = matchTokens_step(inp, pat, i); |
83 | if (result != null) |
84 | list.add(result); |
85 | } |
86 | return list; |
87 | } |
88 | |
89 | // returns |
90 | // -token matched for * |
91 | // -"" on match without * |
92 | // -null on non-match |
93 | static String matchTokens(L<S> inp, L<S> pat) { |
94 | for (int i = 1; i+pat.size()-3 < inp.size(); i += 2) { |
95 | S result = matchTokens_step(inp, pat, i); |
96 | if (result != null) |
97 | return result; |
98 | } |
99 | return null; |
100 | } |
101 | |
102 | static String matchTokens_step(L<S> inp, L<S> pat, int i) { |
103 | S result = ""; |
104 | for (int j = 1; j < pat.size(); j += 2) |
105 | if (pat.get(j).equals("*")) |
106 | result = inp.get(i+j-1); |
107 | else { |
108 | String found = inp.get(i + j - 1); |
109 | String expected = pat.get(j); |
110 | if (!found.equals(expected)) |
111 | return null; |
112 | } |
113 | return result; |
114 | } |
115 | |
116 | static L<S> map(L<S> l, StringFunc f) { |
117 | new L<S> l2; |
118 | for (S s : l) |
119 | l2.add(f.get(s)); |
120 | return l2; |
121 | } |
122 | } |
See example: #1000637
download show line numbers debug dex old transpilations
Travelled to 18 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, ddnzoavkxhuk, gwrvuhgaqvyk, hxnwyiuffukg, ishqpsrjomds, jtubtzbbkimh, lpdgvwnxivlt, mqqgnosmbjvj, onxytkatvevr, pyentgdyhuwx, pzhvpgtvlbxg, teubizvjbppd, tslmcundralx, tvejysmllsmz, vouqrxazstgt
No comments. add comment
Snippet ID: | #1000634 |
Snippet name: | Spike - find table names in MySQL dump |
Eternal ID of this version: | #1000634/1 |
Text MD5: | e86c3615a03765d151b5afa437c287b0 |
Transpilation MD5: | ec659ef44437c9ebeb5497486f3183ef |
Author: | stefan |
Category: | |
Type: | JavaX source code |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2015-08-21 00:30:02 |
Source code size: | 3342 bytes / 122 lines |
Pitched / IR pitched: | No / Yes |
Views / Downloads: | 730 / 1039 |
Referenced in: | [show references] |