mirror of
https://github.com/Ed94/Odin.git
synced 2026-08-04 22:58:46 +00:00
Add for package utf8: rune_at_pos, rune_string_at_pos, rune_at, rune_offset
This commit is contained in:
@@ -161,19 +161,58 @@ decode_last_rune :: proc(s: []u8) -> (rune, int) {
|
|||||||
return r, size;
|
return r, size;
|
||||||
}
|
}
|
||||||
|
|
||||||
rune_index :: proc(s: string, index: int) -> (r: rune = RUNE_ERROR, ok: bool = false) {
|
rune_at_pos :: proc(s: string, pos: int) -> rune {
|
||||||
if index < 0 {
|
if pos < 0 {
|
||||||
return;
|
return RUNE_ERROR;
|
||||||
}
|
}
|
||||||
|
|
||||||
i := 0;
|
i := 0;
|
||||||
for c in s {
|
for c in s {
|
||||||
if i == index {
|
if i == pos {
|
||||||
return r, true;
|
return r;
|
||||||
}
|
}
|
||||||
i += 1;
|
i += 1;
|
||||||
}
|
}
|
||||||
return;
|
return RUNE_ERROR;
|
||||||
|
}
|
||||||
|
|
||||||
|
rune_string_at_pos :: proc(s: string, pos: int) -> string {
|
||||||
|
if pos < 0 {
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
|
||||||
|
i := 0;
|
||||||
|
for c, offset in s {
|
||||||
|
if i == pos {
|
||||||
|
w := rune_size(c);
|
||||||
|
return s[offset:][:w];
|
||||||
|
}
|
||||||
|
i += 1;
|
||||||
|
}
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
|
||||||
|
rune_at :: proc(s: string, byte_index: int) -> rune {
|
||||||
|
str := s[byte_index:];
|
||||||
|
r, _ := decode_rune_in_string(s[byte_index:]);
|
||||||
|
return r;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Returns the byte position of rune at position pos in s with an optional start byte position.
|
||||||
|
// Returns -1 if it runs out of the string.
|
||||||
|
rune_offset :: proc(s: string, pos: int, start: int = 0) -> int {
|
||||||
|
if pos < 0 {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
i := 0;
|
||||||
|
for c, offset in s[start:] {
|
||||||
|
if i == pos {
|
||||||
|
return offset+start;
|
||||||
|
}
|
||||||
|
i += 1;
|
||||||
|
}
|
||||||
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
valid_rune :: proc(r: rune) -> bool {
|
valid_rune :: proc(r: rune) -> bool {
|
||||||
|
|||||||
Reference in New Issue
Block a user