github.com/grumpyhome/grumpy@v0.3.1-0.20201208125205-7b775405bdf1/grumpy-runtime-src/runtime/basestring.go (about) 1 // Copyright 2016 Google Inc. All Rights Reserved. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package grumpy 16 17 import ( 18 "regexp" 19 "strings" 20 ) 21 22 // EncodeDefault is the system default encoding. 23 const EncodeDefault = "utf8" 24 25 // Error handling modes that dictate the behavior of *Str.Decode and 26 // *Unicode.Encode when they encounter bad chars. 27 const ( 28 // EncodeStrict causes UnicodeError to be raised on bad chars. 29 EncodeStrict = "strict" 30 // EncodeReplace replaces bad chars with "\ufffd". 31 EncodeReplace = "replace" 32 // EncodeIgnore discards bad chars. 33 EncodeIgnore = "ignore" 34 ) 35 36 var ( 37 // BaseStringType is the object representing the Python 'basestring' 38 // type. 39 BaseStringType = newSimpleType("basestring", ObjectType) 40 encodingGarbageRegexp = regexp.MustCompile(`[^A-Za-z0-9]+`) 41 escapeMap = map[rune]string{ 42 '\\': `\\`, 43 '\'': `\'`, 44 '\n': `\n`, 45 '\r': `\r`, 46 '\t': `\t`, 47 } 48 ) 49 50 func initBaseStringType(map[string]*Object) { 51 BaseStringType.flags &^= typeFlagInstantiable 52 } 53 54 func normalizeEncoding(encoding string) string { 55 return strings.ToLower(encodingGarbageRegexp.ReplaceAllString(encoding, "")) 56 } 57 58 func escapeRune(r rune) []byte { 59 const hexTable = "0123456789abcdef" 60 61 if r < 0x100 { 62 return []byte{'\\', 'x', hexTable[r>>4], hexTable[r&0x0F]} 63 } 64 65 if r < 0x10000 { 66 return []byte{'\\', 'u', 67 hexTable[r>>12], hexTable[r>>8&0x0F], 68 hexTable[r>>4&0x0F], hexTable[r&0x0F]} 69 } 70 71 return []byte{'\\', 'U', 72 hexTable[r>>28], hexTable[r>>24&0x0F], 73 hexTable[r>>20&0x0F], hexTable[r>>16&0x0F], 74 hexTable[r>>12&0x0F], hexTable[r>>8&0x0F], 75 hexTable[r>>4&0x0F], hexTable[r&0x0F]} 76 }