SectrPrototype/code/grime_unicode.odin

44 lines
937 B
Odin
Raw Normal View History

2024-03-02 15:19:27 -08:00
package sectr
2024-03-19 05:36:58 -07:00
rune16 :: distinct u16
2024-03-02 15:19:27 -08:00
2024-03-05 07:19:27 -08:00
2024-03-06 13:47:00 -08:00
// Exposing the alloc_error
@(require_results)
string_to_runes :: proc ( content : string, allocator := context.allocator) -> (runes : []rune, alloc_error : AllocatorError) #optional_allocator_error {
2024-03-06 13:47:00 -08:00
num := str_rune_count(content)
2024-03-05 07:19:27 -08:00
runes, alloc_error = make([]rune, num, allocator)
2024-03-06 13:47:00 -08:00
if runes == nil || alloc_error != AllocatorError.None {
return
2024-03-05 07:19:27 -08:00
}
idx := 0
2024-03-06 13:47:00 -08:00
for codepoint in content {
2024-03-05 07:19:27 -08:00
runes[idx] = codepoint
idx += 1
}
return
}
2024-03-19 05:36:58 -07:00
string_to_runes_array :: proc( content : string, allocator := context.allocator ) -> ( []rune, AllocatorError )
{
num := cast(u64) str_rune_count(content)
runes_array, alloc_error := array_init_reserve( rune, allocator, num )
if alloc_error != AllocatorError.None {
return nil, alloc_error
}
runes := array_to_slice(runes_array)
idx := 0
for codepoint in content {
runes[idx] = codepoint
idx += 1
}
return runes, alloc_error
}