MCPcopy Index your code
hub / github.com/clips/pattern / py_encode_basestring_ascii

Function py_encode_basestring_ascii

pattern/web/json/encoder.py:47–69  ·  view source on GitHub ↗

Return an ASCII-only JSON representation of a Python string

(s)

Source from the content-addressed store, hash-verified

45
46
47def py_encode_basestring_ascii(s):
48 """Return an ASCII-only JSON representation of a Python string
49
50 """
51 if isinstance(s, str) and HAS_UTF8.search(s) is not None:
52 s = s.decode('utf-8')
53 def replace(match):
54 s = match.group(0)
55 try:
56 return ESCAPE_DCT[s]
57 except KeyError:
58 n = ord(s)
59 if n < 0x10000:
60 #return '\\u{0:04x}'.format(n)
61 return '\\u%04x' % (n,)
62 else:
63 # surrogate pair
64 n -= 0x10000
65 s1 = 0xd800 | ((n >> 10) & 0x3ff)
66 s2 = 0xdc00 | (n & 0x3ff)
67 #return '\\u{0:04x}\\u{1:04x}'.format(s1, s2)
68 return '\\u%04x\\u%04x' % (s1, s2)
69 return '"' + str(ESCAPE_ASCII.sub(replace, s)) + '"'
70
71
72encode_basestring_ascii = (

Callers

nothing calls this directly

Calls 3

strFunction · 0.85
searchMethod · 0.45
decodeMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…