ascii_set.odin 1.2 KB

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