internal_util.odin 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. //+private
  2. package os2
  3. import "base:intrinsics"
  4. import "base:runtime"
  5. // Splits pattern by the last wildcard "*", if it exists, and returns the prefix and suffix
  6. // parts which are split by the last "*"
  7. @(require_results)
  8. _prefix_and_suffix :: proc(pattern: string) -> (prefix, suffix: string, err: Error) {
  9. for i in 0..<len(pattern) {
  10. if is_path_separator(pattern[i]) {
  11. err = .Pattern_Has_Separator
  12. return
  13. }
  14. }
  15. prefix = pattern
  16. for i := len(pattern)-1; i >= 0; i -= 1 {
  17. if pattern[i] == '*' {
  18. prefix, suffix = pattern[:i], pattern[i+1:]
  19. break
  20. }
  21. }
  22. return
  23. }
  24. @(require_results)
  25. clone_string :: proc(s: string, allocator: runtime.Allocator) -> (res: string, err: runtime.Allocator_Error) {
  26. buf := make([]byte, len(s), allocator) or_return
  27. copy(buf, s)
  28. return string(buf), nil
  29. }
  30. @(require_results)
  31. clone_to_cstring :: proc(s: string, allocator: runtime.Allocator) -> (res: cstring, err: runtime.Allocator_Error) {
  32. res = "" // do not use a `nil` cstring
  33. buf := make([]byte, len(s)+1, allocator) or_return
  34. copy(buf, s)
  35. buf[len(s)] = 0
  36. return cstring(&buf[0]), nil
  37. }
  38. @(require_results)
  39. temp_cstring :: proc(s: string) -> (cstring, runtime.Allocator_Error) #optional_allocator_error {
  40. return clone_to_cstring(s, temp_allocator())
  41. }
  42. @(require_results)
  43. string_from_null_terminated_bytes :: proc(b: []byte) -> (res: string) {
  44. s := string(b)
  45. i := 0
  46. for ; i < len(s); i += 1 {
  47. if s[i] == 0 {
  48. break
  49. }
  50. }
  51. return s[:i]
  52. }
  53. @(require_results)
  54. concatenate_strings_from_buffer :: proc(buf: []byte, strings: ..string) -> string {
  55. n := 0
  56. for s in strings {
  57. (n < len(buf)) or_break
  58. n += copy(buf[n:], s)
  59. }
  60. n = min(len(buf), n)
  61. return string(buf[:n])
  62. }
  63. @(require_results)
  64. concatenate :: proc(strings: []string, allocator: runtime.Allocator) -> (res: string, err: runtime.Allocator_Error) {
  65. n := 0
  66. for s in strings {
  67. n += len(s)
  68. }
  69. buf := make([]byte, n, allocator) or_return
  70. n = 0
  71. for s in strings {
  72. n += copy(buf[n:], s)
  73. }
  74. return string(buf), nil
  75. }
  76. @(private="file")
  77. random_string_seed: [2]u64
  78. @(init, private="file")
  79. init_random_string_seed :: proc() {
  80. seed := u64(intrinsics.read_cycle_counter())
  81. s := &random_string_seed
  82. s[0] = 0
  83. s[1] = (seed << 1) | 1
  84. _ = next_random(s)
  85. s[1] += seed
  86. _ = next_random(s)
  87. }
  88. @(require_results)
  89. next_random :: proc(r: ^[2]u64) -> u64 {
  90. old_state := r[0]
  91. r[0] = old_state * 6364136223846793005 + (r[1]|1)
  92. xor_shifted := (((old_state >> 59) + 5) ~ old_state) * 12605985483714917081
  93. rot := (old_state >> 59)
  94. return (xor_shifted >> rot) | (xor_shifted << ((-rot) & 63))
  95. }
  96. @(require_results)
  97. random_string :: proc(buf: []byte) -> string {
  98. @(static, rodata) digits := "0123456789"
  99. u := next_random(&random_string_seed)
  100. b :: 10
  101. i := len(buf)
  102. for u >= b {
  103. i -= 1
  104. buf[i] = digits[u % b]
  105. u /= b
  106. }
  107. i -= 1
  108. buf[i] = digits[u % b]
  109. return string(buf[i:])
  110. }