mirror of
https://github.com/Ed94/Odin.git
synced 2026-08-05 07:08:48 +00:00
Use uint instead of int to improve code generation for bounds checking
This commit is contained in:
@@ -240,14 +240,11 @@ buffer_read_ptr :: proc(b: ^Buffer, ptr: rawptr, size: int) -> (n: int, err: io.
|
|||||||
buffer_read_at :: proc(b: ^Buffer, p: []byte, offset: int) -> (n: int, err: io.Error) {
|
buffer_read_at :: proc(b: ^Buffer, p: []byte, offset: int) -> (n: int, err: io.Error) {
|
||||||
b.last_read = .Invalid
|
b.last_read = .Invalid
|
||||||
|
|
||||||
if offset < 0 || offset >= len(b.buf) {
|
if uint(offset) >= len(b.buf) {
|
||||||
err = .Invalid_Offset
|
err = .Invalid_Offset
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
n = copy(p, b.buf[offset:])
|
||||||
if 0 <= offset && offset < len(b.buf) {
|
|
||||||
n = copy(p, b.buf[offset:])
|
|
||||||
}
|
|
||||||
if n > 0 {
|
if n > 0 {
|
||||||
b.last_read = .Read
|
b.last_read = .Read
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -638,7 +638,7 @@ trim_left_proc :: proc(s: []byte, p: proc(rune) -> bool) -> []byte {
|
|||||||
|
|
||||||
index_rune :: proc(s: []byte, r: rune) -> int {
|
index_rune :: proc(s: []byte, r: rune) -> int {
|
||||||
switch {
|
switch {
|
||||||
case 0 <= r && r < utf8.RUNE_SELF:
|
case u32(r) < utf8.RUNE_SELF:
|
||||||
return index_byte(s, byte(r))
|
return index_byte(s, byte(r))
|
||||||
|
|
||||||
case r == utf8.RUNE_ERROR:
|
case r == utf8.RUNE_ERROR:
|
||||||
|
|||||||
@@ -18,7 +18,7 @@ type_assertion_trap :: proc "contextless" () -> ! {
|
|||||||
|
|
||||||
|
|
||||||
bounds_check_error :: proc "contextless" (file: string, line, column: i32, index, count: int) {
|
bounds_check_error :: proc "contextless" (file: string, line, column: i32, index, count: int) {
|
||||||
if 0 <= index && index < count {
|
if uint(index) < uint(count) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@(cold)
|
@(cold)
|
||||||
@@ -99,8 +99,8 @@ dynamic_array_expr_error :: proc "contextless" (file: string, line, column: i32,
|
|||||||
|
|
||||||
|
|
||||||
matrix_bounds_check_error :: proc "contextless" (file: string, line, column: i32, row_index, column_index, row_count, column_count: int) {
|
matrix_bounds_check_error :: proc "contextless" (file: string, line, column: i32, row_index, column_index, row_count, column_count: int) {
|
||||||
if 0 <= row_index && row_index < row_count &&
|
if uint(row_index) < uint(row_count) &&
|
||||||
0 <= column_index && column_index < column_count {
|
uint(column_index) < uint(column_count) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@(cold)
|
@(cold)
|
||||||
|
|||||||
@@ -321,14 +321,14 @@ last_ptr :: proc(array: $T/[]$E) -> ^E {
|
|||||||
}
|
}
|
||||||
|
|
||||||
get :: proc(array: $T/[]$E, index: int) -> (value: E, ok: bool) {
|
get :: proc(array: $T/[]$E, index: int) -> (value: E, ok: bool) {
|
||||||
if 0 <= index && index < len(array) {
|
if uint(index) < len(array) {
|
||||||
value = array[index]
|
value = array[index]
|
||||||
ok = true
|
ok = true
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
get_ptr :: proc(array: $T/[]$E, index: int) -> (value: ^E, ok: bool) {
|
get_ptr :: proc(array: $T/[]$E, index: int) -> (value: ^E, ok: bool) {
|
||||||
if 0 <= index && index < len(array) {
|
if uint(index) < len(array) {
|
||||||
value = &array[index]
|
value = &array[index]
|
||||||
ok = true
|
ok = true
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -760,7 +760,7 @@ last_index_byte :: proc(s: string, c: byte) -> int {
|
|||||||
*/
|
*/
|
||||||
index_rune :: proc(s: string, r: rune) -> int {
|
index_rune :: proc(s: string, r: rune) -> int {
|
||||||
switch {
|
switch {
|
||||||
case 0 <= r && r < utf8.RUNE_SELF:
|
case u32(r) < utf8.RUNE_SELF:
|
||||||
return index_byte(s, byte(r))
|
return index_byte(s, byte(r))
|
||||||
|
|
||||||
case r == utf8.RUNE_ERROR:
|
case r == utf8.RUNE_ERROR:
|
||||||
|
|||||||
Reference in New Issue
Block a user