class FindGroupsFirst(LibFcn): name = prefix + "findgroupsfirst" sig = Sigs([ Sig([{ "haystack": P.String() }, { "pattern": P.String() }], P.Array(P.String())), Sig([{ "haystack": P.Bytes() }, { "pattern": P.Bytes() }], P.Array(P.Bytes())) ]) errcodeBase = 35080 def __call__(self, state, scope, pos, paramTypes, haystack, pattern): haystack, pattern, to = convert(haystack, pattern, paramTypes[0]) re = Regexer(haystack, pattern, self.errcodeBase + 0, self.name, pos) start = 0 found = re.search(start) out = [] region = re.getRegion() if (found): for i in range(0, re.groupsFound()): out.append(to(haystack[region.beg[i]:region.end[i]])) else: out = [] re.free() return out
class ReplaceFirst(LibFcn): name = prefix + "replacefirst" sig = Sigs([ Sig([{ "haystack": P.String() }, { "pattern": P.String() }, { "replacement": P.String() }], P.String()), Sig([{ "haystack": P.Bytes() }, { "pattern": P.Bytes() }, { "replacement": P.Bytes() }], P.Bytes()) ]) errcodeBase = 35110 def __call__(self, state, scope, pos, paramTypes, haystack, pattern, replacement): haystack, pattern, to = convert(haystack, pattern, paramTypes[0]) re = Regexer(haystack, pattern, self.errcodeBase + 0, self.name, pos) found = re.search(0) region = re.getRegion() if found: out = to(haystack[:region.beg[0]]) + replacement + to( haystack[region.end[0]:]) else: out = to(haystack) re.free() return out
class FindAll(LibFcn): name = prefix + "findall" sig = Sigs([ Sig([{ "haystack": P.String() }, { "pattern": P.String() }], P.Array(P.String())), Sig([{ "haystack": P.Bytes() }, { "pattern": P.Bytes() }], P.Array(P.Bytes())) ]) errcodeBase = 35060 def __call__(self, state, scope, pos, paramTypes, haystack, pattern): haystack, pattern, to = convert(haystack, pattern, paramTypes[0]) re = Regexer(haystack, pattern, self.errcodeBase + 0, self.name, pos) found = re.search(0) region = re.getRegion() start = region.end[0] out = [] if found: while found: region = re.getRegion() start = region.end[0] out.append(to(haystack[region.beg[0]:region.end[0]])) found = re.search(start) else: out = [] re.free() return out
class FindFirst(LibFcn): name = prefix + "findfirst" sig = Sigs([ Sig([{ "haystack": P.String() }, { "pattern": P.String() }], P.Union([P.String(), P.Null()])), Sig([{ "haystack": P.Bytes() }, { "pattern": P.Bytes() }], P.Union([P.Bytes(), P.Null()])) ]) errcodeBase = 35070 def __call__(self, state, scope, pos, paramTypes, haystack, pattern): haystack, pattern, to = convert(haystack, pattern, paramTypes[0]) re = Regexer(haystack, pattern, self.errcodeBase + 0, self.name, pos) found = re.search(0) if found: region = re.getRegion() out = to(haystack[region.beg[0]:region.end[0]]) else: out = None re.free() if out is not None: return {paramTypes[0]: out} else: return out
class RIndex(LibFcn): name = prefix + "rindex" sig = Sigs([ Sig([{ "haystack": P.String() }, { "pattern": P.String() }], P.Array(P.Int())), Sig([{ "haystack": P.Bytes() }, { "pattern": P.Bytes() }], P.Array(P.Int())) ]) errcodeBase = 35030 def __call__(self, state, scope, pos, paramTypes, haystack, pattern): haystack, pattern, to = convert(haystack, pattern, paramTypes[0]) re = Regexer(haystack, pattern, self.errcodeBase + 0, self.name, pos) found = re.search(0) region = re.getRegion() start = 0 if found: while found: region = re.getRegion() start = region.end[0] found = re.search(start) out = [region.beg[0], region.end[0]] else: out = [] re.free() return out
class Groups(LibFcn): name = prefix + "groups" sig = Sigs([ Sig([{ "haystack": P.String() }, { "pattern": P.String() }], P.Array(P.Array(P.Int()))), Sig([{ "haystack": P.Bytes() }, { "pattern": P.Bytes() }], P.Array(P.Array(P.Int()))) ]) errcodeBase = 35040 def __call__(self, state, scope, pos, paramTypes, haystack, pattern): haystack, pattern, to = convert(haystack, pattern, paramTypes[0]) re = Regexer(haystack, pattern, self.errcodeBase + 0, self.name, pos) found = re.search(0) region = re.getRegion() start = region.end[0] out = [] if found: for i in range(0, re.groupsFound()): out.append([region.beg[i], region.end[i]]) else: out = [] re.free() return out
class Count(LibFcn): name = prefix + "count" sig = Sigs([ Sig([{ "haystack": P.String() }, { "pattern": P.String() }], P.Int()), Sig([{ "haystack": P.Bytes() }, { "pattern": P.Bytes() }], P.Int()) ]) errcodeBase = 35020 def __call__(self, state, scope, pos, paramTypes, haystack, pattern): haystack, pattern, to = convert(haystack, pattern, paramTypes[0]) re = Regexer(haystack, pattern, self.errcodeBase + 0, self.name, pos) total = 0 found = re.search(0) region = re.getRegion() start = region.end[0] while found: total += 1 found = re.search(start) region = re.getRegion() start = region.end[0] re.free() return total
class ReplaceAll(LibFcn): name = prefix + "replaceall" sig = Sigs([Sig([{"haystack": P.String()}, {"pattern": P.String()}, {"replacement": P.String()}], P.String()), Sig([{"haystack": P.Bytes()}, {"pattern": P.Bytes()}, {"replacement": P.Bytes()}], P.Bytes())]) errcodeBase = 35140 def __call__(self, state, scope, pos, paramTypes, haystack, pattern, replacement): original = haystack haystack, pattern, to = convert(haystack, pattern, paramTypes[0]) re = Regexer(haystack, pattern, self.errcodeBase + 0, self.name, pos) found = re.search(0) region = re.getRegion() beg = 0 end = region.beg[0] out = "" if found: while found: out = out + to(haystack[beg:end]) + replacement beg = region.end[0] found = re.search(beg) region = re.getRegion() end = region.beg[0] if beg != len(haystack): out = out + to(haystack[beg:]) else: out = original re.free() return out
class Split(LibFcn): name = prefix + "split" sig = Sigs([Sig([{"haystack": P.String()}, {"pattern": P.String()}], P.Array(P.String())), Sig([{"haystack": P.Bytes()}, {"pattern": P.Bytes()}], P.Array(P.Bytes()))]) errcodeBase = 35130 def __call__(self, state, scope, pos, paramTypes, haystack, pattern): haystack, pattern, to = convert(haystack, pattern, paramTypes[0]) re = Regexer(haystack, pattern, self.errcodeBase + 0, self.name, pos) out = [] start = 0 found = re.search(start) region = re.getRegion() beg = 0 end = region.beg[0] if (end == 0): flag = True else: flag = False if found: while found: out.append(to(haystack[beg:end])) beg = region.end[0] found = re.search(beg) region = re.getRegion() end = region.beg[0] if beg != len(haystack): out.append(to(haystack[beg:])) if flag: out = out[1:] else: out = [to(haystack)] re.free() return out
class Contains(LibFcn): name = prefix + "contains" sig = Sigs([Sig([{"haystack": P.String()}, {"pattern": P.String()}], P.Boolean()), Sig([{"haystack": P.Bytes()}, {"pattern": P.Bytes()}], P.Boolean())]) errcodeBase = 35010 def __call__(self, state, scope, pos, paramTypes, haystack, pattern): haystack, pattern, to = convert(haystack, pattern, paramTypes[0]) re = Regexer(haystack, pattern, self.errcodeBase + 0, self.name, pos) out = re.search(0) re.free() return out
class Subseq(LibFcn): name = prefix + "subseq" sig = Sig([{ "x": P.Bytes() }, { "start": P.Int() }, { "end": P.Int() }], P.Bytes()) errcodeBase = 16010 def __call__(self, state, scope, pos, paramTypes, x, start, end): return x[start:end]
class RandomBytes(LibFcn): name = prefix + "bytes" sig = Sigs([ Sig([{ "size": P.Int() }], P.Bytes()), Sig([{ "size": P.Int() }, { "population": P.Bytes() }], P.Bytes()), Sig([{ "size": P.Int() }, { "low": P.Int() }, { "high": P.Int() }], P.Bytes()) ]) errcodeBase = 34090 def __call__(self, state, scope, pos, paramTypes, size, *args): if size <= 0: raise PFARuntimeException("size must be positive", self.errcodeBase + 0, self.name, pos) if len(args) == 0: return "".join( chr(state.rand.randint(0, 255)) for x in range(size)) elif len(args) == 1: if len(args[0]) == 0: raise PFARuntimeException("population must be non-empty", self.errcodeBase + 3, self.name, pos) if isinstance(args[0], str): population = list(args[0]) elif isinstance(args[0], bytes): population = list(bytesToString(args[0])) return "".join(population[state.rand.randint(0, len(args[0]) - 1)] for x in range(size)) else: low, high = args if high <= low: raise PFARuntimeException("high must be greater than low", self.errcodeBase + 1, self.name, pos) if low < 0 or low > 255 or high < 0 or high > 256: raise PFARuntimeException("invalid byte", self.errcodeBase + 2, self.name, pos) return "".join( chr(state.rand.randint(low, high - 1)) for x in range(size))
class ToBase64(LibFcn): name = prefix + "toBase64" sig = Sig([{"x": P.Bytes()}], P.String()) errcodeBase = 16210 def __call__(self, state, scope, pos, paramTypes, x): return base64.b64encode(x)
class Tester(LibFcn): sig = Sigs([ Sig([{ "x": P.Bytes() }], P.Boolean()), Sig([{ "x": P.String() }], P.Boolean()) ]) codec = None def __call__(self, state, scope, pos, paramTypes, x): if paramTypes[0] == "bytes" or paramTypes[0] == {"type": "bytes"}: try: codecs.decode(x, self.codec, "strict") except UnicodeDecodeError: return False else: return True else: try: codecs.encode(x, self.codec, "strict") except UnicodeEncodeError: return False else: return True
class ToBytes(LibFcn): name = prefix + "toBytes" sig = Sig([{"x": P.WildFixed("A")}], P.Bytes()) errcodeBase = 20000 def __call__(self, state, scope, pos, paramTypes, x): return x
class SubseqTo(LibFcn): name = prefix + "subseqto" sig = Sig([{ "x": P.Bytes() }, { "start": P.Int() }, { "end": P.Int() }, { "replacement": P.Bytes() }], P.Bytes()) errcodeBase = 16020 def __call__(self, state, scope, pos, paramTypes, x, start, end, replacement): normStart, normEnd = startEnd(len(x), start, end) before = x[:normStart] after = x[normEnd:] return before + replacement + after
class Len(LibFcn): name = prefix + "len" sig = Sig([{"x": P.Bytes()}], P.Int()) errcodeBase = 16000 def genpy(self, paramTypes, args, pos): return "len({0})".format(*args) def __call__(self, state, scope, pos, paramTypes, x): return len(x)
class Encoder(LibFcn): sig = Sig([{"x": P.String()}], P.Bytes()) codec = None def __call__(self, state, scope, pos, paramTypes, x): try: return codecs.encode(x, self.codec, "strict") except UnicodeEncodeError: raise PFARuntimeException("invalid string", self.errcodeBase + 0, self.name, pos)
class FromBytes(LibFcn): name = prefix + "fromBytes" sig = Sig([{ "original": P.WildFixed("A") }, { "replacement": P.Bytes() }], P.Wildcard("A")) errcodeBase = 20010 def __call__(self, state, scope, pos, paramTypes, original, replacement): length = min(len(original), len(replacement)) return replacement[0:length] + original[length:len(original)]
class Decoder(LibFcn): sig = Sig([{"x": P.Bytes()}], P.String()) codec = None def __call__(self, state, scope, pos, paramTypes, x): try: if isinstance(x, str): x = stringToBytes(x) return codecs.decode(x, self.codec, "strict") except UnicodeDecodeError as err: raise PFARuntimeException("invalid bytes", self.errcodeBase + 0, self.name, pos)
class Decoder(LibFcn): sig = Sig([{"x": P.Bytes()}], P.String()) codec = None def __call__(self, state, scope, pos, paramTypes, x): # Hack for Python 3: return without doing anything if sys.version_info[0] == 3 and isinstance(x, str): return x try: return codecs.decode(x, self.codec, "strict") except UnicodeDecodeError as err: raise PFARuntimeException("invalid bytes", self.errcodeBase + 0, self.name, pos)
class FromBase64(LibFcn): name = prefix + "fromBase64" sig = Sig([{"s": P.String()}], P.Bytes()) errcodeBase = 16220 def __call__(self, state, scope, pos, paramTypes, s): try: if re.match("^[A-Za-z0-9\+/]*=*$", s) is None: raise TypeError return base64.b64decode(s) except TypeError: raise PFARuntimeException("invalid base64", self.errcodeBase + 0, self.name, pos)
class CastAvro(LibFcn): name = prefix + "avro" sig = Sig([{"x": P.Wildcard("A")}], P.Bytes()) errcodeBase = 17110 def __call__(self, state, scope, pos, paramTypes, x): schema = avro.schema.Parse(json.dumps(paramTypes[0])) x = untagUnion(x, paramTypes[0]) bytes_io = io.BytesIO() writer = DatumWriter(schema) writer.write(x, BinaryEncoder(bytes_io)) bytes_io.flush() return bytesToString(bytes_io.getvalue())
def testPassThroughExactParameterMatches(self): self.assertTrue( self.matches( Sig([{ "x": P.Null() }], P.Null()).accepts([AvroNull()], self.version), ([AvroNull()], AvroNull()))) self.assertTrue( self.matches( Sig([{ "x": P.Int() }], P.Null()).accepts([AvroInt()], self.version), ([AvroInt()], AvroNull()))) self.assertTrue( self.matches( Sig([{ "x": P.Long() }], P.Null()).accepts([AvroLong()], self.version), ([AvroLong()], AvroNull()))) self.assertTrue( self.matches( Sig([{ "x": P.Float() }], P.Null()).accepts([AvroFloat()], self.version), ([AvroFloat()], AvroNull()))) self.assertTrue( self.matches( Sig([{ "x": P.Double() }], P.Null()).accepts([AvroDouble()], self.version), ([AvroDouble()], AvroNull()))) self.assertTrue( self.matches( Sig([{ "x": P.Bytes() }], P.Null()).accepts([AvroBytes()], self.version), ([AvroBytes()], AvroNull()))) self.assertTrue( self.matches( Sig([{ "x": P.String() }], P.Null()).accepts([AvroString()], self.version), ([AvroString()], AvroNull()))) self.assertTrue( self.matches( Sig([{ "x": P.Array(P.Int()) }], P.Null()).accepts([AvroArray(AvroInt())], self.version), ([AvroArray(AvroInt())], AvroNull()))) self.assertTrue( self.matches( Sig([{ "x": P.Map(P.Int()) }], P.Null()).accepts([AvroMap(AvroInt())], self.version), ([AvroMap(AvroInt())], AvroNull()))) self.assertTrue( self.matches( Sig([{ "x": P.Union([P.Int()]) }], P.Null()).accepts([AvroUnion([AvroInt()])], self.version), ([AvroUnion([AvroInt()])], AvroNull()))) self.assertTrue( self.matches( Sig([{ "x": P.Fixed(10) }], P.Null()).accepts([AvroFixed(10, "MyFixed")], self.version), ([AvroFixed(10, "MyFixed")], AvroNull()))) self.assertTrue( self.matches( Sig([{ "x": P.Fixed(10, "MyFixed") }], P.Null()).accepts([AvroFixed(10, "MyFixed")], self.version), ([AvroFixed(10, "MyFixed")], AvroNull()))) self.assertTrue( self.matches( Sig([{ "x": P.Enum(["one", "two", "three"]) }], P.Null()).accepts( [AvroEnum(["one", "two", "three"], "MyEnum")], self.version), ([AvroEnum(["one", "two", "three"], "MyEnum")], AvroNull()))) self.assertTrue( self.matches( Sig([{ "x": P.Enum(["one", "two", "three"], "MyEnum") }], P.Null()).accepts( [AvroEnum(["one", "two", "three"], "MyEnum")], self.version), ([AvroEnum(["one", "two", "three"], "MyEnum")], AvroNull()))) self.assertTrue( self.matches( Sig([{ "x": P.Record({"one": P.Int()}) }], P.Null()).accepts( [AvroRecord([AvroField("one", AvroInt())], "MyRecord")], self.version), ([AvroRecord([AvroField("one", AvroInt())], "MyRecord") ], AvroNull()))) self.assertTrue( self.matches( Sig([{ "x": P.Record({"one": P.Int()}, "MyRecord") }], P.Null()).accepts( [AvroRecord([AvroField("one", AvroInt())], "MyRecord")], self.version), ([AvroRecord([AvroField("one", AvroInt())], "MyRecord") ], AvroNull())))