def PrintFieldValue(field, value, out, indent=0, as_utf8=False, as_one_line=False): """Print a single field value (not including name). For repeated fields, the value should be a single element.""" if field.cpp_type == descriptor.FieldDescriptor.CPPTYPE_MESSAGE: if as_one_line: out.write(b(' { ')) PrintMessage(value, out, indent, as_utf8, as_one_line) out.write(b('}')) else: out.write(b(' {\n')) PrintMessage(value, out, indent + 2, as_utf8, as_one_line) out.write(b(' ') * indent + b('}')) elif field.cpp_type == descriptor.FieldDescriptor.CPPTYPE_ENUM: enum_value = field.enum_type.values_by_number.get(value, None) if enum_value is not None: out.write(string_to_bytes(str(enum_value.name))) else: out.write(string_to_bytes(str(value))) elif field.cpp_type == descriptor.FieldDescriptor.CPPTYPE_STRING: out.write(b('\"')) if type(value) is unicode: out.write(_CEscape(value.encode('utf-8'), as_utf8)) else: out.write(_CEscape(value, as_utf8)) out.write(b('\"')) elif field.cpp_type == descriptor.FieldDescriptor.CPPTYPE_BOOL: if value: out.write(b("true")) else: out.write(b("false")) else: out.write(string_to_bytes(str(value)))
def PrintFieldValue(field, value, out, indent=0, as_utf8=False, as_one_line=False): """Print a single field value (not including name). For repeated fields, the value should be a single element.""" if field.cpp_type == descriptor.FieldDescriptor.CPPTYPE_MESSAGE: if as_one_line: out.write(b' { ') PrintMessage(value, out, indent, as_utf8, as_one_line) out.write(b'}') else: out.write(b' {\n') PrintMessage(value, out, indent + 2, as_utf8, as_one_line) out.write(b' ' * indent + b'}') elif field.cpp_type == descriptor.FieldDescriptor.CPPTYPE_ENUM: enum_value = field.enum_type.values_by_number.get(value, None) if enum_value is not None: out.write(string_to_bytes(str(enum_value.name))) else: out.write(string_to_bytes(str(value))) elif field.cpp_type == descriptor.FieldDescriptor.CPPTYPE_STRING: out.write(b'\"') if type(value) is unicode: out.write(_CEscape(value.encode('utf-8'), as_utf8)) else: out.write(_CEscape(value, as_utf8)) out.write(b'\"') elif field.cpp_type == descriptor.FieldDescriptor.CPPTYPE_BOOL: if value: out.write(b"true") else: out.write(b"false") else: out.write(string_to_bytes(str(value)))
def _CUnescape(text): def ReplaceHex(m): return m.group(0) # This is required because the 'string_escape' encoding doesn't # allow single-digit hex escapes (like '\xf'). result = _CUNESCAPE_HEX.sub(ReplaceHex, text) return string_to_bytes(loc_string_escape(result))
def _CUnescape(text): def ReplaceHex(m): # Only replace the match if the number of leading back slashes is odd. i.e. # the slash itself is not escaped. if len(m.group(1)) & 1: return m.group(1) + b('x0') + m.group(2) return m.group(0) # This is required because the 'string_escape' encoding doesn't # allow single-digit hex escapes (like '\xf'). result = _CUNESCAPE_HEX.sub(ReplaceHex, text) return string_to_bytes(loc_string_escape(result))
def escape(c): b = ord(c) if b == 10: return b"\\n" # optional escape if b == 13: return b"\\r" # optional escape if b == 9: return b"\\t" # optional escape if b == 39: return b"\\'" # optional escape if b == 34: return b'\\"' # necessary escape if b == 92: return b"\\\\" # necessary escape # necessary escapes if not as_utf8 and (b >= 127 or b < 32): return string_to_bytes("\\%03o" % b) return c
def escape(c): o = bytes_as_num(c) if o == 10: return b(r"\n") # optional escape if o == 13: return b(r"\r") # optional escape if o == 9: return b(r"\t") # optional escape if o == 39: return b(r"\'") # optional escape if o == 34: return b(r'\"') # necessary escape if o == 92: return b(r"\\") # necessary escape # necessary escapes if not as_utf8 and (o >= 127 or o < 32): return string_to_bytes("\\%03o" % o) return num_as_byte(c)
def escape(c): o = bytes_as_num(c) if o == 10: return rb"\n" # optional escape if o == 13: return rb"\r" # optional escape if o == 9: return rb"\t" # optional escape if o == 39: return rb"\'" # optional escape if o == 34: return rb'\"' # necessary escape if o == 92: return rb"\\" # necessary escape # necessary escapes if not as_utf8 and (o >= 127 or o < 32): return string_to_bytes("\\%03o" % o) return num_as_byte(c)
def testMergeStringFieldUnescape(self): message = unittest_pb2.TestAllTypes() text = r'''repeated_string: "\xf\x62" repeated_string: "\\xf\\x62" repeated_string: "\\\xf\\\x62" repeated_string: "\\\\xf\\\\x62" repeated_string: "\\\\\xf\\\\\x62" repeated_string: "\x5cx20"''' text_format.Merge(string_to_bytes(text), message) SLASH = '\\' self.assertEqual('\x0fb', message.repeated_string[0]) self.assertEqual(SLASH + 'xf' + SLASH + 'x62', message.repeated_string[1]) self.assertEqual(SLASH + '\x0f' + SLASH + 'b', message.repeated_string[2]) self.assertEqual(SLASH + SLASH + 'xf' + SLASH + SLASH + 'x62', message.repeated_string[3]) self.assertEqual(SLASH + SLASH + '\x0f' + SLASH + SLASH + 'b', message.repeated_string[4]) self.assertEqual(SLASH + 'x20', message.repeated_string[5])
def StringByteSize(field_number, string): return BytesByteSize(field_number, string_to_bytes(string))