1
0

strings.l 2.5 KB

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