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

Method _char_to_value

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

Converts a character to its encoded value based on the number of bits per character. Args: c (str): The character to convert. bits_per_char (int): The number of bits per character. Returns: int: The encoded value of the character.

(self, c: str, bits_per_char: int)

Source from the content-addressed store, hash-verified

528 return bytes(bytes_array)
529
530 def _char_to_value(self, c: str, bits_per_char: int) -> int:
531 """
532 Converts a character to its encoded value based on the number of bits per character.
533
534 Args:
535 c (str): The character to convert.
536 bits_per_char (int): The number of bits per character.
537
538 Returns:
539 int: The encoded value of the character.
540 """
541 if bits_per_char == 5:
542 if "a" <= c <= "z":
543 return ord(c) - ord("a")
544 elif c == ".":
545 return 26
546 elif c == "_":
547 return 27
548 elif c == "$":
549 return 28
550 elif c == "|":
551 return 29
552 else:
553 raise ValueError(f"Unsupported character for LOWER_SPECIAL encoding: {c}")
554 elif bits_per_char == 6:
555 if "a" <= c <= "z":
556 return ord(c) - ord("a")
557 elif "A" <= c <= "Z":
558 return 26 + (ord(c) - ord("A"))
559 elif "0" <= c <= "9":
560 return 52 + (ord(c) - ord("0"))
561 elif c == self.special_char1:
562 return 62
563 elif c == self.special_char2:
564 return 63
565 else:
566 raise ValueError(f"Unsupported character for LOWER_UPPER_DIGIT_SPECIAL encoding: {c}")

Callers 1

_encode_genericMethod · 0.95

Calls

no outgoing calls

Tested by

no test coverage detected