def quote(self): return quoteStr(self._s)
def pretty(self, out): out.write(quoteStr(self._s).encode("utf-8"))
def recv(self, atom, args): if atom is ADD_1: other = args[0] if isinstance(other, StrObject): return StrObject(self._s + other._s) if isinstance(other, CharObject): return StrObject(self._s + unicode(other._c)) if atom is ASLIST_0: from typhon.objects.collections.lists import ConstList return ConstList(self.asList()) if atom is ASSET_0: from typhon.objects.collections.sets import ConstSet return ConstSet(self.asSet()) if atom is CONTAINS_1: needle = args[0] if isinstance(needle, CharObject): return wrapBool(needle._c in self._s) if isinstance(needle, StrObject): return wrapBool(needle._s in self._s) if atom is ENDSWITH_1: return wrapBool(self._s.endswith(unwrapStr(args[0]))) if atom is GET_1: index = unwrapInt(args[0]) if not 0 <= index < len(self._s): raise userError(u"string.get/1: Index out of bounds: %d" % index) return CharObject(self._s[index]) if atom is GETSPAN_0: return NullObject if atom is INDEXOF_1: needle = unwrapStr(args[0]) return IntObject(self._s.find(needle)) if atom is INDEXOF_2: needle = unwrapStr(args[0]) offset = unwrapInt(args[1]) if offset < 0: raise userError(u"indexOf/2: Negative offset %d not supported" % offset) return IntObject(self._s.find(needle, offset)) if atom is JOIN_1: from typhon.objects.collections.lists import unwrapList return StrObject(self.join(unwrapList(args[0]))) if atom is LASTINDEXOF_1: needle = unwrapStr(args[0]) return IntObject(self._s.rfind(needle)) if atom is MULTIPLY_1: amount = args[0] if isinstance(amount, IntObject): return StrObject(self._s * amount._i) if atom is OP__CMP_1: return polyCmp(self._s, unwrapStr(args[0])) if atom is REPLACE_2: return StrObject(replace(self._s, unwrapStr(args[0]), unwrapStr(args[1]))) if atom is QUOTE_0: return StrObject(quoteStr(self._s)) if atom is SIZE_0: return IntObject(len(self._s)) if atom is SLICE_1: start = unwrapInt(args[0]) if start < 0: raise userError(u"Slice start cannot be negative") return StrObject(self._s[start:]) if atom is SLICE_2: start = unwrapInt(args[0]) stop = unwrapInt(args[1]) if start < 0: raise userError(u"Slice start cannot be negative") if stop < 0: raise userError(u"Slice stop cannot be negative") return StrObject(self._s[start:stop]) if atom is SPLIT_1: from typhon.objects.collections.lists import ConstList return ConstList(self.split(unwrapStr(args[0]))) if atom is SPLIT_2: from typhon.objects.collections.lists import ConstList return ConstList(self.split(unwrapStr(args[0]), unwrapInt(args[1]))) if atom is STARTSWITH_1: return wrapBool(self._s.startswith(unwrapStr(args[0]))) if atom is TOLOWERCASE_0: return StrObject(self.toLowerCase()) if atom is TOUPPERCASE_0: return StrObject(self.toUpperCase()) if atom is TRIM_0: return StrObject(self.trim()) if atom is WITH_1: return StrObject(self._s + unwrapChar(args[0])) if atom is _MAKEITERATOR_0: return strIterator(self._s) raise Refused(self, atom, args)
def visitStrExpr(self, s, span): self.write(quoteStr(s))
def visitStrExpr(self, s): self.write(quoteStr(s))
def toQuote(self): return quoteStr(self._s)
def testStrNewline(self): self.assertEqual(quoteStr(u"\n"), u'"\\n"')
def testStrNull(self): self.assertEqual(quoteStr(u"\x00"), u'"\\x00"')
def testStr(self): self.assertEqual(quoteStr(u"asdf"), u'"asdf"')
def testStrDoubleQuote(self): self.assertEqual(quoteStr(u'""'), u'"\\"\\""')