Skip to content

Commit a38bf76

Browse files
committed
Refactor and rename is_ascii function
1 parent 336b670 commit a38bf76

File tree

8 files changed

+33
-37
lines changed

8 files changed

+33
-37
lines changed

objc2-foundation/src/__string_macro.rs

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -77,20 +77,16 @@ impl CFStringUtf16 {
7777
}
7878

7979
/// Returns `true` if `bytes` is entirely ASCII with no interior NULs.
80-
pub const fn is_ascii(bytes: &[u8]) -> bool {
80+
pub const fn is_ascii_no_nul(bytes: &[u8]) -> bool {
8181
let mut i = 0;
82-
loop {
83-
if i == bytes.len() {
84-
return true;
85-
}
86-
82+
while i < bytes.len() {
8783
let byte = bytes[i];
88-
if !byte.is_ascii() || byte == 0 {
84+
if !byte.is_ascii() || byte == b'\0' {
8985
return false;
9086
}
91-
9287
i += 1;
9388
}
89+
true
9490
}
9591

9692
pub struct Utf16Char {
@@ -273,7 +269,7 @@ macro_rules! ns_string {
273269

274270
const INPUT: &[u8] = $s.as_bytes();
275271

276-
if $crate::__string_macro::is_ascii(INPUT) {
272+
if $crate::__string_macro::is_ascii_no_nul(INPUT) {
277273
// Convert the input slice to an array with known length so that
278274
// we can add a NUL byte to it.
279275
const ASCII: [u8; INPUT.len() + 1] = {
@@ -355,15 +351,15 @@ mod tests {
355351

356352
#[test]
357353
fn test_is_ascii() {
358-
assert!(is_ascii(b"a"));
359-
assert!(is_ascii(b"abc"));
354+
assert!(is_ascii_no_nul(b"a"));
355+
assert!(is_ascii_no_nul(b"abc"));
360356

361-
assert!(!is_ascii(b"\xff"));
357+
assert!(!is_ascii_no_nul(b"\xff"));
362358

363-
assert!(!is_ascii(b"\0"));
364-
assert!(!is_ascii(b"a\0b"));
365-
assert!(!is_ascii(b"ab\0"));
366-
assert!(!is_ascii(b"a\0b\0"));
359+
assert!(!is_ascii_no_nul(b"\0"));
360+
assert!(!is_ascii_no_nul(b"a\0b"));
361+
assert!(!is_ascii_no_nul(b"ab\0"));
362+
assert!(!is_ascii_no_nul(b"a\0b\0"));
367363
}
368364

369365
#[test]

test-assembly/crates/test_ns_string/expected/apple-aarch64.s

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ Lloh0:
99
Lloh1:
1010
add x0, x0, l___unnamed_1@PAGEOFF
1111
mov w1, #3
12-
bl SYM(objc2_foundation::__string_macro::is_ascii::GENERATED_ID, 0)
12+
bl SYM(objc2_foundation::__string_macro::is_ascii_no_nul::GENERATED_ID, 0)
1313
tbz w0, #0, LBB0_2
1414
Lloh2:
1515
adrp x0, SYM(test_ns_string[CRATE_ID]::get_ascii::CFSTRING, 0)@PAGE
@@ -38,7 +38,7 @@ Lloh6:
3838
Lloh7:
3939
add x0, x0, l___unnamed_2@PAGEOFF
4040
mov w1, #5
41-
bl SYM(objc2_foundation::__string_macro::is_ascii::GENERATED_ID, 0)
41+
bl SYM(objc2_foundation::__string_macro::is_ascii_no_nul::GENERATED_ID, 0)
4242
tbz w0, #0, LBB1_2
4343
Lloh8:
4444
adrp x0, SYM(test_ns_string[CRATE_ID]::get_utf16::CFSTRING, 0)@PAGE
@@ -67,7 +67,7 @@ Lloh12:
6767
Lloh13:
6868
add x0, x0, l___unnamed_3@PAGEOFF
6969
mov w1, #6
70-
bl SYM(objc2_foundation::__string_macro::is_ascii::GENERATED_ID, 0)
70+
bl SYM(objc2_foundation::__string_macro::is_ascii_no_nul::GENERATED_ID, 0)
7171
tbz w0, #0, LBB2_2
7272
Lloh14:
7373
adrp x0, SYM(test_ns_string[CRATE_ID]::get_with_nul::CFSTRING, 0)@PAGE

test-assembly/crates/test_ns_string/expected/apple-armv7.s

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ _get_ascii:
1111
movt r0, :upper16:(l___unnamed_1-(LPC0_0+8))
1212
LPC0_0:
1313
add r0, pc, r0
14-
bl SYM(objc2_foundation::__string_macro::is_ascii::GENERATED_ID, 0)
14+
bl SYM(objc2_foundation::__string_macro::is_ascii_no_nul::GENERATED_ID, 0)
1515
cmp r0, #0
1616
beq LBB0_2
1717
movw r0, :lower16:(SYM(test_ns_string[CRATE_ID]::get_ascii::CFSTRING, 0)-(LPC0_2+8))
@@ -39,7 +39,7 @@ _get_utf16:
3939
movt r0, :upper16:(l___unnamed_2-(LPC1_0+8))
4040
LPC1_0:
4141
add r0, pc, r0
42-
bl SYM(objc2_foundation::__string_macro::is_ascii::GENERATED_ID, 0)
42+
bl SYM(objc2_foundation::__string_macro::is_ascii_no_nul::GENERATED_ID, 0)
4343
cmp r0, #0
4444
beq LBB1_2
4545
movw r0, :lower16:(SYM(test_ns_string[CRATE_ID]::get_utf16::CFSTRING, 0)-(LPC1_2+8))
@@ -67,7 +67,7 @@ _get_with_nul:
6767
movt r0, :upper16:(l___unnamed_3-(LPC2_0+8))
6868
LPC2_0:
6969
add r0, pc, r0
70-
bl SYM(objc2_foundation::__string_macro::is_ascii::GENERATED_ID, 0)
70+
bl SYM(objc2_foundation::__string_macro::is_ascii_no_nul::GENERATED_ID, 0)
7171
cmp r0, #0
7272
beq LBB2_2
7373
movw r0, :lower16:(SYM(test_ns_string[CRATE_ID]::get_with_nul::CFSTRING, 0)-(LPC2_2+8))

test-assembly/crates/test_ns_string/expected/apple-armv7s.s

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ _get_ascii:
1111
movt r0, :upper16:(l___unnamed_1-(LPC0_0+8))
1212
LPC0_0:
1313
add r0, pc, r0
14-
bl SYM(objc2_foundation::__string_macro::is_ascii::GENERATED_ID, 0)
14+
bl SYM(objc2_foundation::__string_macro::is_ascii_no_nul::GENERATED_ID, 0)
1515
cmp r0, #0
1616
beq LBB0_2
1717
movw r0, :lower16:(SYM(test_ns_string[CRATE_ID]::get_ascii::CFSTRING, 0)-(LPC0_2+8))
@@ -39,7 +39,7 @@ _get_utf16:
3939
movt r0, :upper16:(l___unnamed_2-(LPC1_0+8))
4040
LPC1_0:
4141
add r0, pc, r0
42-
bl SYM(objc2_foundation::__string_macro::is_ascii::GENERATED_ID, 0)
42+
bl SYM(objc2_foundation::__string_macro::is_ascii_no_nul::GENERATED_ID, 0)
4343
cmp r0, #0
4444
beq LBB1_2
4545
movw r0, :lower16:(SYM(test_ns_string[CRATE_ID]::get_utf16::CFSTRING, 0)-(LPC1_2+8))
@@ -67,7 +67,7 @@ _get_with_nul:
6767
movt r0, :upper16:(l___unnamed_3-(LPC2_0+8))
6868
LPC2_0:
6969
add r0, pc, r0
70-
bl SYM(objc2_foundation::__string_macro::is_ascii::GENERATED_ID, 0)
70+
bl SYM(objc2_foundation::__string_macro::is_ascii_no_nul::GENERATED_ID, 0)
7171
cmp r0, #0
7272
beq LBB2_2
7373
movw r0, :lower16:(SYM(test_ns_string[CRATE_ID]::get_with_nul::CFSTRING, 0)-(LPC2_2+8))

test-assembly/crates/test_ns_string/expected/apple-x86.s

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ L0$pb:
1313
lea eax, [esi + l___unnamed_1-L0$pb]
1414
mov dword ptr [esp], eax
1515
mov dword ptr [esp + 4], 3
16-
call SYM(objc2_foundation::__string_macro::is_ascii::GENERATED_ID, 0)
16+
call SYM(objc2_foundation::__string_macro::is_ascii_no_nul::GENERATED_ID, 0)
1717
test al, al
1818
je LBB0_1
1919
lea eax, [esi + SYM(test_ns_string[CRATE_ID]::get_ascii::CFSTRING, 0)-L0$pb]
@@ -45,7 +45,7 @@ L1$pb:
4545
lea eax, [esi + l___unnamed_2-L1$pb]
4646
mov dword ptr [esp], eax
4747
mov dword ptr [esp + 4], 5
48-
call SYM(objc2_foundation::__string_macro::is_ascii::GENERATED_ID, 0)
48+
call SYM(objc2_foundation::__string_macro::is_ascii_no_nul::GENERATED_ID, 0)
4949
test al, al
5050
je LBB1_1
5151
lea eax, [esi + SYM(test_ns_string[CRATE_ID]::get_utf16::CFSTRING, 0)-L1$pb]
@@ -77,7 +77,7 @@ L2$pb:
7777
lea eax, [esi + l___unnamed_3-L2$pb]
7878
mov dword ptr [esp], eax
7979
mov dword ptr [esp + 4], 6
80-
call SYM(objc2_foundation::__string_macro::is_ascii::GENERATED_ID, 0)
80+
call SYM(objc2_foundation::__string_macro::is_ascii_no_nul::GENERATED_ID, 0)
8181
test al, al
8282
je LBB2_1
8383
lea eax, [esi + SYM(test_ns_string[CRATE_ID]::get_with_nul::CFSTRING, 0)-L2$pb]

test-assembly/crates/test_ns_string/expected/apple-x86_64.s

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ _get_ascii:
77
mov rbp, rsp
88
lea rdi, [rip + l___unnamed_1]
99
mov esi, 3
10-
call SYM(objc2_foundation::__string_macro::is_ascii::GENERATED_ID, 0)
10+
call SYM(objc2_foundation::__string_macro::is_ascii_no_nul::GENERATED_ID, 0)
1111
test al, al
1212
je LBB0_1
1313
lea rdi, [rip + SYM(test_ns_string[CRATE_ID]::get_ascii::CFSTRING, 0)]
@@ -25,7 +25,7 @@ _get_utf16:
2525
mov rbp, rsp
2626
lea rdi, [rip + l___unnamed_2]
2727
mov esi, 5
28-
call SYM(objc2_foundation::__string_macro::is_ascii::GENERATED_ID, 0)
28+
call SYM(objc2_foundation::__string_macro::is_ascii_no_nul::GENERATED_ID, 0)
2929
test al, al
3030
je LBB1_1
3131
lea rdi, [rip + SYM(test_ns_string[CRATE_ID]::get_utf16::CFSTRING, 0)]
@@ -43,7 +43,7 @@ _get_with_nul:
4343
mov rbp, rsp
4444
lea rdi, [rip + l___unnamed_3]
4545
mov esi, 6
46-
call SYM(objc2_foundation::__string_macro::is_ascii::GENERATED_ID, 0)
46+
call SYM(objc2_foundation::__string_macro::is_ascii_no_nul::GENERATED_ID, 0)
4747
test al, al
4848
je LBB2_1
4949
lea rdi, [rip + SYM(test_ns_string[CRATE_ID]::get_with_nul::CFSTRING, 0)]

test-assembly/crates/test_ns_string/expected/gnustep-x86.s

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ get_ascii:
1515
add ebx, offset _GLOBAL_OFFSET_TABLE_+(.Ltmp0-.L0$pb)
1616
lea eax, [ebx + .L__unnamed_1@GOTOFF]
1717
mov dword ptr [esp], eax
18-
call SYM(objc2_foundation::__string_macro::is_ascii::GENERATED_ID, 0)@PLT
18+
call SYM(objc2_foundation::__string_macro::is_ascii_no_nul::GENERATED_ID, 0)@PLT
1919
test al, al
2020
je .LBB0_1
2121
lea eax, [ebx + SYM(test_ns_string[CRATE_ID]::get_ascii::CFSTRING, 0)@GOTOFF]
@@ -49,7 +49,7 @@ get_utf16:
4949
add ebx, offset _GLOBAL_OFFSET_TABLE_+(.Ltmp1-.L1$pb)
5050
lea eax, [ebx + .L__unnamed_2@GOTOFF]
5151
mov dword ptr [esp], eax
52-
call SYM(objc2_foundation::__string_macro::is_ascii::GENERATED_ID, 0)@PLT
52+
call SYM(objc2_foundation::__string_macro::is_ascii_no_nul::GENERATED_ID, 0)@PLT
5353
test al, al
5454
je .LBB1_1
5555
lea eax, [ebx + SYM(test_ns_string[CRATE_ID]::get_utf16::CFSTRING, 0)@GOTOFF]
@@ -83,7 +83,7 @@ get_with_nul:
8383
add ebx, offset _GLOBAL_OFFSET_TABLE_+(.Ltmp2-.L2$pb)
8484
lea eax, [ebx + .L__unnamed_3@GOTOFF]
8585
mov dword ptr [esp], eax
86-
call SYM(objc2_foundation::__string_macro::is_ascii::GENERATED_ID, 0)@PLT
86+
call SYM(objc2_foundation::__string_macro::is_ascii_no_nul::GENERATED_ID, 0)@PLT
8787
test al, al
8888
je .LBB2_1
8989
lea eax, [ebx + SYM(test_ns_string[CRATE_ID]::get_with_nul::CFSTRING, 0)@GOTOFF]

test-assembly/crates/test_ns_string/expected/gnustep-x86_64.s

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ get_ascii:
88
push rax
99
lea rdi, [rip + .L__unnamed_1]
1010
mov esi, 3
11-
call qword ptr [rip + SYM(objc2_foundation::__string_macro::is_ascii::GENERATED_ID, 0)@GOTPCREL]
11+
call qword ptr [rip + SYM(objc2_foundation::__string_macro::is_ascii_no_nul::GENERATED_ID, 0)@GOTPCREL]
1212
test al, al
1313
je .LBB0_1
1414
lea rdi, [rip + SYM(test_ns_string[CRATE_ID]::get_ascii::CFSTRING, 0)]
@@ -29,7 +29,7 @@ get_utf16:
2929
push rax
3030
lea rdi, [rip + .L__unnamed_2]
3131
mov esi, 5
32-
call qword ptr [rip + SYM(objc2_foundation::__string_macro::is_ascii::GENERATED_ID, 0)@GOTPCREL]
32+
call qword ptr [rip + SYM(objc2_foundation::__string_macro::is_ascii_no_nul::GENERATED_ID, 0)@GOTPCREL]
3333
test al, al
3434
je .LBB1_1
3535
lea rdi, [rip + SYM(test_ns_string[CRATE_ID]::get_utf16::CFSTRING, 0)]
@@ -50,7 +50,7 @@ get_with_nul:
5050
push rax
5151
lea rdi, [rip + .L__unnamed_3]
5252
mov esi, 6
53-
call qword ptr [rip + SYM(objc2_foundation::__string_macro::is_ascii::GENERATED_ID, 0)@GOTPCREL]
53+
call qword ptr [rip + SYM(objc2_foundation::__string_macro::is_ascii_no_nul::GENERATED_ID, 0)@GOTPCREL]
5454
test al, al
5555
je .LBB2_1
5656
lea rdi, [rip + SYM(test_ns_string[CRATE_ID]::get_with_nul::CFSTRING, 0)]

0 commit comments

Comments
 (0)