Commits

Jan Trtík  committed e12c54a

regexp marshalling

  • Participants
  • Parent commits 1fb5c5c

Comments (0)

Files changed (2)

File fan/bson/BsonReader.fan

         case Bson.UNDEFINED:
         case Bson.NULL:
           remaining -= readNull(ins, bson)
-        // case Bson.REGEX:
-          // remaining = readRegex(ins, bson)
+        case Bson.REGEX:
+          remaining = readRegex(ins, bson)
         case Bson.CODE_W_SCOPE:
           remaining -= readCode(ins, bson)
         case Bson.NUMBER_LONG:
     bson[name] = MaxKey() // todo - figure this out
     return (name.size + 1)
   }
+
+  static private Int readRegex(InStream ins, Str:Obj? bson)
+  {
+    name := readCStr(ins)
+    pattern := readCStr(ins)
+    flags := readCStr(ins)   // TODO: implement flags support in Fantom
+    bson[name] = Regex.fromStr(pattern)
+    regexlen := pattern.size + 1 + flags.size + 1
+    return (name.size + 1 + regexlen)
+  }
   
 }

File fan/bson/BsonWriter.fan

     else if (val is Bool) writeBool(b, key, val)
     else if (val is Map) writeMap(b, key, val)
     else if (val is Code) writeCode(b, key, val)
-    // else if (val is Regex) writeRegex(b, key, val)
+    else if (val is Regex) writeRegex(b, key, val)
     else throw Err("unknown BSON type - can't serialize ${Type.of(val).qname}")
   }
   
     b.write(Bson.MAXKEY)
     writeCStr(b.out, key)
   }
+
+  private static Void writeRegex(Buf b, Str key, Regex exp)
+  {
+     b.write(Bson.REGEX)
+     writeCStr(b.out, key)
+     writeCStr(b.out, exp.toStr)
+     writeCStr(b.out, "iu") // TODO: implement flags support in Fantom
+  }
   
 }