ascii_set.odin 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. //+private
  2. package strings
  3. import "core:unicode/utf8"
  4. /*
  5. Ascii_Set is designed to store ASCII characters efficiently as a bit-array
  6. Each bit in the array corresponds to a specific ASCII character, where the value of the bit (0 or 1)
  7. indicates if the character is present in the set or not.
  8. */
  9. Ascii_Set :: distinct [8]u32
  10. /*
  11. Creates an Ascii_Set with unique characters from the input string.
  12. Inputs:
  13. - chars: A string containing characters to include in the Ascii_Set.
  14. Returns:
  15. - as: An Ascii_Set with unique characters from the input string.
  16. - ok: false if any character in the input string is not a valid ASCII character.
  17. */
  18. ascii_set_make :: proc(chars: string) -> (as: Ascii_Set, ok: bool) #no_bounds_check {
  19. for i in 0..<len(chars) {
  20. c := chars[i]
  21. if c >= utf8.RUNE_SELF {
  22. return
  23. }
  24. as[c>>5] |= 1 << uint(c&31)
  25. }
  26. ok = true
  27. return
  28. }
  29. /*
  30. Determines if a given char is contained within an Ascii_Set.
  31. Inputs:
  32. - as: The Ascii_Set to search.
  33. - c: The char to check for in the Ascii_Set.
  34. Returns:
  35. - res: A boolean indicating if the byte is contained in the Ascii_Set (true) or not (false).
  36. */
  37. ascii_set_contains :: proc(as: Ascii_Set, c: byte) -> (res: bool) #no_bounds_check {
  38. return as[c>>5] & (1<<(c&31)) != 0
  39. }