Advertisement
Guest User

Untitled

a guest
Sep 17th, 2019
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.33 KB | None | 0 0
  1. final RegExp _commonLeadingWhitespaceRE =
  2. RegExp(r"(?=[^]*^([ \t]+)$(?![^]))(?![^]*^(?!\1))", multiLine: true);
  3.  
  4. String trimLeadingWhitespaceRE(String text) {
  5. var commonWhitespace = _commonLeadingWhitespaceRE.matchAsPrefix(text);
  6. if (commonWhitespace != null) {
  7. return text.replaceAll(
  8. RegExp("^${commonWhitespace[1]}", multiLine: true), "");
  9. }
  10. return text;
  11. }
  12.  
  13. final RegExp _commonLeadingWhitespaceRE2 =
  14. RegExp(r"([ \t]+)(?![^]*^(?!\1))", multiLine: true);
  15. String trimLeadingWhitespaceRE2(String text) {
  16. var commonWhitespace = _commonLeadingWhitespaceRE2.matchAsPrefix(text);
  17. if (commonWhitespace != null) {
  18. return text.replaceAll(
  19. RegExp("^${commonWhitespace[1]}", multiLine: true), "");
  20. }
  21. return text;
  22. }
  23.  
  24. final RegExp _commonLeadingWhitespaceRE3 =
  25. RegExp(r"(?=[^]*^([ \t]+)$(?![^]))(?![^]*^(?!\1))^\1", multiLine: true);
  26.  
  27. String trimLeadingWhitespaceRE3(String text) {
  28. return text.replaceAll(_commonLeadingWhitespaceRE3, "");
  29. }
  30.  
  31. String trimLeadingWhitespaceDirect(String text) {
  32. // Last line must contain only space and tab.
  33. int lastLineStart = text.length;
  34. int char = 0;
  35. while (lastLineStart > 0 &&
  36. (char = text.codeUnitAt(lastLineStart - 1)) == 0x20 ||
  37. char == 0x09) {
  38. lastLineStart--;
  39. }
  40. if (lastLineStart == 0) return "";
  41. StringBuffer buffer = StringBuffer();
  42. if (char != 0x0d && char != 0x0a) return text;
  43. int i = 0;
  44. do {
  45. for (var j = lastLineStart; j < text.length; j++) {
  46. if (text.codeUnitAt(i) != text.codeUnitAt(j)) return text;
  47. i++;
  48. }
  49. int lineStart = i;
  50. int char = 0;
  51. while ((char = text.codeUnitAt(i++)) != 0x0a && char != 0x0d) {}
  52. if (char == 0x0d && i < lastLineStart && text.codeUnitAt(i) == 0x0a) {
  53. i++;
  54. }
  55. buffer.write(text.substring(lineStart, i));
  56. } while (i < lastLineStart);
  57. return buffer.toString();
  58. }
  59.  
  60. const List<String> samples = <String>[
  61. """
  62. All indented
  63. The same way
  64. As the last line.
  65. """,
  66. """
  67. All indented
  68. More than
  69. The last line.
  70. """,
  71. """
  72. Long indents and long lines and long text on each line.
  73. Long indents and long lines and long text on each line.
  74. Long indents and long lines and long text on each line.
  75. Long indents and long lines and long text on each line.
  76. Long indents and long lines and long text on each line.
  77. Long indents and long lines and long text on each line.
  78. Long indents and long lines and long text on each line.
  79. Long indents and long lines and long text on each line.
  80. Long indents and long lines and long text on each line.
  81. And many lines.
  82. """
  83. ];
  84.  
  85. double bench(String Function(String) trim, int limitMS) {
  86. int c = 0;
  87. int e = 0;
  88. var sw = Stopwatch()..start();
  89. do {
  90. for (int i = 0; i < 100; i++) {
  91. for (var sample in samples) {
  92. var result = trim(sample);
  93. if (result.isEmpty) throw "This won't happen";
  94. c++;
  95. }
  96. }
  97. e = sw.elapsedMilliseconds;
  98. } while (e < limitMS);
  99. return c / e; // Operations per milliseconds.
  100. }
  101.  
  102. main(List args) {
  103. int count = 1;
  104. if (args.isNotEmpty) count = int.tryParse(args[0]) ?? 1;
  105. // Sanity check.
  106. bool sane = true;
  107. for (var sample in samples) {
  108. var direct = trimLeadingWhitespaceDirect(sample);
  109. var re1 = trimLeadingWhitespaceRE(sample);
  110. var re2 = trimLeadingWhitespaceRE2(sample);
  111. var re3 = trimLeadingWhitespaceRE3(sample);
  112. if (direct != re1 || direct != re2 || direct != re3) {
  113. sane = false;
  114. print("Sanity check failed, algorithm is not correct");
  115. print("Sample:\n$sample");
  116. print("Direct:\n$direct");
  117. print("RegExp1:\n$re1");
  118. print("RegExp2:\n$re2");
  119. print("RegExp3:\n$re3");
  120. }
  121. }
  122. if (!sane) return;
  123. // Warmup.
  124. bench(trimLeadingWhitespaceDirect, 250);
  125. bench(trimLeadingWhitespaceRE, 250);
  126. bench(trimLeadingWhitespaceRE2, 250);
  127. bench(trimLeadingWhitespaceRE3, 250);
  128. for (var i = 0; i < count; i++) {
  129. print(
  130. "RegExp1: ${bench(trimLeadingWhitespaceRE, 2000).toStringAsFixed(3)} /ms");
  131. print(
  132. "RegExp2: ${bench(trimLeadingWhitespaceRE2, 2000).toStringAsFixed(3)} /ms");
  133. print(
  134. "RegExp3: ${bench(trimLeadingWhitespaceRE3, 2000).toStringAsFixed(3)} /ms");
  135. print(
  136. "Direct : ${bench(trimLeadingWhitespaceDirect, 2000).toStringAsFixed(3)} /ms");
  137. }
  138. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement