Coverage for /home/runner/work/zserio/zserio/compiler/extensions/python/runtime/src/zserio/builtin.py: 100%

11 statements  

« prev     ^ index     » next       coverage.py v6.5.0, created at 2024-12-05 10:43 +0000

1""" 

2The module provides implementation of zserio built-in operators. 

3""" 

4 

5import typing 

6 

7 

8def isset(bitmask_value: typing.Any, required_mask: typing.Any) -> bool: 

9 """ 

10 Checks whether the required_mask is set within the bitmask_value. 

11 

12 This method implements zserio built-in operator isset. 

13 

14 :param bitmask_value: Bitmask value to check. 

15 :param required_mask: Mask to use. 

16 :returns: True when the required_mask is set within the bitmask_value, False otherwise. 

17 """ 

18 

19 return (bitmask_value & required_mask) == required_mask 

20 

21 

22def numbits(num_values: int) -> int: 

23 """ 

24 Gets the minimum number of bits required to encode given number of different values. 

25 

26 This method implements zserio built-in operator numBits. 

27 

28 :param num_values: The number of different values from which to calculate number of bits. 

29 :returns: Number of bits required to encode num_values different values. 

30 """ 

31 

32 if num_values == 0: 

33 return 0 

34 if num_values == 1: 

35 return 1 

36 

37 return (num_values - 1).bit_length() 

38 

39 

40def lengthof_string(string: str) -> int: 

41 """ 

42 Gets length of the given string in UTF-8 bytes. 

43 

44 :param string: String value. 

45 :returns: Number of bytes needed to encode given string in UTF-8. 

46 """ 

47 

48 return len(string.encode("utf-8"))