Mirror of CollapseOS
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

89 lines
1.6KB

  1. ; Sets Z is A is ' ' or '\t' (whitespace)
  2. isWS:
  3. cp ' '
  4. ret z
  5. cp 0x09
  6. ret
  7. ; Copy string from (HL) in (DE), that is, copy bytes until a null char is
  8. ; encountered. The null char is also copied.
  9. ; HL and DE point to the char right after the null char.
  10. strcpyM:
  11. ld a, (hl)
  12. ld (de), a
  13. inc hl
  14. inc de
  15. or a
  16. jr nz, strcpyM
  17. ret
  18. ; Like strcpyM, but preserve HL and DE
  19. strcpy:
  20. push hl
  21. push de
  22. call strcpyM
  23. pop de
  24. pop hl
  25. ret
  26. ; Compares strings pointed to by HL and DE until one of them hits its null char.
  27. ; If equal, Z is set. If not equal, Z is reset.
  28. strcmp:
  29. push hl
  30. push de
  31. .loop:
  32. ld a, (de)
  33. cp (hl)
  34. jr nz, .end ; not equal? break early. NZ is carried out
  35. ; to the called
  36. or a ; If our chars are null, stop the cmp
  37. jr z, .end ; The positive result will be carried to the
  38. ; caller
  39. inc hl
  40. inc de
  41. jr .loop
  42. .end:
  43. pop de
  44. pop hl
  45. ; Because we don't call anything else than CP that modify the Z flag,
  46. ; our Z value will be that of the last cp (reset if we broke the loop
  47. ; early, set otherwise)
  48. ret
  49. ; Given a string at (HL), move HL until it points to the end of that string.
  50. strskip:
  51. push af
  52. xor a ; look for null char
  53. .loop:
  54. cp (hl)
  55. jp z, .found
  56. inc hl
  57. jr .loop
  58. .found:
  59. pop af
  60. ret
  61. ; Returns length of string at (HL) in A.
  62. ; Doesn't include null termination.
  63. strlen:
  64. push bc
  65. push hl
  66. ld bc, 0
  67. xor a ; look for null char
  68. .loop:
  69. cpi
  70. jp z, .found
  71. jr .loop
  72. .found:
  73. ; How many char do we have? the (NEG BC)-1, which started at 0 and
  74. ; decreased at each CPI call. In this routine, we stay in the 8-bit
  75. ; realm, so C only.
  76. ld a, c
  77. neg
  78. dec a
  79. pop hl
  80. pop bc
  81. ret