string.l 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. (
  2. (def uchr (fn s (uget s 0)))
  3. (def substr_ (fn str beg sz (do
  4. (def res (alloc-str (* 2 sz)))
  5. (def ii 0)
  6. (while (lt ii sz) (do
  7. (uput res ii (uget str (+ beg ii)))
  8. (def ii (+ ii 1))
  9. ))
  10. res
  11. )))
  12. (def ufind-prev (fn buf rune pos (do
  13. (while (and (gt pos 0) (not (= rune (uget buf pos))))
  14. (def pos (- pos 1)))
  15. pos
  16. )))
  17. (def ufind-next (fn buf rune pos (do
  18. (def sz (usize buf))
  19. (while (and (lt pos sz) (not (= rune (uget buf pos))))
  20. (def pos (+ pos 1)))
  21. pos
  22. )))
  23. (def ufind-prev-ws (fn buf pos (do
  24. (while (and (gt pos 0) (not (or (= 13 (uget buf pos)) (or (= 10 (uget buf pos)) (= 32 (uget buf pos))))))
  25. (def pos (- pos 1)))
  26. (if (= pos 0) 0 pos)
  27. )))
  28. (def ufind-next-ws (fn buf pos (do
  29. (while (and (lt pos (usize buf)) (not (or (= 13 (uget buf pos)) (or (= 10 (uget buf pos)) (= 32 (uget buf pos))))))
  30. (def pos (+ pos 1)))
  31. pos
  32. )))
  33. (def ucopy (fn buf from to num (do
  34. (if (lt from to)
  35. (do
  36. (def i (- num 1))
  37. (while (gt (def i (- i 1)) -1) (do
  38. (def c (uget buf (+ from i)))
  39. (uput buf (+ to i) c)
  40. )))
  41. (do
  42. (def i 0)
  43. (while (lt (def i (+ i 1)) num) (do
  44. (def c (uget buf (+ from i)))
  45. (uput buf (+ to i) c)
  46. )))
  47. )
  48. )))
  49. (def uremove (fn buf pos (do
  50. (ucopy buf (- pos 1) (- pos 2) (- (usize buf) (- pos 1)))
  51. (uput buf (- (usize buf) 1) 0)
  52. 0
  53. )))
  54. (def split (fn str sepstr (do
  55. (def sep (uget sepstr 0))
  56. (def result (quote ()))
  57. (def sz (usize str))
  58. (def i (- sz 1))
  59. (def last-i (+ i 1))
  60. (while (gt i -2) (do
  61. (if (or (= (uget str i) sep) (= i -1)) (do
  62. (def partsize (- (- last-i i) 1))
  63. (if (gt partsize 0)
  64. (def result (cons (substr str (+ i 1) partsize) result)) 0)
  65. (def last-i i)
  66. ) 0)
  67. (def i (- i 1))
  68. ))
  69. result
  70. )))
  71. (def join (fn lst jstr (do
  72. (def joined "")
  73. (while (car lst) (do
  74. (def tmp (alloc-str 128))
  75. (write tmp (car lst))
  76. (def tmp (substr tmp 1 (- (usize tmp) 2)))
  77. (def joined (concat (concat joined jstr) tmp))
  78. (print joined)
  79. (def lst (cdr lst))
  80. ))
  81. joined
  82. )))
  83. (def str= (fn str-a str-b (do
  84. (def si 0)
  85. (def equal 1)
  86. (while (and equal (lt si (size str-a))) (do
  87. (if (not (= (get str-a si) (get str-b si))) (def equal 0))
  88. (def si (+ si 1))
  89. ))
  90. equal
  91. )))
  92. )