#NEWLINE# def CEscape( text , as_utf8 ) : #NEWLINE# #INDENT# 'Escape a bytes string for use in an ascii protocol buffer.\n\n  text.encode(\'string_escape\') does not seem to satisfy our needs as it\n  encodes unprintable characters using two-digit hex escapes whereas our\n  C++ unescaping function allows hex escapes to be any length.  So,\n  "\x011".encode(\'string_escape\') ends up being "\\x011", which will be\n  decoded in C++ as a single-character string with char code 0x11.\n\n  Args:\n    text: A byte string to be escaped\n    as_utf8: Specifies if result should be returned in UTF-8 encoding\n  Returns:\n    Escaped string\n  ' #NEWLINE# Ord = ( ord if isinstance ( text , basestring ) else ( lambda x : x ) ) #NEWLINE# if as_utf8 : #NEWLINE# #INDENT# return '' . join ( ( _cescape_utf8_to_str [ Ord ( c ) ] for c in text ) ) #NEWLINE# #UNINDENT# return '' . join ( ( _cescape_byte_to_str [ Ord ( c ) ] for c in text ) )