浏览代码

Use atomic groups to mimic the possessive quantifier change in Java code

Jon Skeet 16 年之前
父节点
当前提交
0ca3fecfaf
共有 1 个文件被更改,包括 6 次插入5 次删除
  1. 6 5
      src/ProtocolBuffers/TextTokenizer.cs

+ 6 - 5
src/ProtocolBuffers/TextTokenizer.cs

@@ -69,13 +69,14 @@ namespace Google.ProtocolBuffers {
     /// </summary>
     private int previousColumn = 0;
 
-    private static readonly Regex WhitespaceAndCommentPattern = new Regex("\\G(\\s|(#.*$))+", 
+    // Note: atomic groups used to mimic possessive quantifiers in Java in both of these regexes
+    private static readonly Regex WhitespaceAndCommentPattern = new Regex("\\G(?>(\\s|(#.*$))+)", 
         RegexOptions.Compiled | RegexOptions.Multiline);
     private static readonly Regex TokenPattern = new Regex(
-      "\\G[a-zA-Z_][0-9a-zA-Z_+-]*|" +              // an identifier
-      "\\G[0-9+-][0-9a-zA-Z_.+-]*|" +                  // a number
-      "\\G\"([^\"\\\n\\\\]|\\\\.)*(\"|\\\\?$)|" +    // a double-quoted string
-      "\\G\'([^\"\\\n\\\\]|\\\\.)*(\'|\\\\?$)",      // a single-quoted string
+      "\\G[a-zA-Z_](?>[0-9a-zA-Z_+-]*)|" +              // an identifier
+      "\\G[0-9+-](?>[0-9a-zA-Z_.+-]*)|" +                  // a number
+      "\\G\"(?>([^\"\\\n\\\\]|\\\\.)*)(\"|\\\\?$)|" +    // a double-quoted string
+      "\\G\'(?>([^\"\\\n\\\\]|\\\\.)*)(\'|\\\\?$)",      // a single-quoted string
       RegexOptions.Compiled | RegexOptions.Multiline);
 
     private static readonly Regex DoubleInfinity = new Regex("^-?inf(inity)?$", RegexOptions.Compiled | RegexOptions.IgnoreCase);