Skip to main content

c/
string_more.rs

1// Copyright (c) 2026 Meng Zhuo
2// SPDX-License-Identifier: BSD-3-Clause
3
4// ============================================================
5// String/memory functions
6// ============================================================
7
8// strlen, memcpy, memset, memmove and memcmp live in string_fast.rs.
9
10#[no_mangle]
11pub unsafe extern "C" fn bcmp(s1: *const c_void, s2: *const c_void, n: usize) -> c_int {
12    memcmp(s1, s2, n)
13}
14
15// strcmp lives in string_fast.rs.
16
17#[no_mangle]
18pub unsafe extern "C" fn strncmp(s1: *const u8, s2: *const u8, n: usize) -> c_int {
19    let mut i = 0;
20    while i < n {
21        let a = *s1.add(i);
22        let b = *s2.add(i);
23        if a != b {
24            return a as c_int - b as c_int;
25        }
26        if a == 0 {
27            return 0;
28        }
29        i += 1;
30    }
31    0
32}
33
34#[no_mangle]
35pub unsafe extern "C" fn strcpy(dst: *mut u8, src: *const u8) -> *mut u8 {
36    let n = strlen(src as *const c_char);
37    core::ptr::copy_nonoverlapping(src, dst, n + 1);
38    dst
39}
40
41#[no_mangle]
42pub unsafe extern "C" fn strncpy(dst: *mut u8, src: *const u8, n: usize) -> *mut u8 {
43    let l = strnlen(src, n);
44    core::ptr::copy_nonoverlapping(src, dst, l);
45    core::ptr::write_bytes(dst.add(l), 0, n - l);
46    dst
47}
48
49#[no_mangle]
50pub unsafe extern "C" fn strcat(dst: *mut u8, src: *const u8) -> *mut u8 {
51    let dl = strlen(dst as *const c_char);
52    strcpy(dst.add(dl), src);
53    dst
54}
55
56#[no_mangle]
57pub unsafe extern "C" fn strncat(dst: *mut u8, src: *const u8, n: usize) -> *mut u8 {
58    let dl = strlen(dst as *const c_char);
59    let l = strnlen(src, n);
60    core::ptr::copy_nonoverlapping(src, dst.add(dl), l);
61    *dst.add(dl + l) = 0;
62    dst
63}
64
65#[no_mangle]
66pub unsafe extern "C" fn strlcpy(dst: *mut u8, src: *const u8, n: usize) -> usize {
67    let l = strlen(src as *const c_char);
68    if n > 0 {
69        let c = if l < n { l } else { n - 1 };
70        core::ptr::copy_nonoverlapping(src, dst, c);
71        *dst.add(c) = 0;
72    }
73    l
74}
75
76#[no_mangle]
77pub unsafe extern "C" fn strlcat(dst: *mut u8, src: *const u8, n: usize) -> usize {
78    let dl = strlen(dst as *const c_char);
79    let sl = strlen(src as *const c_char);
80    if dl >= n { return n + sl; }
81    let c = if sl < n - dl { sl } else { n - dl - 1 };
82    core::ptr::copy_nonoverlapping(src, dst.add(dl), c);
83    *dst.add(dl + c) = 0;
84    dl + sl
85}
86
87#[no_mangle]
88pub unsafe extern "C" fn bcopy(src: *const c_void, dst: *mut c_void, n: usize) {
89    core::ptr::copy(src as *const u8, dst as *mut u8, n);
90}
91
92#[no_mangle]
93pub unsafe extern "C" fn bzero(s: *mut c_void, n: usize) {
94    core::ptr::write_bytes(s as *mut u8, 0, n);
95}
96
97#[no_mangle]
98pub unsafe extern "C" fn strndup(s: *const c_char, n: usize) -> *mut c_char {
99    let mut len = 0;
100    while len < n && *s.add(len) != 0 {
101        len += 1;
102    }
103    let p = malloc(len + 1) as *mut c_char;
104    if p.is_null() {
105        return core::ptr::null_mut();
106    }
107    core::ptr::copy_nonoverlapping(s, p, len);
108    *p.add(len) = 0;
109    p
110}
111
112#[no_mangle]
113pub unsafe extern "C" fn strchr(s: *const u8, c: c_int) -> *mut u8 {
114    let p = strchrnul(s as *const c_char, c) as *const u8;
115    if *p != 0 || c as u8 == 0 {
116        p as *mut u8
117    } else {
118        null_mut()
119    }
120}
121
122#[no_mangle]
123pub unsafe extern "C" fn strrchr(s: *const u8, c: c_int) -> *mut u8 {
124    let len = strlen(s as *const c_char);
125    memrchr(s, c, len + 1)
126}
127
128#[no_mangle]
129pub unsafe extern "C" fn strnlen(s: *const u8, maxlen: usize) -> usize {
130    let p = memchr(s, 0, maxlen);
131    if p.is_null() {
132        maxlen
133    } else {
134        p as usize - s as usize
135    }
136}
137
138#[inline]
139unsafe fn byte_set(members: *const u8) -> [u64; 4] {
140    let mut set = [0u64; 4];
141    let mut p = members;
142    while *p != 0 {
143        let b = *p as usize;
144        set[b >> 6] |= 1u64 << (b & 63);
145        p = p.add(1);
146    }
147    set
148}
149
150#[inline]
151fn in_set(set: &[u64; 4], c: u8) -> bool {
152    set[c as usize >> 6] & (1u64 << (c as usize & 63)) != 0
153}
154
155#[no_mangle]
156pub unsafe extern "C" fn strcspn(s: *const u8, reject: *const u8) -> usize {
157    let set = byte_set(reject);
158    let mut i = 0usize;
159    loop {
160        let c = *s.add(i);
161        if c == 0 || in_set(&set, c) {
162            return i;
163        }
164        i += 1;
165    }
166}
167
168#[no_mangle]
169pub unsafe extern "C" fn strspn(s: *const u8, accept: *const u8) -> usize {
170    let set = byte_set(accept);
171    let mut i = 0usize;
172    loop {
173        let c = *s.add(i);
174        if c == 0 || !in_set(&set, c) {
175            return i;
176        }
177        i += 1;
178    }
179}
180
181#[no_mangle]
182pub unsafe extern "C" fn strpbrk(s: *const u8, accept: *const u8) -> *mut u8 {
183    let set = byte_set(accept);
184    let mut i = 0usize;
185    loop {
186        let c = *s.add(i);
187        if c == 0 {
188            return null_mut();
189        }
190        if in_set(&set, c) {
191            return s.add(i) as *mut u8;
192        }
193        i += 1;
194    }
195}
196
197// ------------------------------------------------------------
198// strstr / memmem (two-way, ported from musl)
199// ------------------------------------------------------------
200
201#[inline]
202fn bitset_contains(set: &[usize; 4], b: usize) -> bool {
203    set[b / 64] & (1usize << (b % 64)) != 0
204}
205
206unsafe fn twobyte_memmem(h0: *const u8, k0: usize, n: *const u8) -> *mut u8 {
207    let nw = ((*n as u16) << 8) | *n.add(1) as u16;
208    let mut hw = ((*h0 as u16) << 8) | *h0.add(1) as u16;
209    let mut h = h0.add(2);
210    let mut k = k0 - 2;
211    while k != 0 {
212        if hw == nw { return h.sub(2) as *mut u8; }
213        k -= 1;
214        hw = (hw << 8) | *h as u16;
215        h = h.add(1);
216    }
217    if hw == nw { h.sub(2) as *mut u8 } else { null_mut() }
218}
219
220unsafe fn threebyte_memmem(h0: *const u8, k0: usize, n: *const u8) -> *mut u8 {
221    let nw = ((*n as u32) << 24) | ((*n.add(1) as u32) << 16) | ((*n.add(2) as u32) << 8);
222    let mut hw = ((*h0 as u32) << 24) | ((*h0.add(1) as u32) << 16) | ((*h0.add(2) as u32) << 8);
223    let mut h = h0.add(3);
224    let mut k = k0 - 3;
225    while k != 0 {
226        if hw == nw { return h.sub(3) as *mut u8; }
227        k -= 1;
228        hw = (hw | *h as u32) << 8;
229        h = h.add(1);
230    }
231    if hw == nw { h.sub(3) as *mut u8 } else { null_mut() }
232}
233
234unsafe fn fourbyte_memmem(h0: *const u8, k0: usize, n: *const u8) -> *mut u8 {
235    let nw = ((*n as u32) << 24)
236        | ((*n.add(1) as u32) << 16)
237        | ((*n.add(2) as u32) << 8)
238        | *n.add(3) as u32;
239    let mut hw = ((*h0 as u32) << 24)
240        | ((*h0.add(1) as u32) << 16)
241        | ((*h0.add(2) as u32) << 8)
242        | *h0.add(3) as u32;
243    let mut h = h0.add(4);
244    let mut k = k0 - 4;
245    while k != 0 {
246        if hw == nw { return h.sub(4) as *mut u8; }
247        k -= 1;
248        hw = (hw << 8) | *h as u32;
249        h = h.add(1);
250    }
251    if hw == nw { h.sub(4) as *mut u8 } else { null_mut() }
252}
253
254unsafe fn maximal_suffix(n: *const u8, l: usize, invert: bool) -> (usize, usize) {
255    let mut ip = usize::MAX;
256    let mut jp = 0usize;
257    let mut k = 1usize;
258    let mut p = 1usize;
259    while jp.wrapping_add(k) < l {
260        let a = *n.add(ip.wrapping_add(k));
261        let b = *n.add(jp + k);
262        let advance = if invert { a < b } else { a > b };
263        if a == b {
264            if k == p {
265                jp += p;
266                k = 1;
267            } else {
268                k += 1;
269            }
270        } else if advance {
271            jp += k;
272            k = 1;
273            p = jp.wrapping_sub(ip);
274        } else {
275            ip = jp;
276            jp += 1;
277            k = 1;
278            p = 1;
279        }
280    }
281    (ip, p)
282}
283
284unsafe fn twoway_memmem(h0: *const u8, end: *const u8, n: *const u8, l: usize) -> *mut u8 {
285    let mut byteset = [0usize; 4];
286    let mut shift = [0usize; 256];
287    for i in 0..l {
288        let b = *n.add(i) as usize;
289        byteset[b / 64] |= 1usize << (b % 64);
290        shift[b] = i + 1;
291    }
292
293    let (ip1, p1) = maximal_suffix(n, l, false);
294    let (ip2, p2) = maximal_suffix(n, l, true);
295    let ms;
296    let mut p;
297    if ip2.wrapping_add(1) > ip1.wrapping_add(1) {
298        ms = ip2;
299        p = p2;
300    } else {
301        ms = ip1;
302        p = p1;
303    }
304
305    // Periodic needle?
306    let mem0;
307    if memcmp(
308        n as *const c_void,
309        n.add(p) as *const c_void,
310        ms.wrapping_add(1),
311    ) != 0
312    {
313        mem0 = 0;
314        let tail = l.wrapping_sub(ms).wrapping_sub(1);
315        p = (if ms > tail { ms } else { tail }) + 1;
316    } else {
317        mem0 = l - p;
318    }
319    let mut mem = 0usize;
320    let mut h = h0;
321    loop {
322        if (end as usize - h as usize) < l {
323            return null_mut();
324        }
325        let last = *h.add(l - 1) as usize;
326        if bitset_contains(&byteset, last) {
327            let mut k = l - shift[last];
328            if k != 0 {
329                if k < mem {
330                    k = mem;
331                }
332                h = h.add(k);
333                mem = 0;
334                continue;
335            }
336        } else {
337            h = h.add(l);
338            mem = 0;
339            continue;
340        }
341        let mut k = if ms.wrapping_add(1) > mem {
342            ms.wrapping_add(1)
343        } else {
344            mem
345        };
346        while k < l && *n.add(k) == *h.add(k) {
347            k += 1;
348        }
349        if k < l {
350            h = h.add(k.wrapping_sub(ms));
351            mem = 0;
352            continue;
353        }
354        let mut k = ms.wrapping_add(1);
355        while k > mem && *n.add(k - 1) == *h.add(k - 1) {
356            k -= 1;
357        }
358        if k <= mem {
359            return h as *mut u8;
360        }
361        h = h.add(p);
362        mem = mem0;
363    }
364}
365
366// Same algorithm for a NUL-terminated haystack.  The end pointer starts at
367// the candidate and grows incrementally, so an early match does not force a
368// full strlen() of the haystack.
369unsafe fn twoway_strstr(h0: *const u8, n: *const u8) -> *mut u8 {
370    let mut byteset = [0usize; 4];
371    let mut shift = [0usize; 256];
372    let mut l = 0usize;
373    while *n.add(l) != 0 && *h0.add(l) != 0 {
374        let b = *n.add(l) as usize;
375        byteset[b / 64] |= 1usize << (b % 64);
376        shift[b] = l + 1;
377        l += 1;
378    }
379    if *n.add(l) != 0 {
380        return null_mut();
381    }
382
383    let (ip1, p1) = maximal_suffix(n, l, false);
384    let (ip2, p2) = maximal_suffix(n, l, true);
385    let ms;
386    let mut p;
387    if ip2.wrapping_add(1) > ip1.wrapping_add(1) {
388        ms = ip2;
389        p = p2;
390    } else {
391        ms = ip1;
392        p = p1;
393    }
394
395    let mem0;
396    if memcmp(
397        n as *const c_void,
398        n.add(p) as *const c_void,
399        ms.wrapping_add(1),
400    ) != 0
401    {
402        mem0 = 0;
403        let tail = l.wrapping_sub(ms).wrapping_sub(1);
404        p = (if ms > tail { ms } else { tail }) + 1;
405    } else {
406        mem0 = l - p;
407    }
408    let mut mem = 0usize;
409    let mut h = h0;
410    let mut z = h0;
411    loop {
412        if (z as usize - h as usize) < l {
413            // Never scan past the end of the current page: a NUL-terminated
414            // string does not promise bytes beyond its terminator.
415            let room = 4096 - (z as usize & 4095);
416            let grow = if (l | 63) < room { l | 63 } else { room };
417            let z2 = memchr(z, 0, grow);
418            if z2.is_null() {
419                // The page cap can make this grow shorter than l, so the
420                // z-h >= l invariant must be re-checked at the loop top.
421                z = z.add(grow);
422                continue;
423            } else {
424                z = z2 as *const u8;
425                if (z as usize - h as usize) < l {
426                    return null_mut();
427                }
428            }
429        }
430        let last = *h.add(l - 1) as usize;
431        if bitset_contains(&byteset, last) {
432            let mut k = l - shift[last];
433            if k != 0 {
434                if k < mem {
435                    k = mem;
436                }
437                h = h.add(k);
438                mem = 0;
439                continue;
440            }
441        } else {
442            h = h.add(l);
443            mem = 0;
444            continue;
445        }
446        let mut k = if ms.wrapping_add(1) > mem {
447            ms.wrapping_add(1)
448        } else {
449            mem
450        };
451        while k < l && *n.add(k) != 0 && *n.add(k) == *h.add(k) {
452            k += 1;
453        }
454        if *n.add(k) != 0 {
455            h = h.add(k.wrapping_sub(ms));
456            mem = 0;
457            continue;
458        }
459        let mut k = ms.wrapping_add(1);
460        while k > mem && *n.add(k - 1) == *h.add(k - 1) {
461            k -= 1;
462        }
463        if k <= mem {
464            return h as *mut u8;
465        }
466        h = h.add(p);
467        mem = mem0;
468    }
469}
470
471#[no_mangle]
472pub unsafe extern "C" fn memmem(
473    haystack: *const c_void, haystacklen: usize,
474    needle: *const c_void, needlelen: usize,
475) -> *mut c_void {
476    if needlelen == 0 { return haystack as *mut c_void; }
477    if haystacklen < needlelen { return null_mut(); }
478    let h = haystack as *const u8;
479    let n = needle as *const u8;
480    let first = memchr(h, *n as c_int, haystacklen);
481    if first.is_null() {
482        return null_mut();
483    }
484    if needlelen == 1 {
485        return first as *mut c_void;
486    }
487    let off = first as usize - h as usize;
488    let h = h.add(off);
489    let k = haystacklen - off;
490    if k < needlelen {
491        return null_mut();
492    }
493    match needlelen {
494        2 => twobyte_memmem(h, k, n) as *mut c_void,
495        3 => threebyte_memmem(h, k, n) as *mut c_void,
496        4 => fourbyte_memmem(h, k, n) as *mut c_void,
497        _ => twoway_memmem(h, h.add(k), n, needlelen) as *mut c_void,
498    }
499}
500
501#[no_mangle]
502pub unsafe extern "C" fn strstr(haystack: *const u8, needle: *const u8) -> *mut u8 {
503    if *needle == 0 {
504        return haystack as *mut u8;
505    }
506    let h = strchrnul(haystack as *const c_char, *needle as c_int) as *const u8;
507    if *h == 0 {
508        return null_mut();
509    }
510    if *needle.add(1) == 0 {
511        return h as *mut u8;
512    }
513    twoway_strstr(h, needle)
514}
515
516#[no_mangle]
517pub unsafe extern "C" fn strtok(s: *mut u8, delim: *const u8) -> *mut u8 {
518    static mut STATE: *mut u8 = core::ptr::null_mut();
519    let mut p = if s.is_null() { STATE } else { s };
520    if p.is_null() {
521        return core::ptr::null_mut();
522    }
523    loop {
524        let c = *p;
525        if c == 0 {
526            STATE = core::ptr::null_mut();
527            return core::ptr::null_mut();
528        }
529        let mut is_delim = false;
530        let mut i = 0;
531        loop {
532            let d = *delim.add(i);
533            if d == 0 {
534                break;
535            }
536            if c == d {
537                is_delim = true;
538                break;
539            }
540            i += 1;
541        }
542        if !is_delim {
543            break;
544        }
545        p = p.add(1);
546    }
547    let start = p;
548    loop {
549        let c = *p;
550        if c == 0 {
551            STATE = core::ptr::null_mut();
552            return start;
553        }
554        let mut is_delim = false;
555        let mut i = 0;
556        loop {
557            let d = *delim.add(i);
558            if d == 0 {
559                break;
560            }
561            if c == d {
562                is_delim = true;
563                break;
564            }
565            i += 1;
566        }
567        if is_delim {
568            *p = 0;
569            STATE = p.add(1);
570            return start;
571        }
572        p = p.add(1);
573    }
574}
575