Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

122
LINES

< > BotCompany Repo | #1000634 // Spike - find table names in MySQL dump

JavaX source code [tags: use-pretranspiled] - run with: x30.jar

Libraryless. Click here for Pure Java version (3812L/26K/70K).

!636
!sf
!multi-line strings
!1000635 // stringfunc {
!723 // S, O etc.
!1000636 // compare strings with ==
!1000482 // quick cast
!JavaTok
!class _javax 19

interface StringFunc {
  S get(S s);
}

main {
  static O in;
  
  psvm {
    if (in == null) // may have been set by hotwire client
      in = "dump for table `mytable` and table `anothertable`";
    
    if (args.length != 0) in = loadSnippet(args[0]);
    
    S s = [[
      example output: "mytable, anothertable"
      javatok input // rather, mysqltok... ^^
      find "table" "`" *
      fix duplicates
      fix output
    ]];
    L<S> ll = toLinesFullTrim(s);
    //System.out.println(ll);
    
    ll = map(ll, stringfunc { s.replaceAll("//.*$", "").trim() });
    //System.out.println(ll);
    O expectedOut = null;

    for (S c : ll) {
      if (c.startsWith("example output:"))
        expectedOut = unquote(c.substring("example output:".length()).trim());
      else if (c == "javatok input")
        in = JavaTok.split((String) in);
      else if (c.startsWith("find")) {
        L<S> tok = JavaTok.split(c);
        tok.remove(0); tok.remove(0);
        tok = map(tok, stringfunc { unquote(s) });
        L<S> inp = cast in;
        //System.out.println("inp: " + inp);
        //System.out.println("pat: " + tok);
        in = matchTokensList(inp, tok);
        //System.out.println("Result: " + in);
      } else if (c == "fix output") {
        if (in instanceof List && expectedOut instanceof String)
          in = join(", ", in);
      } else if (c == "fix duplicates") {
        new L<S> list;
        L<S> l = cast in;
        for (int i = 0; i < l.size(); i++)
          if (i == 0 || !l.get(i).equals(l.get(i-1)))
            list.add(l.get(i));
        in = list;
      }
    }
    
    if (expectedOut != null)
      System.out.println("Expected: " + expectedOut);
    System.out.println("Got: " + in);
    if (expectedOut != null && expectedOut.equals(in))
      print "OK!"
  }
  
  static S join(S glue, O o) {
    return join(glue, (L<S>) o);
  }
  
  !include #2000507 // original join function
  
  // returns a list of the matched tokens
  static L<S> matchTokensList(L<S> inp, L<S> pat) {
    new L<S> list;
    for (int i = 1; i+pat.size()-3 < inp.size(); i += 2) {
      S result = matchTokens_step(inp, pat, i);
      if (result != null)
        list.add(result);
    }
    return list;
  }
  
  // returns
  // -token matched for *
  // -"" on match without *
  // -null on non-match
  static String matchTokens(L<S> inp, L<S> pat) {
    for (int i = 1; i+pat.size()-3 < inp.size(); i += 2) {
      S result = matchTokens_step(inp, pat, i);
      if (result != null)
        return result;
    }
    return null;
  }
  
  static String matchTokens_step(L<S> inp, L<S> pat, int i) {
    S result = "";
    for (int j = 1; j < pat.size(); j += 2)
      if (pat.get(j).equals("*"))
        result = inp.get(i+j-1);
      else {
        String found = inp.get(i + j - 1);
        String expected = pat.get(j);
        if (!found.equals(expected))
          return null;
      }
    return result;
  }
  
  static L<S> map(L<S> l, StringFunc f) {
    new L<S> l2;
    for (S s : l)
      l2.add(f.get(s));
    return l2;
  }
}

Author comment

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: 666 / 957
Referenced in: [show references]