 	private class func write(elementType: BSONElementType, toStream stream: OutputStream) throws -> Int {
 		var t = elementType.rawValue
 		return try write(value: &t, toStream: stream)
+	}
+	
+	private class func bsonRegexpOptionsString(fromFoundationRegexp foundationRegexp: NSRegularExpression) -> String {
+		var result = ""
+		
+		let opt = foundationRegexp.options
+		if  opt.contains(.caseInsensitive)          {result += "i" /* Case insensitive matching */}
+		if !opt.contains(.anchorsMatchLines)        {result += "m" /* Multiline matching. Not sure if what we've set corresponds exactly to the MongoDB implementation's... */}
+		/* Unsupported in foundation flag: x (verbose) */
+		result += "l" /* We consider this flag to be a default, unremovable behaviour in Foundation (makes \w, \W, etc. locale dependent) */
+		if  opt.contains(.dotMatchesLineSeparators) {result += "s" /* Dotall mode ('.' matches everything). Not sure if exactly equivalent to Foundation option... */}
+		if  opt.contains(.useUnicodeWordBoundaries) {result += "u" /* Make \w, \W, etc. match unicode */}
+		/* Ignored foundation options: .allowCommentsAndWhitespace, .ignoreMetacharacters, .useUnixLineSeparators */
+		
+		return result
 	}
 	
 }

		/** An invalid BSON object was given to be serialized. The invalid element
		is passed in argument to this error. */
		case invalidBSONObject(invalidElement: Any)
		/** One of the key cannot be serialized in an unambiguous way. Said key is
		given in arg of the error. */
		case unserializableKey(String)
		
		/** Cannot allocate memory (either with `malloc` or `UnsafePointer.alloc()`). */
		case cannotAllocateMemory(Int)

		
		/* Let's get the UTF8 bytes of the string. */
		let bytes = [UInt8](str.utf8)
		guard !bytes.contains(0) else {throw BSONSerializationError.unserializableCString(str)}
		
		let curWrite = bytes.withUnsafeBufferPointer { p -> Int in return stream.write(p.baseAddress!, maxLength: bytes.count) }
		guard curWrite == bytes.count else {throw BSONSerializationError.cannotWriteToStream(streamError: stream.streamError)}
