mod.rs 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. //! strings implementation for Redox, following http://pubs.opengroup.org/onlinepubs/7908799/xsh/strings.h.html
  2. use core::ptr;
  3. use crate::header::{ctype, string};
  4. use crate::platform::types::*;
  5. #[no_mangle]
  6. pub unsafe extern "C" fn bcmp(first: *const c_void, second: *const c_void, n: size_t) -> c_int {
  7. string::memcmp(first, second, n)
  8. }
  9. #[no_mangle]
  10. pub unsafe extern "C" fn bcopy(src: *const c_void, dst: *mut c_void, n: size_t) {
  11. ptr::copy(src as *const u8, dst as *mut u8, n);
  12. }
  13. #[no_mangle]
  14. pub unsafe extern "C" fn bzero(dst: *mut c_void, n: size_t) {
  15. ptr::write_bytes(dst as *mut u8, 0, n);
  16. }
  17. #[no_mangle]
  18. pub extern "C" fn ffs(i: c_int) -> c_int {
  19. if i == 0 {
  20. return 0;
  21. }
  22. 1 + i.trailing_zeros() as c_int
  23. }
  24. #[no_mangle]
  25. pub unsafe extern "C" fn index(s: *const c_char, c: c_int) -> *mut c_char {
  26. string::strchr(s, c)
  27. }
  28. #[no_mangle]
  29. pub unsafe extern "C" fn rindex(s: *const c_char, c: c_int) -> *mut c_char {
  30. string::strrchr(s, c)
  31. }
  32. #[no_mangle]
  33. pub unsafe extern "C" fn strcasecmp(first: *const c_char, second: *const c_char) -> c_int {
  34. strncasecmp(first, second, size_t::max_value())
  35. }
  36. #[no_mangle]
  37. pub unsafe extern "C" fn strncasecmp(
  38. mut first: *const c_char,
  39. mut second: *const c_char,
  40. mut n: size_t,
  41. ) -> c_int {
  42. while n > 0 && (*first != 0 || *second != 0) {
  43. let cmp = ctype::tolower(*first as c_int) - ctype::tolower(*second as c_int);
  44. if cmp != 0 {
  45. return cmp;
  46. }
  47. first = first.offset(1);
  48. second = second.offset(1);
  49. n -= 1;
  50. }
  51. 0
  52. }