strings-test.scm 82 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879
  1. ;; -*- coding: utf-8 -*-
  2. ;;; Copyright (C) Per Bothner (2017).
  3. ;;; Copyright (C) William D Clinger (2016).
  4. ;;;
  5. ;;; Permission is hereby granted, free of charge, to any person
  6. ;;; obtaining a copy of this software and associated documentation
  7. ;;; files (the "Software"), to deal in the Software without
  8. ;;; restriction, including without limitation the rights to use,
  9. ;;; copy, modify, merge, publish, distribute, sublicense, and/or
  10. ;;; sell copies of the Software, and to permit persons to whom the
  11. ;;; Software is furnished to do so, subject to the following
  12. ;;; conditions:
  13. ;;;
  14. ;;; The above copyright notice and this permission notice shall be
  15. ;;; included in all copies or substantial portions of the Software.
  16. ;;;
  17. ;;; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  18. ;;; EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  19. ;;; OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  20. ;;; NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  21. ;;; HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  22. ;;; WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  23. ;;; FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  24. ;;; OTHER DEALINGS IN THE SOFTWARE.
  25. #|
  26. (import (scheme base)
  27. (scheme write)
  28. (scheme char)
  29. (srfi 140))
  30. |#
  31. (test-begin "strings")
  32. #|
  33. ;;; Help functions for testing.
  34. (define (as-string . args)
  35. (string-concatenate (map (lambda (x)
  36. (cond ((string? x) x)
  37. ((char? x) (string x))
  38. (else
  39. (error "as-string: illegal argument" x))))
  40. args)))
  41. |#
  42. ;;; Unicode is a strong motivation for immutable strings, so we ought
  43. ;;; to use at least some non-ASCII strings for testing.
  44. ;;; Some systems would blow up if this file were to contain non-ASCII
  45. ;;; characters, however, so we have to be careful here.
  46. ;;;
  47. ;;; FIXME: need more tests with really high code points
  48. (cond-expand ((or sagittarius
  49. chibi
  50. kawa
  51. full-unicode-strings)
  52. (define ABC
  53. (list->string (map integer->char
  54. '(#x3b1 #x3b2 #x3b3))))
  55. (define ABCDEF
  56. (list->string (map integer->char
  57. '(#x0c0 #x062 #x0c7 #x064 #x0c9 #x066))))
  58. (define DEFABC
  59. (list->string (map integer->char
  60. '(#x064 #x0c9 #x066 #x0c0 #x062 #x0c7))))
  61. (define eszett (integer->char #xDF))
  62. (define fuss (string #\F #\u eszett))
  63. (define chaos0
  64. (list->string (map integer->char
  65. '(#x39E #x391 #x39F #x3A3))))
  66. (define chaos1
  67. (list->string (map integer->char
  68. '(#x3BE #x3B1 #x3BF #x3C2))))
  69. (define chaos2
  70. (list->string (map integer->char
  71. '(#x3BE #x3B1 #x3BF #x3C3))))
  72. (define beyondBMP
  73. (list->string (map integer->char
  74. '(#x61 #xc0 #x3bf
  75. #x1d441 #x1d113 #x1d110 #x7a)))))
  76. (else
  77. (define ABC "abc")
  78. (define ABCDEF "ABCdef")
  79. (define DEFABC "defabc")))
  80. ;;; Predicates
  81. (test-assert (string? (string)))
  82. (test-assert (not (string? #\a)))
  83. (test-assert (string-null? (string)))
  84. (test-assert (not (string-null? ABC)))
  85. (define (check-istring str)
  86. (list (istring? str) (string-length str)))
  87. (test-equal '(#t 0) (check-istring ""))
  88. (test-equal '(#t 4) (check-istring "abcd"))
  89. (test-equal '(#t 4) (check-istring (string #\A #\b #\c #\d)))
  90. (test-equal '(#t 3) (check-istring (substring (make-string 4 #\X) 1 4)))
  91. (test-equal '(#f 4) (check-istring (make-string 4 #\X)))
  92. (test-equal '(#f 4) (check-istring (string-copy (make-string 4 #\X))))
  93. (test-equal '(#f 3) (check-istring (string-copy (make-string 4 #\X) 1 4)))
  94. (test-equal '(#t 3) (check-istring (vector->string #(#\x #\y #\z))))
  95. (test-equal '(#t 3) (check-istring (vector->string #(#\x #\y #\z))))
  96. (test-equal '(#t 3) (check-istring (list->string '(#\x #\y #\z))))
  97. (test-equal '(#t 3) (check-istring (reverse-list->string '(#\x #\y #\z))))
  98. (test-equal '(#t 3) (check-istring (utf8->string (string->utf8 "abc"))))
  99. (test-equal '(#t 3) (check-istring (utf16->string (string->utf16 "abc"))))
  100. (test-equal '(#t 3) (check-istring (utf16be->string (string->utf16be "abc"))))
  101. (test-equal '(#t 3) (check-istring (utf16le->string (string->utf16le "abc"))))
  102. (test-equal '(#t 2) (check-istring (string-take "abcd" 2)))
  103. (test-equal '(#t 2) (check-istring (string-drop "abcd" 2)))
  104. (test-equal '(#t 2) (check-istring (string-take-right "abcd" 2)))
  105. (test-equal '(#t 2) (check-istring (string-drop-right "abcd" 2)))
  106. (test-equal '(#t 5) (check-istring (string-pad "abcd" 5)))
  107. (test-equal '(#t 3) (check-istring (string-pad-right "abcd" 3)))
  108. (test-equal '(#t 2) (check-istring (string-trim " A ")))
  109. (test-equal '(#t 3) (check-istring (string-trim-right " A ")))
  110. (test-equal '(#t 1) (check-istring (string-trim-both " A ")))
  111. (test-equal '(#t 3) (check-istring (string-replace "AB" "X" 1 1)))
  112. (test-equal '(#t 3) (check-istring (string-upcase (make-string 3 #\X))))
  113. (test-equal '(#t 3) (check-istring (string-downcase (make-string 3 #\x))))
  114. (test-equal '(#t 3) (check-istring (string-foldcase (make-string 3 #\x))))
  115. (test-equal '(#t 3) (check-istring (string-titlecase (make-string 3 #\X))))
  116. (test-equal '(#t 6) (check-istring (string-append "abcd" "XY")))
  117. (test-equal '(#t 6) (check-istring (string-concatenate (list "abcd" "XY"))))
  118. (test-equal '(#t 6) (check-istring
  119. (string-concatenate-reverse (list "abcd" "XY"))))
  120. (test-equal '(#t 7) (check-istring (string-join (list "abc" "xyz"))))
  121. (test-equal '(#t 3) (check-istring (string-map char-upcase "abc")))
  122. (test-equal '(#t 6) (check-istring (string-repeat "ab" 3)))
  123. (test-equal '(#t 14) (check-istring (xsubstring "abcdef" -4 10)))
  124. (test-equal '(#t 3) (check-istring (cadr (string-split "ab cef" " "))))
  125. (test-expect-fail 1)
  126. (test-equal '(#t 5) (check-istring (symbol->string 'Hello)))
  127. (test-equal #t (string-every (lambda (c) (if (char? c) c #f))
  128. (string)))
  129. (test-equal #\c (string-every (lambda (c) (if (char? c) c #f))
  130. "abc"))
  131. (test-equal #f (string-every (lambda (c) (if (char>? c #\b) c #f))
  132. "abc"))
  133. (test-equal #\c (string-every (lambda (c) (if (char>? c #\b) c #f))
  134. "abc" 2))
  135. (test-equal #t (string-every (lambda (c) (if (char>? c #\b) c #f))
  136. "abc" 1 1))
  137. (test-equal #f (string-any (lambda (c) (if (char? c) c #f))
  138. (string)))
  139. (test-equal #\a (string-any (lambda (c) (if (char? c) c #f))
  140. "abc"))
  141. (test-equal #\c (string-any (lambda (c) (if (char>? c #\b) c #f))
  142. "abc"))
  143. (test-equal #\c (string-any (lambda (c) (if (char>? c #\b) c #f))
  144. "abc" 2))
  145. (test-equal #f (string-any (lambda (c) (if (char>? c #\b) c #f))
  146. "abc" 0 2))
  147. (test-equal #t (string-every (lambda (c) (if (char? c) c #f)) ""))
  148. (test-equal #\c (string-every (lambda (c) (if (char? c) c #f)) "abc"))
  149. (test-equal #f (string-every (lambda (c) (if (char>? c #\b) c #f)) "abc"))
  150. (test-equal #\c (string-every (lambda (c) (if (char>? c #\b) c #f)) "abc" 2))
  151. (test-equal #t (string-every (lambda (c) (if (char>? c #\b) c #f)) "abc" 1 1))
  152. (test-equal #f (string-any (lambda (c) (if (char? c) c #f)) ""))
  153. (test-equal #\a (string-any (lambda (c) (if (char? c) c #f)) "abc"))
  154. (test-equal #\c (string-any (lambda (c) (if (char>? c #\b) c #f)) "abc"))
  155. (test-equal #\c (string-any (lambda (c) (if (char>? c #\b) c #f)) "abc" 2))
  156. (test-equal #f (string-any (lambda (c) (if (char>? c #\b) c #f)) "abc" 0 2))
  157. ;;; Constructors
  158. (test-equal ""
  159. (string-tabulate (lambda (i)
  160. (integer->char (+ i (char->integer #\a))))
  161. 0))
  162. (let ((r (string-tabulate (lambda (i)
  163. (integer->char (+ i (char->integer #\a))))
  164. 3)))
  165. (test-equal '(#t 3) (check-istring r))
  166. (test-equal "abc" r))
  167. (let* ((p (open-input-string "abc"))
  168. (r (string-unfold eof-object?
  169. values
  170. (lambda (x) (read-char p))
  171. (read-char p))))
  172. (test-equal '(#t 3) (check-istring r))
  173. (test-equal "abc" r))
  174. (test-equal "" (string-unfold null? car cdr '()))
  175. (test-equal "abc"
  176. (string-unfold null? car cdr (string->list "abc")))
  177. (test-equal "def"
  178. (string-unfold null? car cdr '() "def"))
  179. (test-equal "defabcG"
  180. (string-unfold null?
  181. car
  182. cdr
  183. (string->list "abc")
  184. "def"
  185. (lambda (x) (if (null? x) (string #\G) ""))))
  186. (test-equal "" (string-unfold-right null? car cdr '()))
  187. (test-equal "cba"
  188. (string-unfold-right null? car cdr (string->list "abc")))
  189. (test-equal "def"
  190. (string-unfold-right null? car cdr '() "def"))
  191. (test-equal '(#t 3)
  192. (check-istring (string-unfold-right null? car cdr '() "def")))
  193. (test-equal "Gcbadef"
  194. (string-unfold-right null?
  195. car
  196. cdr
  197. (string->list "abc")
  198. "def"
  199. (lambda (x) (if (null? x) (string #\G) ""))))
  200. (test-equal "def"
  201. (string-unfold null? car cdr '() "def"))
  202. (test-equal "defabcG"
  203. (string-unfold null?
  204. car
  205. cdr
  206. (string->list "abc")
  207. "def"
  208. (lambda (x) (if (null? x) "G" ""))))
  209. (test-equal "dabcG"
  210. (string-unfold null?
  211. car
  212. cdr
  213. (string->list "abc")
  214. #\d
  215. (lambda (x) (if (null? x) "G" ""))))
  216. (test-equal (string-append "%="
  217. (make-string 200 #\*)
  218. "A B C D E F G H I J K L M "
  219. "N O P Q R S T U V W X Y Z "
  220. (make-string (* 200 (- (char->integer #\a)
  221. (char->integer #\Z)
  222. 1))
  223. #\*)
  224. "abcdefghijklmnopqrstuvwxyz"
  225. " ")
  226. (string-unfold (lambda (n) (char>? (integer->char n) #\z))
  227. (lambda (n)
  228. (let ((c (integer->char n)))
  229. (cond ((char<=? #\a c #\z) c)
  230. ((char<=? #\A c #\Z) (string c #\space))
  231. (else (make-string 200 #\*)))))
  232. (lambda (n) (+ n 1))
  233. (char->integer #\@)
  234. "%="
  235. (lambda (n) #\space)))
  236. (test-equal "def"
  237. (string-unfold-right null? car cdr '() "def"))
  238. (test-equal "Gcbadef"
  239. (string-unfold-right null?
  240. car
  241. cdr
  242. (string->list "abc")
  243. "def"
  244. (lambda (x) (if (null? x) "G" ""))))
  245. (test-equal "Gcbad"
  246. (string-unfold-right null?
  247. car
  248. cdr
  249. (string->list "abc")
  250. #\d
  251. (lambda (x) (if (null? x) "G" ""))))
  252. (test-equal (string-append " "
  253. (list->string
  254. (reverse
  255. (string->list "abcdefghijklmnopqrstuvwxyz")))
  256. (make-string (* 200 (- (char->integer #\a)
  257. (char->integer #\Z)
  258. 1))
  259. #\*)
  260. "Z Y X W V U T S R Q P O N "
  261. "M L K J I H G F E D C B A "
  262. (make-string 200 #\*)
  263. "%=")
  264. (string-unfold-right
  265. (lambda (n) (char>? (integer->char n) #\z))
  266. (lambda (n)
  267. (let ((c (integer->char n)))
  268. (cond ((char<=? #\a c #\z) c)
  269. ((char<=? #\A c #\Z) (string c #\space))
  270. (else (make-string 200 #\*)))))
  271. (lambda (n) (+ n 1))
  272. (char->integer #\@)
  273. "%="
  274. (lambda (n) #\space)))
  275. (test-equal " The English alphabet: abcdefghijklmnopqrstuvwxyz "
  276. (string-unfold-right (lambda (n) (< n (char->integer #\A)))
  277. (lambda (n)
  278. (char-downcase (integer->char n)))
  279. (lambda (n) (- n 1))
  280. (char->integer #\Z)
  281. #\space
  282. (lambda (n) " The English alphabet: ")))
  283. ;;; Conversion
  284. (let ((txt (string #\s #\t #\r)))
  285. (test-assert (and (string? txt) (string=? txt "str"))))
  286. (test-equal "" (string))
  287. (test-equal "" (substring (string) 0 0))
  288. (test-equal "abc" (string #\a #\b #\c))
  289. (test-equal "" (substring (string #\a #\b #\c) 3 3))
  290. (test-equal "bc" (substring (string #\a #\b #\c) 1 3))
  291. ;(test-equal "" (substring "" 0))
  292. (test-equal "" (substring "" 0 0))
  293. (test-equal "" (substring "abc" 3 3))
  294. (test-equal "bc" (substring "abc" 1 3))
  295. (test-equal '#() (string->vector (string)))
  296. ;(test-equal '#() (string->vector (string) 0))
  297. (test-equal '#() (string->vector (string) 0 0))
  298. (test-equal '#(#\a #\b #\c) (string->vector (string #\a #\b #\c)))
  299. (test-equal '#() (string->vector (string #\a #\b #\c) 3))
  300. (test-equal '#(#\b #\c) (string->vector (string #\a #\b #\c) 1 3))
  301. (test-equal '#() (string->vector ""))
  302. (test-equal '#() (string->vector "" 0))
  303. (test-equal '#() (string->vector "" 0 0))
  304. (test-equal '#(#\a #\b #\c) (string->vector "abc"))
  305. (test-equal '#() (string->vector "abc" 3))
  306. (test-equal '#(#\b #\c) (string->vector "abc" 1 3))
  307. (test-equal '() (string->list (string)))
  308. (test-equal '() (string->list (string) 0))
  309. (test-equal '() (string->list (string) 0 0))
  310. (test-equal '(#\a #\b #\c) (string->list (string #\a #\b #\c)))
  311. (test-equal '() (string->list (string #\a #\b #\c) 3))
  312. (test-equal '(#\b #\c) (string->list (string #\a #\b #\c) 1 3))
  313. (test-equal '() (string->list ""))
  314. (test-equal '() (string->list "" 0))
  315. (test-equal '() (string->list "" 0 0))
  316. (test-equal '(#\a #\b #\c) (string->list "abc"))
  317. (test-equal '() (string->list "abc" 3))
  318. (test-equal '(#\b #\c) (string->list "abc" 1 3))
  319. (test-equal "" "")
  320. (test-equal "" (substring "" 0 0))
  321. (test-equal "bc" (substring "abc" 1 3))
  322. (test-equal "" (substring "abc" 3 3))
  323. (test-equal "b" (substring "abc" 1 2))
  324. (test-equal "bc" (substring "abc" 1 3))
  325. (test-equal "" (vector->string '#()))
  326. (test-equal "" (vector->string '#() 0))
  327. (test-equal "" (vector->string '#() 0 0))
  328. (test-equal "abc" (vector->string '#(#\a #\b #\c)))
  329. (test-equal "bc" (vector->string '#(#\a #\b #\c) 1))
  330. (test-equal "" (vector->string '#(#\a #\b #\c) 3))
  331. (test-equal "b" (vector->string '#(#\a #\b #\c) 1 2))
  332. (test-equal "bc" (vector->string '#(#\a #\b #\c) 1 3))
  333. (test-equal "" (list->string '()))
  334. #| FIXME TODO
  335. (test-equal "" (list->string '() 0))
  336. (test-equal "" (list->string '() 0 0))
  337. (test-equal "abc" (list->string '(#\a #\b #\c)))
  338. (test-equal "bc" (list->string '(#\a #\b #\c) 1))
  339. (test-equal "" (list->string '(#\a #\b #\c) 3))
  340. (test-equal "b" (list->string '(#\a #\b #\c) 1 2))
  341. (test-equal "bc" (list->string '(#\a #\b #\c) 1 3))
  342. |#
  343. (test-equal "" (reverse-list->string '()))
  344. (test-equal "cba" (reverse-list->string '(#\a #\b #\c)))
  345. (test-equal '#u8(97 98 99)
  346. (string->utf8 "abc"))
  347. (test-equal '#u8(97 98 99 121 121 121 122 122 122)
  348. (string->utf8 "xxxabcyyyzzz" 3))
  349. (test-equal '#u8(97 98 99)
  350. (string->utf8 "xxxabcyyyzzz" 3 6))
  351. (test-equal (cond-expand (big-endian '#u8(254 255 0 97 0 98 0 99))
  352. (else '#u8(255 254 97 0 98 0 99 0)))
  353. (string->utf16 "abc"))
  354. (test-equal (cond-expand (big-endian '#u8(254 255 0 97 0 98 0 99 0 121 0 121 0 121 0 122 0 122 0 122))
  355. (else '#u8(255 254 97 0 98 0 99 0 121 0 121 0 121 0 122 0 122 0 122 0)))
  356. (string->utf16 "xxxabcyyyzzz" 3))
  357. (test-equal (cond-expand (big-endian '#u8(254 255 0 97 0 98 0 99))
  358. (else '#u8(255 254 97 0 98 0 99 0)))
  359. (string->utf16 "xxxabcyyyzzz" 3 6))
  360. (test-equal '#u8(0 97 0 98 0 99)
  361. (string->utf16be "abc"))
  362. (test-equal '#u8(0 97 0 98 0 99 0 121 0 121 0 121 0 122 0 122 0 122)
  363. (string->utf16be "xxxabcyyyzzz" 3))
  364. (test-equal '#u8(0 97 0 98 0 99)
  365. (string->utf16be "xxxabcyyyzzz" 3 6))
  366. (test-equal '#u8(97 0 98 0 99 0)
  367. (string->utf16le "abc"))
  368. (test-equal '#u8(97 0 98 0 99 0 121 0 121 0 121 0 122 0 122 0 122 0)
  369. (string->utf16le "xxxabcyyyzzz" 3))
  370. (test-equal '#u8(97 0 98 0 99 0)
  371. (string->utf16le "xxxabcyyyzzz" 3 6))
  372. (test-equal "abc"
  373. (utf8->string '#u8(97 98 99)))
  374. (test-equal "abcyyyzzz"
  375. (utf8->string '#u8(0 1 2 97 98 99 121 121 121 122 122 122) 3))
  376. (test-equal "abc"
  377. (utf8->string '#u8(41 42 43 97 98 99 100 101 102) 3 6))
  378. (test-equal "abc"
  379. (utf16->string '#u8(254 255 0 97 0 98 0 99)))
  380. (test-equal "abc"
  381. (utf16->string '#u8(255 254 97 0 98 0 99 0)))
  382. (test-equal "abc"
  383. (utf16->string (string->utf16 "abc") 2))
  384. (test-equal "bcdef"
  385. (utf16->string (string->utf16 "abcdef") 4))
  386. (test-equal "bcd"
  387. (utf16->string (string->utf16 "abcdef") 4 10))
  388. (test-equal "abc"
  389. (utf16be->string '#u8(0 97 0 98 0 99)))
  390. (test-equal "bc"
  391. (utf16be->string (string->utf16be "abc") 2))
  392. (test-equal "bcd"
  393. (utf16be->string (string->utf16be "abcdef") 2 8))
  394. (test-equal "abc"
  395. (utf16le->string '#u8(97 0 98 0 99 0)))
  396. (test-equal "bc"
  397. (utf16le->string (string->utf16le "abc") 2))
  398. (test-equal "bcd"
  399. (utf16le->string (string->utf16le "abcdef") 2 8))
  400. (cond-expand
  401. ((or sagittarius
  402. chibi
  403. kawa
  404. full-unicode-strings)
  405. (test-equal '#u8(97 195 128 206 191
  406. 240 157 145 129 240 157 132 147 240 157 132 144 122)
  407. (string->utf8 beyondBMP))
  408. (let ((bv (string->utf16 beyondBMP)))
  409. (test-assert
  410. (or (equal? bv
  411. '#u8(254 255 0 97 0 192 3 191
  412. 216 53 220 65 216 52 221 19 216 52 221 16 0 122))
  413. (equal? bv
  414. '#u8(255 254 97 0 192 0 191 3
  415. 53 216 65 220 52 216 19 221 52 216 16 221 122 0)))))
  416. (test-equal
  417. '#u8(0 97 0 192 3 191 216 53 220 65 216 52 221 19 216 52 221 16 0 122)
  418. (string->utf16be beyondBMP))
  419. (test-equal
  420. '#u8(97 0 192 0 191 3 53 216 65 220 52 216 19 221 52 216 16 221 122 0)
  421. (string->utf16le beyondBMP))
  422. (test-equal
  423. beyondBMP
  424. (utf8->string
  425. '#u8(97 195 128 206 191
  426. 240 157 145 129 240 157 132 147 240 157 132 144 122)))
  427. (test-equal beyondBMP (utf16->string (string->utf16 beyondBMP)))
  428. (test-equal beyondBMP
  429. (utf16->string (string->utf16 beyondBMP) 2))
  430. (test-equal beyondBMP (utf16be->string (string->utf16be beyondBMP)))
  431. (test-equal beyondBMP (utf16le->string (string->utf16le beyondBMP)))
  432. (test-equal (string-append (string (integer->char #xfeff)) "abc")
  433. (utf16be->string '#u8(254 255 0 97 0 98 0 99)))
  434. (test-equal (string-append (string (integer->char #xfeff)) "abc")
  435. (utf16le->string '#u8(255 254 97 0 98 0 99 0)))
  436. )
  437. (else))
  438. ;;; Selection
  439. (test-equal 0 (string-length (string)))
  440. (test-equal 6 (string-length ABCDEF))
  441. (test-equal 1234 (string-length (make-string 1234 (string-ref ABC 0))))
  442. (test-equal #\a (string-ref (string #\a #\b #\c) 0))
  443. (test-equal #\c (string-ref (string #\a #\b #\c) 2))
  444. (test-equal 0 (string-length (string)))
  445. (test-equal 6 (string-length ABCDEF))
  446. (test-equal 1234 (string-length (make-string 1234 (string-ref ABC 0))))
  447. (test-equal #\a (string-ref (string #\a #\b #\c) 0))
  448. (test-equal #\c (string-ref (string #\a #\b #\c) 2))
  449. (test-equal ""
  450. (substring (string) 0 0))
  451. (test-equal ""
  452. (substring "abcdef" 0 0))
  453. (test-equal "" (substring "abcdef" 4 4))
  454. (test-equal "" (substring "abcdef" 6 6))
  455. (test-equal "abcd" (substring "abcdef" 0 4))
  456. (test-equal "cde" (substring "abcdef" 2 5))
  457. (test-equal "cdef" (substring "abcdef" 2 6))
  458. (test-equal "abcdef" (substring "abcdef" 0 6))
  459. (test-equal "" (substring (string) 0 0))
  460. (test-equal "" (substring "abcdef" 0 0))
  461. (test-equal "" (substring "abcdef" 4 4))
  462. (test-equal "" (substring "abcdef" 6 6))
  463. (test-equal "abcd" (substring "abcdef" 0 4))
  464. (test-equal "cde" (substring "abcdef" 2 5))
  465. (test-equal "cdef" (substring "abcdef" 2 6))
  466. (test-equal "abcdef" (substring "abcdef" 0 6))
  467. (test-equal "" (substring "" 0 0))
  468. (test-equal "" (substring "abcdef" 0 0))
  469. (test-equal "" (substring "abcdef" 4 4))
  470. (test-equal "" (substring "abcdef" 6 6))
  471. (test-equal "abcd" (substring "abcdef" 0 4))
  472. (test-equal "cde" (substring "abcdef" 2 5))
  473. (test-equal "cdef" (substring "abcdef" 2 6))
  474. (test-equal "abcdef" (substring "abcdef" 0 6))
  475. (test-equal "" (string-copy (string)))
  476. (let* ((txt "abcdef")
  477. (copy (string-copy txt)))
  478. (test-equal "abcdef" copy)
  479. (test-assert (not (eqv? txt copy))))
  480. (test-equal "" (string-copy ""))
  481. (test-equal "abcdef" (string-copy "abcdef"))
  482. (test-equal "" (string-copy (string) 0))
  483. (test-equal "abcdef" (string-copy "abcdef" 0))
  484. (test-equal "ef" (string-copy "abcdef" 4))
  485. (test-equal "" (string-copy "abcdef" 6))
  486. (test-equal "" (string-copy "" 0))
  487. (test-equal "abcdef" (string-copy "abcdef" 0))
  488. (test-equal "ef" (string-copy "abcdef" 4))
  489. (test-equal "" (string-copy "abcdef" 6))
  490. (test-equal "" (string-copy (string) 0 0))
  491. (test-equal "" (string-copy "abcdef" 0 0))
  492. (test-equal "" (string-copy "abcdef" 4 4))
  493. (test-equal "" (string-copy "abcdef" 6 6))
  494. (test-equal "abcd" (string-copy "abcdef" 0 4))
  495. (test-equal "cde" (string-copy "abcdef" 2 5))
  496. (test-equal "cdef" (string-copy "abcdef" 2 6))
  497. (test-equal "abcdef" (string-copy "abcdef" 0 6))
  498. (test-equal ""
  499. (string-copy "" 0 0))
  500. (test-equal ""
  501. (string-copy "abcdef" 0 0))
  502. (test-equal ""
  503. (string-copy "abcdef" 4 4))
  504. (test-equal ""
  505. (string-copy "abcdef" 6 6))
  506. (test-equal "abcd"
  507. (string-copy "abcdef" 0 4))
  508. (test-equal "cde"
  509. (string-copy "abcdef" 2 5))
  510. (test-equal "cdef"
  511. (string-copy "abcdef" 2 6))
  512. (test-equal "abcdef"
  513. (string-copy "abcdef" 0 6))
  514. (test-equal "" (string-take (string) 0))
  515. (test-equal "" (string-take "abcdef" 0))
  516. (test-equal "ab" (string-take "abcdef" 2))
  517. (test-equal "" (string-drop "" 0))
  518. (test-equal "abcdef" (string-drop "abcdef" 0))
  519. (test-equal "cdef" (string-drop "abcdef" 2))
  520. (test-equal "" (string-take-right (string) 0))
  521. (test-equal "" (string-take-right "abcdef" 0))
  522. (test-equal "ef" (string-take-right "abcdef" 2))
  523. (test-equal "" (string-drop-right (string) 0))
  524. (test-equal "abcdef"
  525. (string-drop-right "abcdef" 0))
  526. (test-equal "abcd"
  527. (string-drop-right "abcdef" 2))
  528. (test-equal "" (string-take "" 0))
  529. (test-equal "" (string-take "abcdef" 0))
  530. (test-equal "ab" (string-take "abcdef" 2))
  531. (test-equal "" (string-drop "" 0))
  532. (test-equal "abcdef" (string-drop "abcdef" 0))
  533. (test-equal "cdef" (string-drop "abcdef" 2))
  534. (test-equal "" (string-take-right "" 0))
  535. (test-equal "" (string-take-right "abcdef" 0))
  536. (test-equal "ef" (string-take-right "abcdef" 2))
  537. (test-equal "" (string-drop-right "" 0))
  538. (test-equal "abcdef" (string-drop-right "abcdef" 0))
  539. (test-equal "abcd" (string-drop-right "abcdef" 2))
  540. (test-equal ""
  541. (string-pad "" 0))
  542. (test-equal " "
  543. (string-pad "" 5))
  544. (test-equal " 325"
  545. (string-pad "325" 5))
  546. (test-equal "71325"
  547. (string-pad "71325" 5))
  548. (test-equal "71325"
  549. (string-pad "8871325" 5))
  550. (test-equal ""
  551. (string-pad "" 0 #\*))
  552. (test-equal "*****"
  553. (string-pad "" 5 #\*))
  554. (test-equal "**325"
  555. (string-pad "325" 5 #\*))
  556. (test-equal "71325"
  557. (string-pad "71325" 5 #\*))
  558. (test-equal "71325"
  559. (string-pad "8871325" 5 #\*))
  560. (test-equal ""
  561. (string-pad "" 0 #\* 0))
  562. (test-equal "*****"
  563. (string-pad "" 5 #\* 0))
  564. (test-equal "**325"
  565. (string-pad "325" 5 #\* 0))
  566. (test-equal "71325"
  567. (string-pad "71325" 5 #\* 0))
  568. (test-equal "71325"
  569. (string-pad "8871325" 5 #\* 0))
  570. (test-equal "***25"
  571. (string-pad "325" 5 #\* 1))
  572. (test-equal "*1325"
  573. (string-pad "71325" 5 #\* 1))
  574. (test-equal "71325"
  575. (string-pad "8871325" 5 #\* 1))
  576. (test-equal ""
  577. (string-pad "" 0 #\* 0 0))
  578. (test-equal "*****"
  579. (string-pad "" 5 #\* 0 0))
  580. (test-equal "**325"
  581. (string-pad "325" 5 #\* 0 3))
  582. (test-equal "**713"
  583. (string-pad "71325" 5 #\* 0 3))
  584. (test-equal "**887"
  585. (string-pad "8871325" 5 #\* 0 3))
  586. (test-equal "***25"
  587. (string-pad "325" 5 #\* 1 3))
  588. (test-equal "**132"
  589. (string-pad "71325" 5 #\* 1 4))
  590. (test-equal "*8713"
  591. (string-pad "8871325" 5 #\* 1 5))
  592. (test-equal ""
  593. (string-pad-right "" 0))
  594. (test-equal " "
  595. (string-pad-right "" 5))
  596. (test-equal "325 "
  597. (string-pad-right "325" 5))
  598. (test-equal "71325"
  599. (string-pad-right "71325" 5))
  600. (test-equal "88713"
  601. (string-pad-right "8871325" 5))
  602. (test-equal ""
  603. (string-pad-right "" 0 #\*))
  604. (test-equal "*****"
  605. (string-pad-right "" 5 #\*))
  606. (test-equal "325**"
  607. (string-pad-right "325" 5 #\*))
  608. (test-equal "71325"
  609. (string-pad-right "71325" 5 #\*))
  610. (test-equal "88713"
  611. (string-pad-right "8871325" 5 #\*))
  612. (test-equal ""
  613. (string-pad-right "" 0 #\* 0))
  614. (test-equal "*****"
  615. (string-pad-right "" 5 #\* 0))
  616. (test-equal "325**"
  617. (string-pad-right "325" 5 #\* 0))
  618. (test-equal "71325"
  619. (string-pad-right "71325" 5 #\* 0))
  620. (test-equal "88713"
  621. (string-pad-right "8871325" 5 #\* 0))
  622. (test-equal "25***"
  623. (string-pad-right "325" 5 #\* 1))
  624. (test-equal "1325*"
  625. (string-pad-right "71325" 5 #\* 1))
  626. (test-equal "87132"
  627. (string-pad-right "8871325" 5 #\* 1))
  628. (test-equal ""
  629. (string-pad-right "" 0 #\* 0 0))
  630. (test-equal "*****"
  631. (string-pad-right "" 5 #\* 0 0))
  632. (test-equal "325**"
  633. (string-pad-right "325" 5 #\* 0 3))
  634. (test-equal "713**"
  635. (string-pad-right "71325" 5 #\* 0 3))
  636. (test-equal "887**"
  637. (string-pad-right "8871325" 5 #\* 0 3))
  638. (test-equal "25***"
  639. (string-pad-right "325" 5 #\* 1 3))
  640. (test-equal "132**"
  641. (string-pad-right "71325" 5 #\* 1 4))
  642. (test-equal "8713*"
  643. (string-pad-right "8871325" 5 #\* 1 5))
  644. (test-equal "" (string-pad "" 0))
  645. (test-equal " " (string-pad "" 5))
  646. (test-equal " 325" (string-pad "325" 5))
  647. (test-equal "71325" (string-pad "71325" 5))
  648. (test-equal "71325" (string-pad "8871325" 5))
  649. (test-equal "" (string-pad "" 0 #\*))
  650. (test-equal "*****" (string-pad "" 5 #\*))
  651. (test-equal "**325" (string-pad "325" 5 #\*))
  652. (test-equal "71325" (string-pad "71325" 5 #\*))
  653. (test-equal "71325" (string-pad "8871325" 5 #\*))
  654. (test-equal "" (string-pad "" 0 #\* 0))
  655. (test-equal "*****" (string-pad "" 5 #\* 0))
  656. (test-equal "**325" (string-pad "325" 5 #\* 0))
  657. (test-equal "71325" (string-pad "71325" 5 #\* 0))
  658. (test-equal "71325" (string-pad "8871325" 5 #\* 0))
  659. (test-equal "***25" (string-pad "325" 5 #\* 1))
  660. (test-equal "*1325" (string-pad "71325" 5 #\* 1))
  661. (test-equal "71325" (string-pad "8871325" 5 #\* 1))
  662. (test-equal "" (string-pad "" 0 #\* 0 0))
  663. (test-equal "*****" (string-pad "" 5 #\* 0 0))
  664. (test-equal "**325" (string-pad "325" 5 #\* 0 3))
  665. (test-equal "**713" (string-pad "71325" 5 #\* 0 3))
  666. (test-equal "**887" (string-pad "8871325" 5 #\* 0 3))
  667. (test-equal "***25" (string-pad "325" 5 #\* 1 3))
  668. (test-equal "**132" (string-pad "71325" 5 #\* 1 4))
  669. (test-equal "*8713" (string-pad "8871325" 5 #\* 1 5))
  670. (test-equal "" (string-pad-right "" 0))
  671. (test-equal " " (string-pad-right "" 5))
  672. (test-equal "325 " (string-pad-right "325" 5))
  673. (test-equal "71325" (string-pad-right "71325" 5))
  674. (test-equal "88713" (string-pad-right "8871325" 5))
  675. (test-equal "" (string-pad-right "" 0 #\*))
  676. (test-equal "*****" (string-pad-right "" 5 #\*))
  677. (test-equal "325**" (string-pad-right "325" 5 #\*))
  678. (test-equal "71325" (string-pad-right "71325" 5 #\*))
  679. (test-equal "88713" (string-pad-right "8871325" 5 #\*))
  680. (test-equal "" (string-pad-right "" 0 #\* 0))
  681. (test-equal "*****" (string-pad-right "" 5 #\* 0))
  682. (test-equal "325**" (string-pad-right "325" 5 #\* 0))
  683. (test-equal "71325" (string-pad-right "71325" 5 #\* 0))
  684. (test-equal "88713" (string-pad-right "8871325" 5 #\* 0))
  685. (test-equal "25***" (string-pad-right "325" 5 #\* 1))
  686. (test-equal "1325*" (string-pad-right "71325" 5 #\* 1))
  687. (test-equal "87132" (string-pad-right "8871325" 5 #\* 1))
  688. (test-equal "" (string-pad-right "" 0 #\* 0 0))
  689. (test-equal "*****" (string-pad-right "" 5 #\* 0 0))
  690. (test-equal "325**" (string-pad-right "325" 5 #\* 0 3))
  691. (test-equal "713**" (string-pad-right "71325" 5 #\* 0 3))
  692. (test-equal "887**" (string-pad-right "8871325" 5 #\* 0 3))
  693. (test-equal "25***" (string-pad-right "325" 5 #\* 1 3))
  694. (test-equal "132**" (string-pad-right "71325" 5 #\* 1 4))
  695. (test-equal "8713*" (string-pad-right "8871325" 5 #\* 1 5))
  696. (test-equal ""
  697. (string-trim ""))
  698. (test-equal "a b c "
  699. (string-trim " a b c "))
  700. (test-equal ""
  701. (string-trim "" char-whitespace?))
  702. (test-equal "a b c "
  703. (string-trim " a b c " char-whitespace?))
  704. (test-equal ""
  705. (string-trim " a b c " char?))
  706. (test-equal ""
  707. (string-trim "" char-whitespace? 0))
  708. (test-equal "a b c "
  709. (string-trim " a b c " char-whitespace? 0))
  710. (test-equal ""
  711. (string-trim " a b c " char? 0))
  712. (test-equal "b c "
  713. (string-trim " a b c " char-whitespace? 3))
  714. (test-equal ""
  715. (string-trim " a b c " char? 3))
  716. (test-equal ""
  717. (string-trim " a b c " char? 0 11))
  718. (test-equal "b c "
  719. (string-trim " a b c "
  720. char-whitespace? 3 11))
  721. (test-equal "" (string-trim " a b c " char? 3 11))
  722. (test-equal ""
  723. (string-trim " a b c " char? 0 8))
  724. (test-equal "b "
  725. (string-trim " a b c "
  726. char-whitespace? 3 8))
  727. (test-equal ""
  728. (string-trim " a b c " char? 3 8))
  729. (test-equal ""
  730. (string-trim-right ""))
  731. (test-equal " a b c" (string-trim-right " a b c "))
  732. (test-equal "" (string-trim-right "" char-whitespace?))
  733. (test-equal " a b c"
  734. (string-trim-right " a b c " char-whitespace?))
  735. (test-equal ""
  736. (string-trim-right " a b c " char?))
  737. (test-equal ""
  738. (string-trim-right "" char-whitespace? 0))
  739. (test-equal " a b c"
  740. (string-trim-right " a b c "
  741. char-whitespace? 0))
  742. (test-equal ""
  743. (string-trim-right " a b c " char? 0))
  744. (test-equal " b c"
  745. (string-trim-right " a b c "
  746. char-whitespace? 3))
  747. (test-equal ""
  748. (string-trim-right " a b c " char? 3))
  749. (test-equal ""
  750. (string-trim-right " a b c " char? 0 11))
  751. (test-equal " b c"
  752. (string-trim-right " a b c "
  753. char-whitespace? 3 11))
  754. (test-equal ""
  755. (string-trim-right " a b c " char? 3 11))
  756. (test-equal ""
  757. (string-trim-right " a b c " char? 0 8))
  758. (test-equal " b"
  759. (string-trim-right " a b c "
  760. char-whitespace? 3 8))
  761. (test-equal ""
  762. (string-trim-right " a b c " char? 3 8))
  763. (test-equal ""
  764. (string-trim-both ""))
  765. (test-equal "a b c"
  766. (string-trim-both " a b c "))
  767. (test-equal ""
  768. (string-trim-both "" char-whitespace?))
  769. (test-equal "a b c"
  770. (string-trim-both " a b c "
  771. char-whitespace?))
  772. (test-equal ""
  773. (string-trim-both " a b c " char?))
  774. (test-equal ""
  775. (string-trim-both "" char-whitespace? 0))
  776. (test-equal "a b c"
  777. (string-trim-both " a b c "
  778. char-whitespace? 0))
  779. (test-equal ""
  780. (string-trim-both " a b c " char? 0))
  781. (test-equal "b c"
  782. (string-trim-both " a b c "
  783. char-whitespace? 3))
  784. (test-equal ""
  785. (string-trim-both " a b c " char? 3))
  786. (test-equal ""
  787. (string-trim-both " a b c " char? 0 11))
  788. (test-equal "b c"
  789. (string-trim-both " a b c "
  790. char-whitespace? 3 11))
  791. (test-equal ""
  792. (string-trim-both " a b c " char? 3 11))
  793. (test-equal ""
  794. (string-trim-both " a b c " char? 0 8))
  795. (test-equal "b"
  796. (string-trim-both " a b c "
  797. char-whitespace? 3 8))
  798. (test-equal ""
  799. (string-trim-both " a b c " char? 3 8))
  800. (test-equal ""
  801. (string-trim ""))
  802. (test-equal "a b c "
  803. (string-trim " a b c "))
  804. (test-equal ""
  805. (string-trim "" char-whitespace?))
  806. (test-equal "a b c "
  807. (string-trim " a b c " char-whitespace?))
  808. (test-equal ""
  809. (string-trim " a b c " char?))
  810. (test-equal ""
  811. (string-trim "" char-whitespace? 0))
  812. (test-equal "a b c "
  813. (string-trim " a b c " char-whitespace? 0))
  814. (test-equal ""
  815. (string-trim " a b c " char? 0))
  816. (test-equal "b c "
  817. (string-trim " a b c " char-whitespace? 3))
  818. (test-equal ""
  819. (string-trim " a b c " char? 3))
  820. (test-equal ""
  821. (string-trim " a b c " char? 0 11))
  822. (test-equal "b c "
  823. (string-trim " a b c " char-whitespace? 3 11))
  824. (test-equal ""
  825. (string-trim " a b c " char? 3 11))
  826. (test-equal ""
  827. (string-trim " a b c " char? 0 8))
  828. (test-equal "b "
  829. (string-trim " a b c " char-whitespace? 3 8))
  830. (test-equal ""
  831. (string-trim " a b c " char? 3 8))
  832. (test-equal ""
  833. (string-trim-right ""))
  834. (test-equal " a b c"
  835. (string-trim-right " a b c "))
  836. (test-equal ""
  837. (string-trim-right "" char-whitespace?))
  838. (test-equal " a b c"
  839. (string-trim-right " a b c " char-whitespace?))
  840. (test-equal ""
  841. (string-trim-right " a b c " char?))
  842. (test-equal ""
  843. (string-trim-right "" char-whitespace? 0))
  844. (test-equal " a b c"
  845. (string-trim-right " a b c " char-whitespace? 0))
  846. (test-equal ""
  847. (string-trim-right " a b c " char? 0))
  848. (test-equal " b c"
  849. (string-trim-right " a b c " char-whitespace? 3))
  850. (test-equal ""
  851. (string-trim-right " a b c " char? 3))
  852. (test-equal ""
  853. (string-trim-right " a b c " char? 0 11))
  854. (test-equal " b c"
  855. (string-trim-right " a b c " char-whitespace? 3 11))
  856. (test-equal ""
  857. (string-trim-right " a b c " char? 3 11))
  858. (test-equal ""
  859. (string-trim-right " a b c " char? 0 8))
  860. (test-equal " b"
  861. (string-trim-right " a b c " char-whitespace? 3 8))
  862. (test-equal ""
  863. (string-trim-right " a b c " char? 3 8))
  864. (test-equal ""
  865. (string-trim-both ""))
  866. (test-equal "a b c"
  867. (string-trim-both " a b c "))
  868. (test-equal ""
  869. (string-trim-both "" char-whitespace?))
  870. (test-equal "a b c"
  871. (string-trim-both " a b c " char-whitespace?))
  872. (test-equal ""
  873. (string-trim-both " a b c " char?))
  874. (test-equal ""
  875. (string-trim-both "" char-whitespace? 0))
  876. (test-equal "a b c"
  877. (string-trim-both " a b c " char-whitespace? 0))
  878. (test-equal ""
  879. (string-trim-both " a b c " char? 0))
  880. (test-equal "b c"
  881. (string-trim-both " a b c " char-whitespace? 3))
  882. (test-equal ""
  883. (string-trim-both " a b c " char? 3))
  884. (test-equal ""
  885. (string-trim-both " a b c " char? 0 11))
  886. (test-equal "b c"
  887. (string-trim-both " a b c " char-whitespace? 3 11))
  888. (test-equal ""
  889. (string-trim-both " a b c " char? 3 11))
  890. (test-equal ""
  891. (string-trim-both " a b c " char? 0 8))
  892. (test-equal "b"
  893. (string-trim-both " a b c " char-whitespace? 3 8))
  894. (test-equal ""
  895. (string-trim-both " a b c " char? 3 8))
  896. ;;; Replacement
  897. (test-equal "It's lots of fun to code it up in Scheme."
  898. (string-replace "It's easy to code it up in Scheme."
  899. "lots of fun"
  900. 5 9))
  901. (test-equal "The miserable perl programmer endured daily ridicule."
  902. (string-replace "The TCL programmer endured daily ridicule."
  903. "another miserable perl drone"
  904. 4 7 8 22))
  905. (test-equal "It's really easy to code it up in Scheme."
  906. (string-replace "It's easy to code it up in Scheme."
  907. "really "
  908. 5 5))
  909. (test-equal "Runs in O(1) time." ; for strings (using sample implementations)
  910. (string-replace "Runs in O(n) time." (string #\1) 10 11))
  911. ;;; Comparison
  912. ;;;
  913. ;;; The comparison tests aren't perfectly black-box because the
  914. ;;; specification of these comparison procedures allows them to
  915. ;;; use an ordering other than the usual lexicographic ordering.
  916. ;;; The sample implementations use lexicographic ordering, however,
  917. ;;; and a test program that discourages implementations from using
  918. ;;; orderings that differ from the usual on such simple cases is
  919. ;;; probably doing a public service.
  920. (test-assert (string=? "Strasse" "Strasse"))
  921. (test-assert (string=? "Strasse" "Strasse" "Strasse"))
  922. (test-equal #f (string<? "z" "z"))
  923. (test-assert (string<? "z" "zz"))
  924. (test-equal #f (string<? "z" "Z"))
  925. (test-assert (string<=? "z" "zz"))
  926. (test-equal #f (string<=? "z" "Z"))
  927. (test-assert (string<=? "z" "z"))
  928. (test-equal #f (string<? "z" "z"))
  929. (test-equal #f (string>? "z" "zz"))
  930. (test-equal #t (string>? "z" "Z"))
  931. (test-equal #f (string>=? "z" "zz"))
  932. (test-equal #t (string>=? "z" "Z"))
  933. (test-assert (string>=? "z" "z"))
  934. (let* ((w "a")
  935. (x "abc")
  936. (y "def")
  937. (z (string #\a #\b #\c)))
  938. (test-equal (string=? x y z) #f)
  939. (test-equal (string=? x x z) #t)
  940. (test-equal (string=? w x y) #f)
  941. (test-equal (string=? y x w) #f)
  942. (test-equal (string<? x y z) #f)
  943. (test-equal (string<? x x z) #f)
  944. (test-equal (string<? w x y) #t)
  945. (test-equal (string<? y x w) #f)
  946. (test-equal (string>? x y z) #f)
  947. (test-equal (string>? x x z) #f)
  948. (test-equal (string>? w x y) #f)
  949. (test-equal (string>? y x w) #t)
  950. (test-equal (string<=? x y z) #f)
  951. (test-equal (string<=? x x z) #t)
  952. (test-equal (string<=? w x y) #t)
  953. (test-equal (string<=? y x w) #f)
  954. (test-equal (string>=? x y z) #f)
  955. (test-equal (string>=? x x z) #t)
  956. (test-equal (string>=? w x y) #f)
  957. (test-equal (string>=? y x w) #t)
  958. (test-equal (string=? x x) #t)
  959. (test-equal (string=? w x) #f)
  960. (test-equal (string=? y x) #f)
  961. (test-equal (string<? x x) #f)
  962. (test-equal (string<? w x) #t)
  963. (test-equal (string<? y x) #f)
  964. (test-equal (string>? x x) #f)
  965. (test-equal (string>? w x) #f)
  966. (test-equal (string>? y x) #t)
  967. (test-equal (string<=? x x) #t)
  968. (test-equal (string<=? w x) #t)
  969. (test-equal (string<=? y x) #f)
  970. (test-equal (string>=? x x) #t)
  971. (test-equal (string>=? w x) #f)
  972. (test-equal (string>=? y x) #t)
  973. )
  974. (test-equal #t (string-ci<? "a" "Z"))
  975. (test-equal #t (string-ci<? "A" "z"))
  976. (test-equal #f (string-ci<? "Z" "a"))
  977. (test-equal #f (string-ci<? "z" "A"))
  978. (test-equal #f (string-ci<? "z" "Z"))
  979. (test-equal #f (string-ci<? "Z" "z"))
  980. (test-equal #f (string-ci>? "a" "Z"))
  981. (test-equal #f (string-ci>? "A" "z"))
  982. (test-equal #t (string-ci>? "Z" "a"))
  983. (test-equal #t (string-ci>? "z" "A"))
  984. (test-equal #f (string-ci>? "z" "Z"))
  985. (test-equal #f (string-ci>? "Z" "z"))
  986. (test-equal #t (string-ci=? "z" "Z"))
  987. (test-equal #f (string-ci=? "z" "a"))
  988. (test-equal #t (string-ci<=? "a" "Z"))
  989. (test-equal #t (string-ci<=? "A" "z"))
  990. (test-equal #f (string-ci<=? "Z" "a"))
  991. (test-equal #f (string-ci<=? "z" "A"))
  992. (test-equal #t (string-ci<=? "z" "Z"))
  993. (test-equal #t (string-ci<=? "Z" "z"))
  994. (test-equal #f (string-ci>=? "a" "Z"))
  995. (test-equal #f (string-ci>=? "A" "z"))
  996. (test-equal #t (string-ci>=? "Z" "a"))
  997. (test-equal #t (string-ci>=? "z" "A"))
  998. (test-equal #t (string-ci>=? "z" "Z"))
  999. (test-equal #t (string-ci>=? "Z" "z"))
  1000. ;;; The full-unicode feature doesn't imply full Unicode in strings,
  1001. ;;; so these tests might fail even in a conforming implementation.
  1002. ;;; Implementations that support full Unicode strings often have
  1003. ;;; this feature, however, even though it isn't listed in the R7RS.
  1004. (cond-expand
  1005. (full-unicode
  1006. (test-equal #f (string=? ABCDEF DEFABC))
  1007. (test-equal #f (string=? DEFABC ABCDEF))
  1008. (test-equal #t (string=? DEFABC DEFABC))
  1009. (test-equal #f (string<? ABCDEF DEFABC))
  1010. (test-equal #t (string<? DEFABC ABCDEF))
  1011. (test-equal #f (string<? DEFABC DEFABC))
  1012. (test-equal #t (string>? ABCDEF DEFABC))
  1013. (test-equal #f (string>? DEFABC ABCDEF))
  1014. (test-equal #f (string>? DEFABC DEFABC))
  1015. (test-equal #f (string<=? ABCDEF DEFABC))
  1016. (test-equal #t (string<=? DEFABC ABCDEF))
  1017. (test-equal #t (string<=? DEFABC DEFABC))
  1018. (test-equal #t (string>=? ABCDEF DEFABC))
  1019. (test-equal #f (string>=? DEFABC ABCDEF))
  1020. (test-equal #t (string>=? DEFABC DEFABC))
  1021. (test-equal #f (string=? "Fuss" fuss))
  1022. (test-equal #f (string=? "Fuss" "Fuss" fuss))
  1023. (test-equal #f (string=? "Fuss" fuss "Fuss"))
  1024. (test-equal #f (string=? fuss "Fuss" "Fuss"))
  1025. (test-equal #t (string<? "z" (string eszett)))
  1026. (test-equal #f (string<? (string eszett) "z"))
  1027. (test-equal #t (string<=? "z" (string eszett)))
  1028. (test-equal #f (string<=? (string eszett) "z"))
  1029. (test-equal #f (string>? "z" (string eszett)))
  1030. (test-equal #t (string>? (string eszett) "z"))
  1031. (test-equal #f (string>=? "z" (string eszett)))
  1032. (test-equal #t (string>=? (string eszett) "z"))
  1033. (test-assert (string-ci=? fuss "Fuss"))
  1034. (test-assert (string-ci=? fuss "FUSS"))
  1035. (test-assert (string-ci=? chaos0 chaos1 chaos2)))
  1036. (else))
  1037. ;;; Prefixes and suffixes
  1038. (test-equal 0 (string-prefix-length ABC ABCDEF))
  1039. (test-equal 0 (string-prefix-length ABCDEF ABC))
  1040. (test-equal 0 (string-prefix-length ABCDEF DEFABC))
  1041. (test-equal 6 (string-prefix-length DEFABC DEFABC))
  1042. (test-equal 0 (string-prefix-length "" ""))
  1043. (test-equal 0 (string-prefix-length "" "aabbccddee"))
  1044. (test-equal 0 (string-prefix-length "aisle" ""))
  1045. (test-equal 0 (string-prefix-length "" "aabbccddee"))
  1046. (test-equal 1 (string-prefix-length "aisle" "aabbccddee"))
  1047. (test-equal 0 (string-prefix-length "bail" "aabbccddee"))
  1048. (test-equal 4 (string-prefix-length "prefix" "preface"))
  1049. (test-equal 0 (string-prefix-length "" "" 0))
  1050. (test-equal 0 (string-prefix-length "" "aabbccddee" 0))
  1051. (test-equal 0 (string-prefix-length "aisle" "" 0))
  1052. (test-equal 1 (string-prefix-length "aisle" "aabbccddee" 0))
  1053. (test-equal 0 (string-prefix-length "bail" "aabbccddee" 0))
  1054. (test-equal 4 (string-prefix-length "prefix" "preface" 0))
  1055. (test-equal 0 (string-prefix-length "aisle" "" 1))
  1056. (test-equal 0 (string-prefix-length "aisle" "aabbccddee" 1))
  1057. (test-equal 1 (string-prefix-length "bail" "aabbccddee" 1))
  1058. (test-equal 0 (string-prefix-length "prefix" "preface" 1))
  1059. (test-equal 0 (string-prefix-length "" "" 0 0))
  1060. (test-equal 0 (string-prefix-length "" "aabbccddee" 0 0))
  1061. (test-equal 0 (string-prefix-length "aisle" "" 0 4))
  1062. (test-equal 1 (string-prefix-length "aisle" "aabbccddee" 0 4))
  1063. (test-equal 0 (string-prefix-length "bail" "aabbccddee" 0 1))
  1064. (test-equal 0 (string-prefix-length "aisle" "" 1 4))
  1065. (test-equal 0 (string-prefix-length "aisle" "aabbccddee" 1 4))
  1066. (test-equal 1 (string-prefix-length "bail" "aabbccddee" 1 4))
  1067. (test-equal 0 (string-prefix-length "prefix" "preface" 1 5))
  1068. (test-equal 0 (string-prefix-length "" "" 0 0 0))
  1069. (test-equal 0 (string-prefix-length "" "aabbccddee" 0 0 0))
  1070. (test-equal 0 (string-prefix-length "aisle" "" 0 4 0))
  1071. (test-equal 0 (string-prefix-length "aisle" "aabbccddee" 0 4 2))
  1072. (test-equal 1 (string-prefix-length "bail" "aabbccddee" 0 1 2))
  1073. (test-equal 0 (string-prefix-length "prefix" "preface" 0 5 1))
  1074. (test-equal 0 (string-prefix-length "aisle" "" 1 4 0))
  1075. (test-equal 0 (string-prefix-length "aisle" "aabbccddee" 1 4 3))
  1076. (test-equal 0 (string-prefix-length "bail" "aabbccddee" 1 4 3))
  1077. (test-equal 3 (string-prefix-length "prefix" "preface" 1 5 1))
  1078. (test-equal 0 (string-prefix-length "" "" 0 0 0 0))
  1079. (test-equal 0 (string-prefix-length "" "aabbccddee" 0 0 0 0))
  1080. (test-equal 0 (string-prefix-length "aisle" "" 0 4 0 0))
  1081. (test-equal 0 (string-prefix-length "aisle" "aabbccddee" 0 4 2 10))
  1082. (test-equal 1 (string-prefix-length "bail" "aabbccddee" 0 1 2 10))
  1083. (test-equal 0 (string-prefix-length "prefix" "preface" 0 5 1 6))
  1084. (test-equal 0 (string-prefix-length "aisle" "" 1 4 0 0))
  1085. (test-equal 0 (string-prefix-length "aisle" "aabbccddee" 1 4 3 3))
  1086. (test-equal 0 (string-prefix-length "bail" "aabbccddee" 1 4 3 6))
  1087. (test-equal 3 (string-prefix-length "prefix" "preface" 1 5 1 7))
  1088. (test-equal 0 (string-suffix-length ABC ABCDEF))
  1089. (test-equal 0 (string-suffix-length ABCDEF ABC))
  1090. (test-equal 0 (string-suffix-length ABCDEF DEFABC))
  1091. (test-equal 6 (string-suffix-length DEFABC DEFABC))
  1092. (test-equal 0 (string-suffix-length "" ""))
  1093. (test-equal 0 (string-suffix-length "" "aabbccddee"))
  1094. (test-equal 0 (string-suffix-length "aisle" ""))
  1095. (test-equal 0 (string-suffix-length "" "aabbccddee"))
  1096. (test-equal 1 (string-suffix-length "aisle" "aabbccddee"))
  1097. (test-equal 0 (string-suffix-length "bail" "aabbccddee"))
  1098. (test-equal 3 (string-suffix-length "place" "preface"))
  1099. (test-equal 0 (string-suffix-length "" "" 0))
  1100. (test-equal 0 (string-suffix-length "" "aabbccddee" 0))
  1101. (test-equal 0 (string-suffix-length "aisle" "" 0))
  1102. (test-equal 1 (string-suffix-length "aisle" "aabbccddee" 0))
  1103. (test-equal 0 (string-suffix-length "bail" "aabbccddee" 0))
  1104. (test-equal 3 (string-suffix-length "place" "preface" 0))
  1105. (test-equal 0 (string-suffix-length "aisle" "" 1))
  1106. (test-equal 1 (string-suffix-length "aisle" "aabbccddee" 1))
  1107. (test-equal 0 (string-suffix-length "bail" "aabbccddee" 1))
  1108. (test-equal 3 (string-suffix-length "place" "preface" 1))
  1109. (test-equal 0 (string-suffix-length "" "" 0 0))
  1110. (test-equal 0 (string-suffix-length "" "aabbccddee" 0 0))
  1111. (test-equal 0 (string-suffix-length "aisle" "" 0 4))
  1112. (test-equal 0 (string-suffix-length "aisle" "aabbccddee" 0 4))
  1113. (test-equal 0 (string-suffix-length "bail" "aabbccddee" 0 1))
  1114. (test-equal 0 (string-suffix-length "aisle" "" 1 4))
  1115. (test-equal 0 (string-suffix-length "aisle" "aabbccddee" 1 4))
  1116. (test-equal 1 (string-suffix-length "aisle" "aabbccddee" 1 5))
  1117. (test-equal 0 (string-suffix-length "bail" "aabbccddee" 1 4))
  1118. (test-equal 3 (string-suffix-length "place" "preface" 1 5))
  1119. (test-equal 0 (string-suffix-length "" "" 0 0 0))
  1120. (test-equal 0 (string-suffix-length "" "aabbccddee" 0 0 0))
  1121. (test-equal 0 (string-suffix-length "aisle" "" 0 4 0))
  1122. (test-equal 0 (string-suffix-length "aisle" "aabbccddee" 0 4 2))
  1123. (test-equal 0 (string-suffix-length "bail" "aabbccddee" 0 1 2))
  1124. (test-equal 3 (string-suffix-length "place" "preface" 0 5 1))
  1125. (test-equal 0 (string-suffix-length "aisle" "" 1 4 0))
  1126. (test-equal 0 (string-suffix-length "aisle" "aabbccddee" 1 4 3))
  1127. (test-equal 0 (string-suffix-length "bail" "aabbccddee" 1 4 3))
  1128. (test-equal 3 (string-suffix-length "place" "preface" 1 5 1))
  1129. (test-equal 0 (string-suffix-length "" "" 0 0 0 0))
  1130. (test-equal 0 (string-suffix-length "" "aabbccddee" 0 0 0 0))
  1131. (test-equal 0 (string-suffix-length "aisle" "" 0 4 0 0))
  1132. (test-equal 1 (string-suffix-length "aisle" "aabbccddee" 0 5 2 10))
  1133. (test-equal 1 (string-suffix-length "bail" "aabbccddee" 0 1 2 4))
  1134. (test-equal 0 (string-suffix-length "place" "preface" 0 5 1 6))
  1135. (test-equal 2 (string-suffix-length "place" "preface" 0 4 1 6))
  1136. (test-equal 0 (string-suffix-length "aisle" "" 1 4 0 0))
  1137. (test-equal 0 (string-suffix-length "aisle" "aabbccddee" 1 4 3 3))
  1138. (test-equal 0 (string-suffix-length "bail" "aabbccddee" 1 4 3 6))
  1139. (test-equal 3 (string-suffix-length "place" "preface" 1 5 1 7))
  1140. (test-equal #f (string-prefix? ABC ABCDEF))
  1141. (test-equal #f (string-prefix? ABCDEF ABC))
  1142. (test-equal #f (string-prefix? ABCDEF DEFABC))
  1143. (test-equal #t (string-prefix? DEFABC DEFABC))
  1144. (test-equal #t (string-prefix? "" ""))
  1145. (test-equal #t (string-prefix? "" "abc"))
  1146. (test-equal #t (string-prefix? "a" "abc"))
  1147. (test-equal #f (string-prefix? "c" "abc"))
  1148. (test-equal #t (string-prefix? "ab" "abc"))
  1149. (test-equal #f (string-prefix? "ac" "abc"))
  1150. (test-equal #t (string-prefix? "abc" "abc"))
  1151. (test-equal #f (string-suffix? ABC ABCDEF))
  1152. (test-equal #f (string-suffix? ABCDEF ABC))
  1153. (test-equal #f (string-suffix? ABCDEF DEFABC))
  1154. (test-equal #t (string-suffix? DEFABC DEFABC))
  1155. (test-equal #t (string-suffix? "" ""))
  1156. (test-equal #t (string-suffix? "" "abc"))
  1157. (test-equal #f (string-suffix? "a" "abc"))
  1158. (test-equal #t (string-suffix? "c" "abc"))
  1159. (test-equal #f (string-suffix? "ac" "abc"))
  1160. (test-equal #t (string-suffix? "bc" "abc"))
  1161. (test-equal #t (string-suffix? "abc" "abc"))
  1162. (test-equal #t (string-prefix? "" "" 0))
  1163. (test-equal #t (string-prefix? "" "abc" 0))
  1164. (test-equal #t (string-prefix? "a" "abc" 0))
  1165. (test-equal #f (string-prefix? "c" "abc" 0))
  1166. (test-equal #t (string-prefix? "ab" "abc" 0))
  1167. (test-equal #f (string-prefix? "ac" "abc" 0))
  1168. (test-equal #t (string-prefix? "abc" "abc" 0))
  1169. (test-equal #t (string-suffix? "" "" 0))
  1170. (test-equal #t (string-suffix? "" "abc" 0))
  1171. (test-equal #f (string-suffix? "a" "abc" 0))
  1172. (test-equal #t (string-suffix? "c" "abc" 0))
  1173. (test-equal #f (string-suffix? "ac" "abc" 0))
  1174. (test-equal #t (string-suffix? "bc" "abc" 0))
  1175. (test-equal #t (string-suffix? "abc" "abc" 0))
  1176. (test-equal #t (string-prefix? "ab" "abc" 2))
  1177. (test-equal #t (string-prefix? "ac" "abc" 2))
  1178. (test-equal #f (string-prefix? "abc" "abc" 2))
  1179. (test-equal #t (string-suffix? "ac" "abc" 2))
  1180. (test-equal #t (string-suffix? "bc" "abc" 2))
  1181. (test-equal #t (string-suffix? "abc" "abc" 2))
  1182. (test-equal #t (string-prefix? "" "" 0 0))
  1183. (test-equal #t (string-prefix? "" "abc" 0 0))
  1184. (test-equal #t (string-prefix? "a" "abc" 0 0))
  1185. (test-equal #f (string-prefix? "c" "abc" 0 1))
  1186. (test-equal #t (string-prefix? "ab" "abc" 0 1))
  1187. (test-equal #t (string-prefix? "ab" "abc" 0 2))
  1188. (test-equal #f (string-prefix? "ac" "abc" 0 2))
  1189. (test-equal #t (string-prefix? "abc" "abc" 0 3))
  1190. (test-equal #t (string-suffix? "" "" 0 0))
  1191. (test-equal #t (string-suffix? "" "abc" 0 0))
  1192. (test-equal #f (string-suffix? "a" "abc" 0 1))
  1193. (test-equal #t (string-suffix? "c" "abc" 0 1))
  1194. (test-equal #t (string-suffix? "ac" "abc" 1 2))
  1195. (test-equal #f (string-suffix? "ac" "abc" 0 2))
  1196. (test-equal #t (string-suffix? "bc" "abc" 0 2))
  1197. (test-equal #t (string-suffix? "abc" "abc" 0 3))
  1198. (test-equal #t (string-prefix? "ab" "abc" 2 2))
  1199. (test-equal #t (string-prefix? "ac" "abc" 2 2))
  1200. (test-equal #f (string-prefix? "abc" "abc" 2 3))
  1201. (test-equal #t (string-suffix? "ac" "abc" 2 2))
  1202. (test-equal #t (string-suffix? "bc" "abc" 2 2))
  1203. (test-equal #t (string-suffix? "abc" "abc" 2 3))
  1204. (test-equal #t (string-prefix? "" "" 0 0 0))
  1205. (test-equal #t (string-prefix? "" "abc" 0 0 0))
  1206. (test-equal #t (string-prefix? "a" "abc" 0 0 0))
  1207. (test-equal #f (string-prefix? "c" "abc" 0 1 0))
  1208. (test-equal #t (string-prefix? "ab" "abc" 0 1 0))
  1209. (test-equal #t (string-prefix? "ab" "abc" 0 2 0))
  1210. (test-equal #f (string-prefix? "ac" "abc" 0 2 0))
  1211. (test-equal #t (string-prefix? "abc" "abc" 0 3 0))
  1212. (test-equal #t (string-suffix? "" "" 0 0 0))
  1213. (test-equal #t (string-suffix? "" "abc" 0 0 0))
  1214. (test-equal #f (string-suffix? "a" "abc" 0 1 0))
  1215. (test-equal #t (string-suffix? "c" "abc" 0 1 0))
  1216. (test-equal #t (string-suffix? "ac" "abc" 1 2 0))
  1217. (test-equal #f (string-suffix? "ac" "abc" 0 2 0))
  1218. (test-equal #t (string-suffix? "bc" "abc" 0 2 0))
  1219. (test-equal #t (string-suffix? "abc" "abc" 0 3 0))
  1220. (test-equal #t (string-prefix? "ab" "abc" 2 2 0))
  1221. (test-equal #t (string-prefix? "ac" "abc" 2 2 0))
  1222. (test-equal #f (string-prefix? "abc" "abc" 2 3 0))
  1223. (test-equal #t (string-suffix? "ac" "abc" 2 2 0))
  1224. (test-equal #t (string-suffix? "bc" "abc" 2 2 0))
  1225. (test-equal #t (string-suffix? "abc" "abc" 2 3 0))
  1226. (test-equal #t (string-prefix? "" "abc" 0 0 1))
  1227. (test-equal #t (string-prefix? "a" "abc" 0 0 1))
  1228. (test-equal #t (string-prefix? "c" "abc" 0 1 2))
  1229. (test-equal #f (string-prefix? "ab" "abc" 0 1 2))
  1230. (test-equal #f (string-prefix? "ab" "abc" 0 2 1))
  1231. (test-equal #f (string-prefix? "ac" "abc" 0 2 1))
  1232. (test-equal #f (string-prefix? "abc" "abc" 0 3 1))
  1233. (test-equal #f (string-suffix? "a" "abc" 0 1 2))
  1234. (test-equal #t (string-suffix? "c" "abc" 0 1 1))
  1235. (test-equal #t (string-suffix? "ac" "abc" 1 2 2))
  1236. (test-equal #t (string-suffix? "bc" "abc" 0 2 1))
  1237. (test-equal #f (string-suffix? "bc" "abc" 0 2 2))
  1238. (test-equal #t (string-prefix? "" "" 0 0 0 0))
  1239. (test-equal #t (string-prefix? "" "abc" 0 0 0 3))
  1240. (test-equal #t (string-prefix? "a" "abc" 0 0 0 3))
  1241. (test-equal #f (string-prefix? "c" "abc" 0 1 0 3))
  1242. (test-equal #t (string-prefix? "ab" "abc" 0 1 0 3))
  1243. (test-equal #t (string-prefix? "ab" "abc" 0 2 0 3))
  1244. (test-equal #f (string-prefix? "ac" "abc" 0 2 0 3))
  1245. (test-equal #t (string-prefix? "abc" "abc" 0 3 0 3))
  1246. (test-equal #t (string-suffix? "" "abc" 0 0 0 3))
  1247. (test-equal #f (string-suffix? "a" "abc" 0 1 0 3))
  1248. (test-equal #t (string-suffix? "c" "abc" 0 1 0 3))
  1249. (test-equal #t (string-suffix? "ac" "abc" 1 2 0 3))
  1250. (test-equal #f (string-suffix? "ac" "abc" 0 2 0 3))
  1251. (test-equal #t (string-suffix? "bc" "abc" 0 2 0 3))
  1252. (test-equal #t (string-suffix? "abc" "abc" 0 3 0 3))
  1253. (test-equal #t (string-prefix? "ab" "abc" 2 2 0 3))
  1254. (test-equal #t (string-prefix? "ac" "abc" 2 2 0 3))
  1255. (test-equal #f (string-prefix? "abc" "abc" 2 3 0 3))
  1256. (test-equal #t (string-suffix? "ac" "abc" 2 2 0 3))
  1257. (test-equal #t (string-suffix? "bc" "abc" 2 2 0 3))
  1258. (test-equal #t (string-suffix? "abc" "abc" 2 3 0 3))
  1259. (test-equal #t (string-prefix? "" "abc" 0 0 1 3))
  1260. (test-equal #t (string-prefix? "a" "abc" 0 0 1 3))
  1261. (test-equal #t (string-prefix? "c" "abc" 0 1 2 3))
  1262. (test-equal #f (string-prefix? "ab" "abc" 0 1 2 3))
  1263. (test-equal #f (string-prefix? "ab" "abc" 0 2 1 3))
  1264. (test-equal #f (string-prefix? "ac" "abc" 0 2 1 3))
  1265. (test-equal #f (string-prefix? "abc" "abc" 0 3 1 3))
  1266. (test-equal #f (string-suffix? "a" "abc" 0 1 2 3))
  1267. (test-equal #t (string-suffix? "c" "abc" 0 1 1 3))
  1268. (test-equal #t (string-suffix? "ac" "abc" 1 2 2 3))
  1269. (test-equal #t (string-suffix? "bc" "abc" 0 2 1 3))
  1270. (test-equal #f (string-suffix? "bc" "abc" 0 2 2 3))
  1271. (test-equal #t (string-prefix? "" "abc" 0 0 0 2))
  1272. (test-equal #t (string-prefix? "a" "abc" 0 0 0 2))
  1273. (test-equal #f (string-prefix? "c" "abc" 0 1 0 2))
  1274. (test-equal #t (string-prefix? "ab" "abc" 0 1 0 2))
  1275. (test-equal #f (string-prefix? "abc" "abc" 0 3 0 2))
  1276. (test-equal #t (string-suffix? "" "abc" 0 0 0 2))
  1277. (test-equal #f (string-suffix? "c" "abc" 0 1 0 2))
  1278. (test-equal #f (string-suffix? "ac" "abc" 1 2 0 2))
  1279. ;;; Searching
  1280. (test-equal #f (string-index "" char?))
  1281. (test-equal 0 (string-index "abcdef" char?))
  1282. (test-equal 4 (string-index "abcdef" (lambda (c) (char>? c #\d))))
  1283. (test-equal #f (string-index "abcdef" char-whitespace?))
  1284. (test-equal #f (string-index-right "" char?))
  1285. (test-equal 5 (string-index-right "abcdef" char?))
  1286. (test-equal 5 (string-index-right "abcdef"
  1287. (lambda (c) (char>? c #\d))))
  1288. (test-equal #f (string-index-right "abcdef" char-whitespace?))
  1289. (test-equal #f (string-skip "" string?))
  1290. (test-equal 0 (string-skip "abcdef" string?))
  1291. (test-equal 4 (string-skip "abcdef" (lambda (c) (char<=? c #\d))))
  1292. (test-equal #f (string-skip "abcdef" char?))
  1293. (test-equal #f (string-skip-right "" string?))
  1294. (test-equal 5 (string-skip-right "abcdef" string?))
  1295. (test-equal 5 (string-skip-right "abcdef"
  1296. (lambda (c) (char<=? c #\d))))
  1297. (test-equal #f (string-skip-right "abcdef" char?))
  1298. (test-equal 2 (string-index "abcdef" char? 2))
  1299. (test-equal 4 (string-index "abcdef" (lambda (c) (char>? c #\d)) 2))
  1300. (test-equal #f (string-index "abcdef" char-whitespace? 2))
  1301. (test-equal 5 (string-index-right "abcdef" char? 2))
  1302. (test-equal 5 (string-index-right "abcdef"
  1303. (lambda (c)
  1304. (char>? c #\d)) 2))
  1305. (test-equal #f (string-index-right "abcdef" char-whitespace? 2))
  1306. (test-equal 2 (string-skip "abcdef" string? 2))
  1307. (test-equal 4 (string-skip "abcdef"
  1308. (lambda (c)
  1309. (char<=? c #\d)) 2))
  1310. (test-equal #f (string-skip "abcdef" char? 2))
  1311. (test-equal 5 (string-skip-right "abcdef" string? 2))
  1312. (test-equal 5 (string-skip-right "abcdef"
  1313. (lambda (c)
  1314. (char<=? c #\d)) 2))
  1315. (test-equal #f (string-skip-right "abcdef" char? 2))
  1316. (test-equal 2 (string-index "abcdef" char? 2 5))
  1317. (test-equal 4 (string-index "abcdef"
  1318. (lambda (c) (char>? c #\d)) 2 5))
  1319. (test-equal #f (string-index "abcdef" char-whitespace? 2 5))
  1320. (test-equal 4 (string-index-right "abcdef" char? 2 5))
  1321. (test-equal 4 (string-index-right "abcdef"
  1322. (lambda (c)
  1323. (char>? c #\d)) 2 5))
  1324. (test-equal #f (string-index-right "abcdef"
  1325. char-whitespace? 2 5))
  1326. (test-equal 2 (string-skip "abcdef" string? 2 5))
  1327. (test-equal 4 (string-skip "abcdef"
  1328. (lambda (c) (char<=? c #\d)) 2 5))
  1329. (test-equal #f (string-skip "abcdef" char? 2 5))
  1330. (test-equal 4 (string-skip-right "abcdef" string? 2 5))
  1331. (test-equal 4 (string-skip-right "abcdef"
  1332. (lambda (c)
  1333. (char<=? c #\d)) 2 5))
  1334. (test-equal #f (string-skip-right "abcdef" char? 2 5))
  1335. (test-equal 0 (string-contains "" ""))
  1336. (test-equal 0 (string-contains "abcdeffffoo" ""))
  1337. (test-equal 0 (string-contains "abcdeffffoo" "a"))
  1338. (test-equal 5 (string-contains "abcdeffffoo" "ff"))
  1339. (test-equal 4 (string-contains "abcdeffffoo" "eff"))
  1340. (test-equal 8 (string-contains "abcdeffffoo" "foo"))
  1341. (test-equal #f (string-contains "abcdeffffoo" "efffoo"))
  1342. (test-equal 0 (string-contains-right "" ""))
  1343. (test-equal 11 (string-contains-right "abcdeffffoo" ""))
  1344. (test-equal 0 (string-contains-right "abcdeffffoo" "a"))
  1345. (test-equal 7 (string-contains-right "abcdeffffoo" "ff"))
  1346. (test-equal 4 (string-contains-right "abcdeffffoo" "eff"))
  1347. (test-equal 8 (string-contains-right "abcdeffffoo" "foo"))
  1348. (test-equal #f (string-contains-right "abcdeffffoo"
  1349. "efffoo"))
  1350. (test-equal 0 (string-contains "" "" 0))
  1351. (test-equal 2 (string-contains "abcdeffffoo" "" 2))
  1352. (test-equal #f (string-contains "abcdeffffoo" "a" 2))
  1353. (test-equal 5 (string-contains "abcdeffffoo" "ff" 2))
  1354. (test-equal 4 (string-contains "abcdeffffoo" "eff" 2))
  1355. (test-equal 8 (string-contains "abcdeffffoo" "foo" 2))
  1356. (test-equal #f (string-contains "abcdeffffoo" "efffoo" 2))
  1357. (test-equal 0 (string-contains-right "" "" 0))
  1358. (test-equal 11 (string-contains-right "abcdeffffoo" "" 2))
  1359. (test-equal #f (string-contains-right "abcdeffffoo" "a" 2))
  1360. (test-equal 7 (string-contains-right "abcdeffffoo" "ff" 2))
  1361. (test-equal 4 (string-contains-right "abcdeffffoo" "eff" 2))
  1362. (test-equal 8 (string-contains-right "abcdeffffoo" "foo" 2))
  1363. (test-equal #f (string-contains-right "abcdeffffoo" "efffoo" 2))
  1364. (test-equal 0 (string-contains "" "" 0 0))
  1365. (test-equal 2 (string-contains "abcdeffffoo" "" 2 10))
  1366. (test-equal #f (string-contains "abcdeffffoo" "a" 2 10))
  1367. (test-equal 5 (string-contains "abcdeffffoo" "ff" 2 10))
  1368. (test-equal 4 (string-contains "abcdeffffoo" "eff" 2 10))
  1369. (test-equal #f (string-contains "abcdeffffoo" "foo" 2 10))
  1370. (test-equal #f (string-contains "abcdeffffoo" "efffoo" 2 10))
  1371. (test-equal 0 (string-contains-right "" "" 0 0))
  1372. (test-equal 10 (string-contains-right "abcdeffffoo" "" 2 10))
  1373. (test-equal #f (string-contains-right "abcdeffffoo" "a" 2 10))
  1374. (test-equal 7 (string-contains-right "abcdeffffoo" "ff" 2 10))
  1375. (test-equal 4 (string-contains-right "abcdeffffoo" "eff" 2 10))
  1376. (test-equal #f (string-contains-right "abcdeffffoo" "foo" 2 10))
  1377. (test-equal #f (string-contains-right "abcdeffffoo" "efffoo" 2 10))
  1378. (test-equal 0 (string-contains "" "" 0 0 0))
  1379. (test-equal 2 (string-contains "abcdeffffoo" "" 2 10 0))
  1380. (test-equal 2 (string-contains "abcdeffffoo" "a" 2 10 1))
  1381. (test-equal 5 (string-contains "abcdeffffoo" "ff" 2 10 1))
  1382. (test-equal 5 (string-contains "abcdeffffoo" "eff" 2 10 1))
  1383. (test-equal #f (string-contains "abcdeffffoo" "foo" 2 10 1))
  1384. (test-equal #f (string-contains "abcdeffffoo" "efffoo" 2 10 1))
  1385. (test-equal 0 (string-contains-right "" "" 0 0 0))
  1386. (test-equal 10 (string-contains-right "abcdeffffoo" "" 2 10 0))
  1387. (test-equal 10 (string-contains-right "abcdeffffoo" "a" 2 10 1))
  1388. (test-equal 8 (string-contains-right "abcdeffffoo" "ff" 2 10 1))
  1389. (test-equal 7 (string-contains-right "abcdeffffoo" "eff" 2 10 1))
  1390. (test-equal #f (string-contains-right "abcdeffffoo" "foo" 2 10 1))
  1391. (test-equal #f (string-contains-right "abcdeffffoo" "efffoo" 2 10 1))
  1392. (test-equal 0 (string-contains "" "" 0 0 0 0))
  1393. (test-equal 2 (string-contains "abcdeffffoo" "" 2 10 0 0))
  1394. (test-equal 2 (string-contains "abcdeffffoo" "a" 2 10 1 1))
  1395. (test-equal 5 (string-contains "abcdeffffoo" "ff" 2 10 1 2))
  1396. (test-equal 5 (string-contains "abcdeffffoo" "eff" 2 10 1 2))
  1397. (test-equal 9 (string-contains "abcdeffffoo" "foo" 2 10 1 2))
  1398. (test-equal 4 (string-contains "abcdeffffoo" "efffoo" 2 10 0 2))
  1399. (test-equal 0 (string-contains-right "" "" 0 0 0 0))
  1400. (test-equal 10 (string-contains-right "abcdeffffoo" "" 2 10 0 0))
  1401. (test-equal 10 (string-contains-right "abcdeffffoo" "a" 2 10 1 1))
  1402. (test-equal 8 (string-contains-right "abcdeffffoo" "ff" 2 10 1 2))
  1403. (test-equal 8 (string-contains-right "abcdeffffoo" "eff" 2 10 1 2))
  1404. (test-equal 9 (string-contains-right "abcdeffffoo" "foo" 2 10 1 2))
  1405. (test-equal 7 (string-contains-right "abcdeffffoo" "efffoo" 2 10 1 3))
  1406. ;;; Case conversion
  1407. ;;; FIXME: should test some non-ASCII cases here.
  1408. (test-equal "1234STRIKES" (string-upcase "1234Strikes"))
  1409. (test-equal "1234STRIKES" (string-upcase "1234strikes"))
  1410. (test-equal "1234STRIKES" (string-upcase "1234STRIKES"))
  1411. (test-equal "1234strikes" (string-downcase "1234Strikes"))
  1412. (test-equal "1234strikes" (string-downcase "1234strikes"))
  1413. (test-equal "1234strikes" (string-downcase "1234STRIKES"))
  1414. (test-equal "1234strikes" (string-foldcase "1234Strikes"))
  1415. (test-equal "1234strikes" (string-foldcase "1234strikes"))
  1416. (test-equal "1234strikes" (string-foldcase "1234STRIKES"))
  1417. (test-equal "And With Three Strikes You Are Out"
  1418. (string-titlecase
  1419. "and with THREE STRIKES you are oUT"))
  1420. ;;; Concatenation
  1421. (test-equal "" (string-append))
  1422. (test-equal "abcdef"
  1423. (string-append ""
  1424. "a"
  1425. "bcd"
  1426. "" "ef" "" ""))
  1427. (test-equal "" (string-concatenate '()))
  1428. (test-equal "abcdef"
  1429. (string-concatenate '("" "a" "bcd" "" "ef" "" "")))
  1430. ;;; string-concatenate is likely to have special cases for longer strings.
  1431. (let* ((alphabet "abcdefghijklmnopqrstuvwxyz")
  1432. (str1 alphabet)
  1433. (str10 (apply string-append (vector->list (make-vector 10 str1))))
  1434. (str100 (apply string-append (vector->list (make-vector 10 str10))))
  1435. (str100-500 (substring str100 100 500))
  1436. (str600-999 (substring str100 600 999))
  1437. (alph1 (string-copy alphabet))
  1438. (alph10 (string-concatenate (vector->list (make-vector 10 alph1))))
  1439. (alph100 (string-concatenate (vector->list (make-vector 10 alph10))))
  1440. (t100-500 (substring alph100 100 500))
  1441. (t600-999 (substring alph100 600 999)))
  1442. (test-equal str10 alph10)
  1443. (test-equal str100 alph100)
  1444. (test-equal str100-500 t100-500)
  1445. (test-equal str600-999 t600-999)
  1446. ;; concatenating a short string with a long string
  1447. (test-equal (string-append str1 str600-999)
  1448. (string-concatenate (list alph1 t600-999)))
  1449. (test-equal (string-append str1 str600-999)
  1450. (string-concatenate (list alph1 (string-copy t600-999))))
  1451. (test-equal (string-append str600-999 str1)
  1452. (string-concatenate (list t600-999 alph1)))
  1453. (test-equal (string-append str600-999 str1)
  1454. (string-concatenate (list (string-copy t600-999) alph1))))
  1455. (test-equal "" (string-concatenate-reverse '()))
  1456. (test-equal "efbcda"
  1457. (string-concatenate-reverse '("" "a" "bcd" "" "ef" "" "")))
  1458. (test-equal "huh?"
  1459. (string-concatenate-reverse '() "huh?"))
  1460. (test-equal "efbcdaxy"
  1461. (string-concatenate-reverse '("" "a" "bcd" "" "ef" "" "") "xy"))
  1462. (test-equal "huh"
  1463. (string-concatenate-reverse '() "huh?" 3))
  1464. (test-equal "efbcdax"
  1465. (string-concatenate-reverse
  1466. '("" "a" "bcd" "" "ef" "" "") "x" 1))
  1467. (test-equal "" (string-join '()))
  1468. (test-equal " ab cd e f "
  1469. (string-join '("" "ab" "cd" "" "e" "f" "")))
  1470. (test-equal ""
  1471. (string-join '() ""))
  1472. (test-equal "abcdef"
  1473. (string-join '("" "ab" "cd" "" "e" "f" "") ""))
  1474. (test-equal ""
  1475. (string-join '() "xyz"))
  1476. (test-equal "xyzabxyzcdxyzxyzexyzfxyz"
  1477. (string-join '("" "ab" "cd" "" "e" "f" "") "xyz"))
  1478. (test-equal ""
  1479. (string-join '() "" 'infix))
  1480. (test-equal "abcdef"
  1481. (string-join '("" "ab" "cd" "" "e" "f" "") "" 'infix))
  1482. (test-equal ""
  1483. (string-join '() "xyz" 'infix))
  1484. (test-equal "xyzabxyzcdxyzxyzexyzfxyz"
  1485. (string-join '("" "ab" "cd" "" "e" "f" "") "xyz" 'infix))
  1486. (test-equal "foo bar baz" (string-join '("foo" "bar" "baz")))
  1487. (test-equal "foobarbaz" (string-join '("foo" "bar" "baz") ""))
  1488. (test-equal "foo:bar:baz" (string-join '("foo" "bar" "baz") ":"))
  1489. (test-equal "foo:bar:baz:" (string-join '("foo" "bar" "baz") ":" 'suffix))
  1490. (test-equal "" (string-join '() ":"))
  1491. (test-equal "" (string-join '("") ":"))
  1492. (test-equal "" (string-join '() ":" 'infix))
  1493. (test-error (string-join '() ":" 'strict-infix))
  1494. (test-equal "A" (string-join '("A") ":" 'strict-infix))
  1495. (test-equal "A:B" (string-join '("A" "B") ":" 'strict-infix))
  1496. (test-equal "" (string-join '() ":" 'suffix))
  1497. (test-equal ":" (string-join '("") ":" 'suffix))
  1498. (test-equal 'horror
  1499. (guard (exn (#t 'horror))
  1500. (string-join '() "" 'strict-infix)))
  1501. (test-equal "abcdef"
  1502. (string-join '("" "ab" "cd" "" "e" "f" "") "" 'strict-infix))
  1503. (test-equal 'wham
  1504. (guard (exn (else 'wham))
  1505. (string-join '() "xyz" 'strict-infix)))
  1506. (test-equal "xyzabxyzcdxyzxyzexyzfxyz"
  1507. (string-join '("" "ab" "cd" "" "e" "f" "") "xyz" 'strict-infix))
  1508. (test-equal ""
  1509. (string-join '() "" 'suffix))
  1510. (test-equal "abcdef"
  1511. (string-join '("" "ab" "cd" "" "e" "f" "") "" 'suffix))
  1512. (test-equal ""
  1513. (string-join '() "xyz" 'suffix))
  1514. (test-equal "xyzabxyzcdxyzxyzexyzfxyzxyz"
  1515. (string-join '("" "ab" "cd" "" "e" "f" "") "xyz" 'suffix))
  1516. (test-equal ""
  1517. (string-join '() "" 'prefix))
  1518. (test-equal "abcdef"
  1519. (string-join '("" "ab" "cd" "" "e" "f" "") "" 'prefix))
  1520. (test-equal ""
  1521. (string-join '() "xyz" 'prefix))
  1522. (test-equal "xyzxyzabxyzcdxyzxyzexyzfxyz"
  1523. (string-join '("" "ab" "cd" "" "e" "f" "") "xyz" 'prefix))
  1524. ;;; Fold & map & friends
  1525. (test-equal 8
  1526. (string-fold (lambda (c count)
  1527. (if (char-whitespace? c)
  1528. (+ count 1)
  1529. count))
  1530. 0
  1531. " ...a couple of spaces in this one... "))
  1532. (test-equal 7 (string-fold (lambda (c count)
  1533. (if (char-whitespace? c)
  1534. (+ count 1)
  1535. count))
  1536. 0
  1537. " ...a couple of spaces in this one... "
  1538. 1))
  1539. (test-equal 6 (string-fold (lambda (c count)
  1540. (if (char-whitespace? c)
  1541. (+ count 1)
  1542. count))
  1543. 0
  1544. " ...a couple of spaces in this one... "
  1545. 1
  1546. 32))
  1547. (test-equal (string->list "abcdef")
  1548. (string-fold-right cons '() "abcdef"))
  1549. (test-equal (string->list "def")
  1550. (string-fold-right cons '() "abcdef" 3))
  1551. (test-equal (string->list "cde")
  1552. (string-fold-right cons '() "abcdef" 2 5))
  1553. (test-equal "aabraacaadaabraa"
  1554. (let* ((s "abracadabra")
  1555. (ans-len (string-fold (lambda (c sum)
  1556. (+ sum (if (char=? c #\a) 2 1)))
  1557. 0 s))
  1558. (ans (make-string ans-len)))
  1559. (string-fold (lambda (c i)
  1560. (let ((i (if (char=? c #\a)
  1561. (begin (string-set! ans i #\a)
  1562. (+ i 1))
  1563. i)))
  1564. (string-set! ans i c)
  1565. (+ i 1)))
  1566. 0 s)
  1567. ans))
  1568. (test-equal "abc" (string-map string "abc"))
  1569. (test-equal "ABC" (string-map char-upcase "abc"))
  1570. (test-equal "Hear-here!"
  1571. (string-map (lambda (c0 c1 c2)
  1572. (case c0
  1573. ((#\1) c1)
  1574. ((#\2) (string c2))
  1575. ((#\-) (string #\- c1))))
  1576. "1222-1111-2222"
  1577. "Hi There!"
  1578. "Dear John"))
  1579. (test-equal "abc"
  1580. (let ((q (open-output-string)))
  1581. (string-for-each (lambda (c) (write-char c q))
  1582. "abc")
  1583. (get-output-string q)))
  1584. (test-equal '("cfi" "beh" "adg")
  1585. (let ((x '()))
  1586. (string-for-each (lambda (c1 c2 c3)
  1587. (set! x (cons (string c1 c2 c3) x)))
  1588. "abc"
  1589. "defxyz"
  1590. "ghijklmnopqrstuvwxyz")
  1591. x))
  1592. (test-equal "abc"
  1593. (string-map-index (lambda (i)
  1594. (integer->char (+ i (char->integer #\a))))
  1595. "xyz"))
  1596. (let ((r (string-map-index (lambda (i)
  1597. (integer->char (+ i (char->integer #\a))))
  1598. "xyz***" 3)))
  1599. (test-equal '(#t 3) (check-istring r))
  1600. (test-equal "def" r))
  1601. (test-equal "cde"
  1602. (string-map-index (lambda (i)
  1603. (integer->char (+ i (char->integer #\a))))
  1604. "......" 2 5))
  1605. (test-equal '(101 100 99 98 97)
  1606. (let ((s "abcde")
  1607. (v '()))
  1608. (string-for-each-index
  1609. (lambda (i)
  1610. (set! v (cons (char->integer (string-ref s i)) v)))
  1611. s)
  1612. v))
  1613. (test-equal '(101 100 99)
  1614. (let ((s "abcde")
  1615. (v '()))
  1616. (string-for-each-index
  1617. (lambda (i)
  1618. (set! v (cons (char->integer (string-ref s i)) v)))
  1619. s 2)
  1620. v))
  1621. (test-equal '(99 98)
  1622. (let ((s "abcde")
  1623. (v '()))
  1624. (string-for-each-index
  1625. (lambda (i)
  1626. (set! v (cons (char->integer (string-ref s i)) v)))
  1627. s 1 3)
  1628. v))
  1629. (test-equal 6 (string-count "abcdef" char?))
  1630. (test-equal 4 (string-count "counting whitespace, again " char-whitespace? 5))
  1631. (test-equal 3 (string-count "abcdefwxyz"
  1632. (lambda (c) (odd? (char->integer c)))
  1633. 2 8))
  1634. (let ((r (string-filter (lambda (c) (memv c (string->list "aeiou")))
  1635. "What is number, that man may know it?")))
  1636. (test-equal "aiueaaaoi" r)
  1637. (test-equal '(#t 9) (check-istring r)))
  1638. (let ((r (string-remove (lambda (c) (memv c (string->list "aeiou")))
  1639. "And woman, that she may know number?")))
  1640. (test-equal "And wmn, tht sh my knw nmbr?" r)
  1641. (test-equal '(#t 28) (check-istring r)))
  1642. (test-equal "iueaaaoi"
  1643. (string-filter (lambda (c) (memv c (string->list "aeiou")))
  1644. "What is number, that man may know it?"
  1645. 4))
  1646. (test-equal "mn, tht sh my knw nmbr?"
  1647. (string-remove (lambda (c) (memv c (string->list "aeiou")))
  1648. "And woman, that she may know number?"
  1649. 6))
  1650. (test-equal "aaao"
  1651. (string-filter (lambda (c) (memv c (string->list "aeiou")))
  1652. "What is number, that man may know it?"
  1653. 16 32))
  1654. (test-equal "And woman, that sh may know"
  1655. (string-remove (lambda (c) (memv c (string->list "eiu")))
  1656. "And woman, that she may know number?"
  1657. 0 28))
  1658. #|
  1659. (test-equal "" (string-reverse ""))
  1660. (test-equal "fedcba" (string-reverse "abcdef"))
  1661. (test-equal "" (string-reverse "" 0))
  1662. (test-equal "fedcba" (string-reverse "abcdef" 0))
  1663. (test-equal "fedc" (string-reverse "abcdef" 2))
  1664. (test-equal "" (string-reverse "" 0 0))
  1665. (test-equal "fedcba" (string-reverse "abcdef" 0 6))
  1666. (test-equal "edc" (string-reverse "abcdef" 2 5))
  1667. |#
  1668. ;;; Replication and splitting
  1669. (test-equal "" (string-repeat #\X 0))
  1670. (test-equal "XXX" (string-repeat #\X 3))
  1671. (test-equal "" (string-repeat "abc" 0))
  1672. (test-equal "abcabcabc" (string-repeat "abc" 3))
  1673. (test-equal "cdefabcdefabcd"
  1674. (xsubstring "abcdef" -4 10))
  1675. (test-equal "bcdefbcdefbcd"
  1676. (xsubstring "abcdef" 90 103 1))
  1677. (test-equal "ecdecdecde"
  1678. (xsubstring "abcdef" -13 -3 2 5))
  1679. (test-equal "cdefab" (xsubstring "abcdef" 2 8))
  1680. (test-equal "efabcd" (xsubstring "abcdef" -2 4))
  1681. (test-equal "abcabca" (xsubstring "abc" 0 7))
  1682. (test-equal '() (string-split "" ""))
  1683. (test-equal '("a" "b" "c") (string-split "abc" ""))
  1684. (test-equal '("too" "" "much" "" "data")
  1685. (string-split "too much data" " "))
  1686. (test-equal '("" "there" "ya" "go" "")
  1687. (string-split "***there***ya***go***" "***"))
  1688. (test-equal '() (string-split "" "" 'infix))
  1689. (test-equal '("a" "b" "c")
  1690. (string-split "abc" "" 'infix))
  1691. (test-equal '("too" "" "much" "" "data")
  1692. (string-split "too much data" " " 'infix))
  1693. (test-equal '("" "there" "ya" "go" "")
  1694. (string-split "***there***ya***go***" "***" 'infix))
  1695. (test-equal 'error
  1696. (guard (exn (else 'error))
  1697. (string-split "" "" 'strict-infix)))
  1698. (test-equal '("a" "b" "c")
  1699. (string-split "abc" "" 'strict-infix))
  1700. (test-equal '("too" "" "much" "" "data")
  1701. (string-split "too much data" " " 'strict-infix))
  1702. (test-equal '("" "there" "ya" "go" "")
  1703. (string-split "***there***ya***go***" "***" 'strict-infix))
  1704. (test-equal '()
  1705. (string-split "" "" 'prefix))
  1706. (test-equal '("a" "b" "c")
  1707. (string-split "abc" "" 'prefix))
  1708. (test-equal '("too" "" "much" "" "data")
  1709. (string-split "too much data" " " 'prefix))
  1710. (test-equal '("there" "ya" "go" "")
  1711. (string-split "***there***ya***go***" "***" 'prefix))
  1712. (test-equal '()
  1713. (string-split "" "" 'suffix))
  1714. (test-equal '("a" "b" "c")
  1715. (string-split "abc" "" 'suffix))
  1716. (test-equal '("too" "" "much" "" "data")
  1717. (string-split "too much data" " " 'suffix))
  1718. (test-equal '("" "there" "ya" "go")
  1719. (string-split "***there***ya***go***" "***" 'suffix))
  1720. (test-equal '() (string-split "" "" 'infix #f))
  1721. (test-equal '("a" "b" "c") (string-split "abc" "" 'infix #f))
  1722. (test-equal '("too" "" "much" "" "data")
  1723. (string-split "too much data" " " 'infix #f))
  1724. (test-equal '("" "there" "ya" "go" "")
  1725. (string-split "***there***ya***go***" "***" 'infix #f))
  1726. (test-equal 'error
  1727. (guard (exn (else 'error))
  1728. (string-split "" "" 'strict-infix #f)))
  1729. (test-equal '("a" "b" "c")
  1730. (string-split "abc" "" 'strict-infix #f))
  1731. (test-equal '("too" "" "much" "" "data")
  1732. (string-split "too much data" " " 'strict-infix #f))
  1733. (test-equal '("" "there" "ya" "go" "")
  1734. (string-split "***there***ya***go***" "***" 'strict-infix #f))
  1735. (test-equal '() (string-split "" "" 'prefix #f))
  1736. (test-equal '("a" "b" "c")
  1737. (string-split "abc" "" 'prefix #f))
  1738. (test-equal '("too" "" "much" "" "data")
  1739. (string-split "too much data" " " 'prefix #f))
  1740. (test-equal '("there" "ya" "go" "")
  1741. (string-split "***there***ya***go***" "***" 'prefix #f))
  1742. (test-equal '()
  1743. (string-split "" "" 'suffix #f))
  1744. (test-equal '("a" "b" "c")
  1745. (string-split "abc" "" 'suffix #f))
  1746. (test-equal '("too" "" "much" "" "data")
  1747. (string-split "too much data" " " 'suffix #f))
  1748. (test-equal '("" "there" "ya" "go")
  1749. (string-split "***there***ya***go***" "***" 'suffix #f))
  1750. (test-equal 'error
  1751. (guard (exn (else 'error))
  1752. (string-split "" "" 'strict-infix 3)))
  1753. (test-equal '("a" "b" "c")
  1754. (string-split "abc" "" 'strict-infix 3))
  1755. (test-equal '("too" "" "much" " data")
  1756. (string-split "too much data" " " 'strict-infix 3))
  1757. (test-equal '("" "there" "ya" "go***")
  1758. (string-split "***there***ya***go***" "***" 'strict-infix 3))
  1759. (test-equal '()
  1760. (string-split "" "" 'prefix 3))
  1761. (test-equal '("a" "b" "c")
  1762. (string-split "abc" "" 'prefix 3))
  1763. (test-equal '("too" "" "much" " data")
  1764. (string-split "too much data" " " 'prefix 3))
  1765. (test-equal '("there" "ya" "go***")
  1766. (string-split "***there***ya***go***" "***" 'prefix 3))
  1767. (test-equal '()
  1768. (string-split "" "" 'suffix 3))
  1769. (test-equal '("a" "b" "c")
  1770. (string-split "abc" "" 'suffix 3))
  1771. (test-equal '("too" "" "much" " data")
  1772. (string-split "too much data" " " 'suffix 3))
  1773. (test-equal '("" "there" "ya" "go***")
  1774. (string-split "***there***ya***go***" "***" 'suffix 3))
  1775. (test-equal 'error
  1776. (guard (exn (else 'error))
  1777. (string-split "" "" 'strict-infix 3 0)))
  1778. (test-equal '("b" "c")
  1779. (string-split "abc" "" 'strict-infix 3 1))
  1780. (test-equal '("oo" "" "much" " data")
  1781. (string-split "too much data" " " 'strict-infix 3 1))
  1782. (test-equal '("**there" "ya" "go" "")
  1783. (string-split "***there***ya***go***" "***" 'strict-infix 3 1))
  1784. (test-equal '()
  1785. (string-split "" "" 'prefix 3 0))
  1786. (test-equal '("b" "c")
  1787. (string-split "abc" "" 'prefix 3 1))
  1788. (test-equal '("oo" "" "much" " data")
  1789. (string-split "too much data" " " 'prefix 3 1))
  1790. (test-equal '("**there" "ya" "go" "")
  1791. (string-split "***there***ya***go***" "***" 'prefix 3 1))
  1792. (test-equal '()
  1793. (string-split "" "" 'suffix 3 0))
  1794. (test-equal '("b" "c")
  1795. (string-split "abc" "" 'suffix 3 1))
  1796. (test-equal '("oo" "" "much" " data")
  1797. (string-split "too much data" " " 'suffix 3 1))
  1798. (test-equal '("**there" "ya" "go")
  1799. (string-split "***there***ya***go***" "***" 'suffix 3 1))
  1800. (test-equal 'error
  1801. (guard (exn (else 'error))
  1802. (string-split "" "" 'strict-infix 3 0 0)))
  1803. (test-equal '("b")
  1804. (string-split "abc" "" 'strict-infix 3 1 2))
  1805. (test-equal '("oo" "" "much" " ")
  1806. (string-split "too much data" " " 'strict-infix 3 1 11))
  1807. (test-equal '()
  1808. (string-split "" "" 'prefix 3 0 0))
  1809. (test-equal '("b")
  1810. (string-split "abc" "" 'prefix 3 1 2))
  1811. (test-equal '("oo" "" "much" " ")
  1812. (string-split "too much data" " " 'prefix 3 1 11))
  1813. (test-equal '()
  1814. (string-split "" "" 'suffix 3 0 0))
  1815. (test-equal '("b")
  1816. (string-split "abc" "" 'suffix 3 1 2))
  1817. (test-equal '("oo" "" "much" " ")
  1818. (string-split "too much data" " " 'suffix 3 1 11))
  1819. (define (translate-space-to-newline str)
  1820. (let ((result (make-string 0)))
  1821. (string-for-each
  1822. (lambda (ch)
  1823. (string-append! result
  1824. (if (char=? ch #\space) #\newline ch)))
  1825. str)
  1826. result))
  1827. (test-equal "ab\ncd\nx"
  1828. (translate-space-to-newline "ab cd x"))
  1829. ;; begin section with UTF-8 literals
  1830. (cond-expand
  1831. (full-unicode
  1832. (let ((str (make-string 3 #\😂)))
  1833. (test-equal 3 (string-length str))
  1834. ;; (test-equal 6 (str:length))
  1835. (string-replace! str 1 2 "abc")
  1836. (test-equal "😂abc😂" str)
  1837. (string-replace! str 5 5 str 3)
  1838. (test-equal "😂abc😂c😂" str)
  1839. (string-replace! str 0 2 "ABC" 1 2)
  1840. (test-equal "Bbc😂c😂" str)
  1841. (test-equal 6 (string-length str))
  1842. (test-equal #\c (string-ref str 2))
  1843. (test-equal #\x1f602 (string-ref str 3))
  1844. (test-equal #\c (string-ref str 4)))
  1845. (test-equal "c😼b😂a" (reverse-list->string '(#\a #\😂 #\b #\😼 #\c)))
  1846. (test-equal "y😂a😼xy" (xsubstring "a😼xy😂" 3 9))
  1847. (test-equal "y😂a😼" (xsubstring "a😼xy😂" -2 2))
  1848. ))
  1849. ;; end section with UTF-8 literals
  1850. (test-end)