mod.rs 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. //! ctype implementation for Redox, following http://pubs.opengroup.org/onlinepubs/7908799/xsh/ctype.h.html
  2. use platform::types::*;
  3. #[no_mangle]
  4. pub extern "C" fn isalnum(c: c_int) -> c_int {
  5. (isdigit(c) != 0 || isalpha(c) != 0) as c_int
  6. }
  7. #[no_mangle]
  8. pub extern "C" fn isalpha(c: c_int) -> c_int {
  9. (islower(c) != 0 || isupper(c) != 0) as c_int
  10. }
  11. #[no_mangle]
  12. pub extern "C" fn isascii(c: c_int) -> c_int {
  13. ((c & !0x7f) == 0) as c_int
  14. }
  15. #[no_mangle]
  16. pub extern "C" fn isblank(c: c_int) -> c_int {
  17. (c == ' ' as c_int || c == '\t' as c_int) as c_int
  18. }
  19. #[no_mangle]
  20. pub extern "C" fn iscntrl(c: c_int) -> c_int {
  21. ((c >= 0x00 && c <= 0x1f) || c == 0x7f) as c_int
  22. }
  23. #[no_mangle]
  24. pub extern "C" fn isdigit(c: c_int) -> c_int {
  25. (c >= b'0' as c_int && c <= b'9' as c_int) as c_int
  26. }
  27. #[no_mangle]
  28. pub extern "C" fn isgraph(c: c_int) -> c_int {
  29. (c >= 0x21 && c <= 0x7e) as c_int
  30. }
  31. #[no_mangle]
  32. pub extern "C" fn islower(c: c_int) -> c_int {
  33. (c >= b'a' as c_int && c <= b'z' as c_int) as c_int
  34. }
  35. #[no_mangle]
  36. pub extern "C" fn isprint(c: c_int) -> c_int {
  37. (c >= 0x20 && c < 0x7f) as c_int
  38. }
  39. #[no_mangle]
  40. pub extern "C" fn ispunct(c: c_int) -> c_int {
  41. ((c >= b'!' as c_int && c <= b'/' as c_int)
  42. || (c >= b':' as c_int && c <= b'@' as c_int)
  43. || (c >= b'[' as c_int && c <= b'`' as c_int)
  44. || (c >= b'{' as c_int && c <= b'~' as c_int)) as c_int
  45. }
  46. #[no_mangle]
  47. pub extern "C" fn isspace(c: c_int) -> c_int {
  48. (c == ' ' as c_int
  49. || c == '\t' as c_int
  50. || c == '\n' as c_int
  51. || c == '\r' as c_int
  52. || c == 0x0b
  53. || c == 0x0c) as c_int
  54. }
  55. #[no_mangle]
  56. pub extern "C" fn isupper(c: c_int) -> c_int {
  57. (c >= b'A' as c_int && c <= b'Z' as c_int) as c_int
  58. }
  59. #[no_mangle]
  60. pub extern "C" fn isxdigit(c: c_int) -> c_int {
  61. (isdigit(c) != 0 || (c | 32 >= b'a' as c_int && c | 32 <= 'f' as c_int)) as c_int
  62. }
  63. #[no_mangle]
  64. /// The comment in musl:
  65. /// "nonsense function that should NEVER be used!"
  66. pub extern "C" fn toascii(c: c_int) -> c_int {
  67. c & 0x7f
  68. }
  69. #[no_mangle]
  70. pub extern "C" fn tolower(c: c_int) -> c_int {
  71. if isupper(c) != 0 {
  72. c | 0x20
  73. } else {
  74. c
  75. }
  76. }
  77. #[no_mangle]
  78. pub extern "C" fn toupper(c: c_int) -> c_int {
  79. if islower(c) != 0 {
  80. c & !0x20
  81. } else {
  82. c
  83. }
  84. }