0xDEADBEEF

RSS odkazy english edition

Java, Scala a regulární výrazy #8 - kód

D:

import std.regex;
import std.stdio;
import std.array;
import std.datetime.stopwatch;

enum runCompileTime = true;

void main() {
  enum r  = `^(\S+) (\S+) (\S+) \[([^\]]+)\] "((?:\\"|[^"])+)" (\d+) (\d+|-) "((?:\\"|[^"])*)" "((?:\\"|[^"])*)".*$`;
  static if (runCompileTime) {
    auto lineRegex = regex.ctRegex!r;
  } else {
    auto lineRegex = regex.regex(r);
  }

  auto lines = array(File("access.log", "r").byLineCopy);
  auto timer = StopWatch(AutoStart.yes);
  foreach (l; lines) {
    matchFirst(l, lineRegex);
  }
  writeln(timer.peek().total!"msecs", " ms");
}

Java Slurp:

def slurp(line: String) = {
  val s = asciiblog.Slurp(line)

  val ip = s.until(' ').asString()
  s.char(' ')
  s.until(' ')
  s.char(' ')
  s.until(' ')
  s.char(' ').ignore()
  val date = s.delimited('[', ']').asString(1, -1)
  s.char(' ').ignore()
  val req = s.delimited('"', '"').asString(1, -1)
  s.char(' ').ignore()
  val resp = s.until(' ').asString()
  s.char(' ').ignore()
  val bytes = s.until(' ').asString()
  s.char(' ').ignore()
  val ref = s.delimited('"', '"').asString(1, -1)
  s.char(' ').ignore()
  val ua = s.delimited('"', '"').asString(1, -1)

  require(s.matches, line)

  Array(ip, date, req, resp, bytes, ref, ua)
}
píše k47 (@kaja47, k47)