Beispiel #1
0
 def quote(self):
     return quoteStr(self._s)
Beispiel #2
0
 def pretty(self, out):
     out.write(quoteStr(self._s).encode("utf-8"))
Beispiel #3
0
    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)
Beispiel #4
0
 def visitStrExpr(self, s, span):
     self.write(quoteStr(s))
Beispiel #5
0
 def visitStrExpr(self, s):
     self.write(quoteStr(s))
Beispiel #6
0
 def toQuote(self):
     return quoteStr(self._s)
Beispiel #7
0
 def testStrNewline(self):
     self.assertEqual(quoteStr(u"\n"), u'"\\n"')
Beispiel #8
0
 def testStrNull(self):
     self.assertEqual(quoteStr(u"\x00"), u'"\\x00"')
Beispiel #9
0
 def testStr(self):
     self.assertEqual(quoteStr(u"asdf"), u'"asdf"')
Beispiel #10
0
 def testStrDoubleQuote(self):
     self.assertEqual(quoteStr(u'""'), u'"\\"\\""')
Beispiel #11
0
 def testStrNull(self):
     self.assertEqual(quoteStr(u"\x00"), u'"\\x00"')
Beispiel #12
0
 def testStrNewline(self):
     self.assertEqual(quoteStr(u"\n"), u'"\\n"')
Beispiel #13
0
 def testStrDoubleQuote(self):
     self.assertEqual(quoteStr(u'""'), u'"\\"\\""')
Beispiel #14
0
 def testStr(self):
     self.assertEqual(quoteStr(u"asdf"), u'"asdf"')
Beispiel #15
0
 def pretty(self, out):
     out.write(quoteStr(self._s).encode("utf-8"))