MCPcopy Create free account
hub / github.com/apache/fory / MetaStringEncoder

Class MetaStringEncoder

python/pyfory/meta/metastring.py:259–566  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

257
258
259class MetaStringEncoder:
260 def __init__(self, special_char1: str, special_char2: str):
261 """
262 Creates a MetaStringEncoder with specified special characters used for encoding.
263
264 Args:
265 special_char1 (str): The first special character used in custom encoding.
266 special_char2 (str): The second special character used in custom encoding.
267 """
268 self.special_char1 = special_char1
269 self.special_char2 = special_char2
270
271 def encode(self, input_string: str, encodings: List[Encoding] = None) -> MetaString:
272 """
273 Encodes the input string into a MetaString object.
274
275 Args:
276 input_string (str): The string to encode.
277
278 Returns:
279 MetaString: The encoded MetaString object.
280 """
281 # Long meta string than _METASTRING_NUM_CHARS_LIMIT is not allowed.
282 assert len(input_string) < _METASTRING_NUM_CHARS_LIMIT, "Long meta string than _METASTRING_NUM_CHARS_LIMIT is not allowed."
283
284 if not input_string:
285 return MetaString(
286 input_string,
287 Encoding.UTF_8,
288 bytes(),
289 0,
290 self.special_char1,
291 self.special_char2,
292 )
293
294 encoding = self.compute_encoding(input_string, encodings)
295 return self.encode_with_encoding(input_string, encoding)
296
297 def encode_with_encoding(self, input_string: str, encoding: Encoding) -> MetaString:
298 """
299 Encodes the input string with the specified encoding.
300
301 Args:
302 input_string (str): The string to encode.
303 encoding (Encoding): The encoding type.
304
305 Returns:
306 MetaString: An encoded MetaString object.
307 """
308 # Long meta string longer than _METASTRING_NUM_CHARS_LIMIT is not allowed.
309 assert len(input_string) < _METASTRING_NUM_CHARS_LIMIT, "Long meta string longer than _METASTRING_NUM_CHARS_LIMIT is not allowed."
310
311 if not input_string:
312 return MetaString(
313 input_string,
314 Encoding.UTF_8,
315 bytes(),
316 0,

Calls

no outgoing calls