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.

114 lines
1.9KB

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