Add core:text/regex

This commit is contained in:
Feoramund
2024-07-22 14:25:12 -04:00
parent ccf8b2764d
commit cb0704d51c
16 changed files with 3700 additions and 0 deletions
+27
View File
@@ -0,0 +1,27 @@
// This package helps break dependency cycles.
package regex_common
// VM limitations
MAX_CAPTURE_GROUPS :: 10
MAX_PROGRAM_SIZE :: int(max(i16))
MAX_CLASSES :: int(max(u8))
Flag :: enum u8 {
// Global: try to match the pattern anywhere in the string.
Global,
// Multiline: treat `^` and `$` as if they also match newlines.
Multiline,
// Case Insensitive: treat `a-z` as if it was also `A-Z`.
Case_Insensitive,
// Ignore Whitespace: bypass unescaped whitespace outside of classes.
Ignore_Whitespace,
// Unicode: let the compiler and virtual machine know to expect Unicode strings.
Unicode,
// No Capture: avoid saving capture group data entirely.
No_Capture,
// No Optimization: do not pass the pattern through the optimizer; for debugging.
No_Optimization,
}
Flags :: bit_set[Flag; u8]
+25
View File
@@ -0,0 +1,25 @@
package regex_common
@require import "core:os"
import "core:io"
import "core:strings"
ODIN_DEBUG_REGEX :: #config(ODIN_DEBUG_REGEX, false)
when ODIN_DEBUG_REGEX {
debug_stream := os.stream_from_handle(os.stderr)
}
write_padded_hex :: proc(w: io.Writer, #any_int n, zeroes: int) {
sb := strings.builder_make()
defer strings.builder_destroy(&sb)
sbw := strings.to_writer(&sb)
io.write_int(sbw, n, 0x10)
io.write_string(w, "0x")
for _ in 0..<max(0, zeroes - strings.builder_len(sb)) {
io.write_byte(w, '0')
}
io.write_int(w, n, 0x10)
}