13.upstream.scm 76 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020
  1. ;;; SRFI 13 string library reference implementation -*- Scheme -*-
  2. ;;; Olin Shivers 7/2000
  3. ;;;
  4. ;;; Copyright (c) 1988-1994 Massachusetts Institute of Technology.
  5. ;;; Copyright (c) 1998, 1999, 2000 Olin Shivers. All rights reserved.
  6. ;;; The details of the copyrights appear at the end of the file. Short
  7. ;;; summary: BSD-style open source.
  8. ;;; Exports:
  9. ;;; string-map string-map!
  10. ;;; string-fold string-unfold
  11. ;;; string-fold-right string-unfold-right
  12. ;;; string-tabulate string-for-each string-for-each-index
  13. ;;; string-every string-any
  14. ;;; string-hash string-hash-ci
  15. ;;; string-compare string-compare-ci
  16. ;;; string= string< string> string<= string>= string<>
  17. ;;; string-ci= string-ci< string-ci> string-ci<= string-ci>= string-ci<>
  18. ;;; string-downcase string-upcase string-titlecase
  19. ;;; string-downcase! string-upcase! string-titlecase!
  20. ;;; string-take string-take-right
  21. ;;; string-drop string-drop-right
  22. ;;; string-pad string-pad-right
  23. ;;; string-trim string-trim-right string-trim-both
  24. ;;; string-filter string-delete
  25. ;;; string-index string-index-right
  26. ;;; string-skip string-skip-right
  27. ;;; string-count
  28. ;;; string-prefix-length string-prefix-length-ci
  29. ;;; string-suffix-length string-suffix-length-ci
  30. ;;; string-prefix? string-prefix-ci?
  31. ;;; string-suffix? string-suffix-ci?
  32. ;;; string-contains string-contains-ci
  33. ;;; string-copy! substring/shared
  34. ;;; string-reverse string-reverse! reverse-list->string
  35. ;;; string-concatenate string-concatenate/shared string-concatenate-reverse
  36. ;;; string-append/shared
  37. ;;; xsubstring string-xcopy!
  38. ;;; string-null?
  39. ;;; string-join
  40. ;;; string-tokenize
  41. ;;; string-replace
  42. ;;;
  43. ;;; R5RS extended:
  44. ;;; string->list string-copy string-fill!
  45. ;;;
  46. ;;; R5RS re-exports:
  47. ;;; string? make-string string-length string-ref string-set!
  48. ;;;
  49. ;;; R5RS re-exports (also defined here but commented-out):
  50. ;;; string string-append list->string
  51. ;;;
  52. ;;; Low-level routines:
  53. ;;; make-kmp-restart-vector string-kmp-partial-search kmp-step
  54. ;;; string-parse-start+end
  55. ;;; string-parse-final-start+end
  56. ;;; let-string-start+end
  57. ;;; check-substring-spec
  58. ;;; substring-spec-ok?
  59. ;;; Imports
  60. ;;; This is a fairly large library. While it was written for portability, you
  61. ;;; must be aware of its dependencies in order to run it in a given scheme
  62. ;;; implementation. Here is a complete list of the dependencies it has and the
  63. ;;; assumptions it makes beyond stock R5RS Scheme:
  64. ;;;
  65. ;;; This code has the following non-R5RS dependencies:
  66. ;;; - (RECEIVE (var ...) mv-exp body ...) multiple-value binding macro;
  67. ;;;
  68. ;;; - Various imports from the char-set library for the routines that can
  69. ;;; take char-set arguments;
  70. ;;;
  71. ;;; - An n-ary ERROR procedure;
  72. ;;;
  73. ;;; - BITWISE-AND for the hash functions;
  74. ;;;
  75. ;;; - A simple CHECK-ARG procedure for checking parameter values; it is
  76. ;;; (lambda (pred val proc)
  77. ;;; (if (pred val) val (error "Bad arg" val pred proc)))
  78. ;;;
  79. ;;; - :OPTIONAL and LET-OPTIONALS* macros for parsing, defaulting &
  80. ;;; type-checking optional parameters from a rest argument;
  81. ;;;
  82. ;;; - CHAR-CASED? and CHAR-TITLECASE for the STRING-TITLECASE &
  83. ;;; STRING-TITLECASE! procedures. The former returns true iff a character is
  84. ;;; one that has case distinctions; in ASCII it returns true on a-z and A-Z.
  85. ;;; CHAR-TITLECASE is analagous to CHAR-UPCASE and CHAR-DOWNCASE. In ASCII &
  86. ;;; Latin-1, it is the same as CHAR-UPCASE.
  87. ;;;
  88. ;;; The code depends upon a small set of core string primitives from R5RS:
  89. ;;; MAKE-STRING STRING-REF STRING-SET! STRING? STRING-LENGTH SUBSTRING
  90. ;;; (Actually, SUBSTRING is not a primitive, but we assume that an
  91. ;;; implementation's native version is probably faster than one we could
  92. ;;; define, so we import it from R5RS.)
  93. ;;;
  94. ;;; The code depends upon a small set of R5RS character primitives:
  95. ;;; char? char=? char-ci=? char<? char-ci<?
  96. ;;; char-upcase char-downcase
  97. ;;; char->integer (for the hash functions)
  98. ;;;
  99. ;;; We assume the following:
  100. ;;; - CHAR-DOWNCASE o CHAR-UPCASE = CHAR-DOWNCASE
  101. ;;; - CHAR-CI=? is equivalent to
  102. ;;; (lambda (c1 c2) (char=? (char-downcase (char-upcase c1))
  103. ;;; (char-downcase (char-upcase c2))))
  104. ;;; - CHAR-UPCASE, CHAR-DOWNCASE and CHAR-TITLECASE are locale-insensitive
  105. ;;; and consistent with Unicode's 1-1 char-mapping spec.
  106. ;;; These things are typically true, but if not, you would need to modify
  107. ;;; the case-mapping and case-insensitive routines.
  108. ;;; Enough introductory blather. On to the source code. (But see the end of
  109. ;;; the file for further notes on porting & performance tuning.)
  110. ;;; Support for START/END substring specs
  111. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  112. ;;; This macro parses optional start/end arguments from arg lists, defaulting
  113. ;;; them to 0/(string-length s), and checks them for correctness.
  114. (define-syntax let-string-start+end
  115. (syntax-rules ()
  116. ((let-string-start+end (start end) proc s-exp args-exp body ...)
  117. (receive (start end) (string-parse-final-start+end proc s-exp args-exp)
  118. body ...))
  119. ((let-string-start+end (start end rest) proc s-exp args-exp body ...)
  120. (receive (rest start end) (string-parse-start+end proc s-exp args-exp)
  121. body ...))))
  122. ;;; This one parses out a *pair* of final start/end indices.
  123. ;;; Not exported; for internal use.
  124. (define-syntax let-string-start+end2
  125. (syntax-rules ()
  126. ((l-s-s+e2 (start1 end1 start2 end2) proc s1 s2 args body ...)
  127. (let ((procv proc)) ; Make sure PROC is only evaluated once.
  128. (let-string-start+end (start1 end1 rest) procv s1 args
  129. (let-string-start+end (start2 end2) procv s2 rest
  130. body ...))))))
  131. ;;; Returns three values: rest start end
  132. (define (string-parse-start+end proc s args)
  133. (if (not (string? s)) (error "Non-string value" proc s))
  134. (let ((slen (string-length s)))
  135. (if (pair? args)
  136. (let ((start (car args))
  137. (args (cdr args)))
  138. (if (and (integer? start) (exact? start) (>= start 0))
  139. (receive (end args)
  140. (if (pair? args)
  141. (let ((end (car args))
  142. (args (cdr args)))
  143. (if (and (integer? end) (exact? end) (<= end slen))
  144. (values end args)
  145. (error "Illegal substring END spec" proc end s)))
  146. (values slen args))
  147. (if (<= start end) (values args start end)
  148. (error "Illegal substring START/END spec"
  149. proc start end s)))
  150. (error "Illegal substring START spec" proc start s)))
  151. (values '() 0 slen))))
  152. (define (string-parse-final-start+end proc s args)
  153. (receive (rest start end) (string-parse-start+end proc s args)
  154. (if (pair? rest) (error "Extra arguments to procedure" proc rest)
  155. (values start end))))
  156. (define (substring-spec-ok? s start end)
  157. (and (string? s)
  158. (integer? start)
  159. (exact? start)
  160. (integer? end)
  161. (exact? end)
  162. (<= 0 start)
  163. (<= start end)
  164. (<= end (string-length s))))
  165. (define (check-substring-spec proc s start end)
  166. (if (not (substring-spec-ok? s start end))
  167. (error "Illegal substring spec." proc s start end)))
  168. ;;; Defined by R5RS, so commented out here.
  169. ;(define (string . chars)
  170. ; (let* ((len (length chars))
  171. ; (ans (make-string len)))
  172. ; (do ((i 0 (+ i 1))
  173. ; (chars chars (cdr chars)))
  174. ; ((>= i len))
  175. ; (string-set! ans i (car chars)))
  176. ; ans))
  177. ;
  178. ;(define (string . chars) (string-unfold null? car cdr chars))
  179. ;;; substring/shared S START [END]
  180. ;;; string-copy S [START END]
  181. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  182. ;;; All this goop is just arg parsing & checking surrounding a call to the
  183. ;;; actual primitive, %SUBSTRING/SHARED.
  184. (define (substring/shared s start . maybe-end)
  185. (check-arg string? s substring/shared)
  186. (let ((slen (string-length s)))
  187. (check-arg (lambda (start) (and (integer? start) (exact? start) (<= 0 start)))
  188. start substring/shared)
  189. (%substring/shared s start
  190. (:optional maybe-end slen
  191. (lambda (end) (and (integer? end)
  192. (exact? end)
  193. (<= start end)
  194. (<= end slen)))))))
  195. ;;; Split out so that other routines in this library can avoid arg-parsing
  196. ;;; overhead for END parameter.
  197. (define (%substring/shared s start end)
  198. (if (and (zero? start) (= end (string-length s))) s
  199. (substring s start end)))
  200. (define (string-copy s . maybe-start+end)
  201. (let-string-start+end (start end) string-copy s maybe-start+end
  202. (substring s start end)))
  203. ;This library uses the R5RS SUBSTRING, but doesn't export it.
  204. ;Here is a definition, just for completeness.
  205. ;(define (substring s start end)
  206. ; (check-substring-spec substring s start end)
  207. ; (let* ((slen (- end start))
  208. ; (ans (make-string slen)))
  209. ; (do ((i 0 (+ i 1))
  210. ; (j start (+ j 1)))
  211. ; ((>= i slen) ans)
  212. ; (string-set! ans i (string-ref s j)))))
  213. ;;; Basic iterators and other higher-order abstractions
  214. ;;; (string-map proc s [start end])
  215. ;;; (string-map! proc s [start end])
  216. ;;; (string-fold kons knil s [start end])
  217. ;;; (string-fold-right kons knil s [start end])
  218. ;;; (string-unfold p f g seed [base make-final])
  219. ;;; (string-unfold-right p f g seed [base make-final])
  220. ;;; (string-for-each proc s [start end])
  221. ;;; (string-for-each-index proc s [start end])
  222. ;;; (string-every char-set/char/pred s [start end])
  223. ;;; (string-any char-set/char/pred s [start end])
  224. ;;; (string-tabulate proc len)
  225. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  226. ;;; You want compiler support for high-level transforms on fold and unfold ops.
  227. ;;; You'd at least like a lot of inlining for clients of these procedures.
  228. ;;; Don't hold your breath.
  229. (define (string-map proc s . maybe-start+end)
  230. (check-arg procedure? proc string-map)
  231. (let-string-start+end (start end) string-map s maybe-start+end
  232. (%string-map proc s start end)))
  233. (define (%string-map proc s start end) ; Internal utility
  234. (let* ((len (- end start))
  235. (ans (make-string len)))
  236. (do ((i (- end 1) (- i 1))
  237. (j (- len 1) (- j 1)))
  238. ((< j 0))
  239. (string-set! ans j (proc (string-ref s i))))
  240. ans))
  241. (define (string-map! proc s . maybe-start+end)
  242. (check-arg procedure? proc string-map!)
  243. (let-string-start+end (start end) string-map! s maybe-start+end
  244. (%string-map! proc s start end)))
  245. (define (%string-map! proc s start end)
  246. (do ((i (- end 1) (- i 1)))
  247. ((< i start))
  248. (string-set! s i (proc (string-ref s i)))))
  249. (define (string-fold kons knil s . maybe-start+end)
  250. (check-arg procedure? kons string-fold)
  251. (let-string-start+end (start end) string-fold s maybe-start+end
  252. (let lp ((v knil) (i start))
  253. (if (< i end) (lp (kons (string-ref s i) v) (+ i 1))
  254. v))))
  255. (define (string-fold-right kons knil s . maybe-start+end)
  256. (check-arg procedure? kons string-fold-right)
  257. (let-string-start+end (start end) string-fold-right s maybe-start+end
  258. (let lp ((v knil) (i (- end 1)))
  259. (if (>= i start) (lp (kons (string-ref s i) v) (- i 1))
  260. v))))
  261. ;;; (string-unfold p f g seed [base make-final])
  262. ;;; This is the fundamental constructor for strings.
  263. ;;; - G is used to generate a series of "seed" values from the initial seed:
  264. ;;; SEED, (G SEED), (G^2 SEED), (G^3 SEED), ...
  265. ;;; - P tells us when to stop -- when it returns true when applied to one
  266. ;;; of these seed values.
  267. ;;; - F maps each seed value to the corresponding character
  268. ;;; in the result string. These chars are assembled into the
  269. ;;; string in a left-to-right order.
  270. ;;; - BASE is the optional initial/leftmost portion of the constructed string;
  271. ;;; it defaults to the empty string "".
  272. ;;; - MAKE-FINAL is applied to the terminal seed value (on which P returns
  273. ;;; true) to produce the final/rightmost portion of the constructed string.
  274. ;;; It defaults to (LAMBDA (X) "").
  275. ;;;
  276. ;;; In other words, the following (simple, inefficient) definition holds:
  277. ;;; (define (string-unfold p f g seed base make-final)
  278. ;;; (string-append base
  279. ;;; (let recur ((seed seed))
  280. ;;; (if (p seed) (make-final seed)
  281. ;;; (string-append (string (f seed))
  282. ;;; (recur (g seed)))))))
  283. ;;;
  284. ;;; STRING-UNFOLD is a fairly powerful constructor -- you can use it to
  285. ;;; reverse a string, copy a string, convert a list to a string, read
  286. ;;; a port into a string, and so forth. Examples:
  287. ;;; (port->string port) =
  288. ;;; (string-unfold (compose eof-object? peek-char)
  289. ;;; read-char values port)
  290. ;;;
  291. ;;; (list->string lis) = (string-unfold null? car cdr lis)
  292. ;;;
  293. ;;; (tabulate-string f size) = (string-unfold (lambda (i) (= i size)) f add1 0)
  294. ;;; A problem with the following simple formulation is that it pushes one
  295. ;;; stack frame for every char in the result string -- an issue if you are
  296. ;;; using it to read a 100kchar string. So we don't use it -- but I include
  297. ;;; it to give a clear, straightforward description of what the function
  298. ;;; does.
  299. ;(define (string-unfold p f g seed base make-final)
  300. ; (let ((ans (let recur ((seed seed) (i (string-length base)))
  301. ; (if (p seed)
  302. ; (let* ((final (make-final seed))
  303. ; (ans (make-string (+ i (string-length final)))))
  304. ; (string-copy! ans i final)
  305. ; ans)
  306. ;
  307. ; (let* ((c (f seed))
  308. ; (s (recur (g seed) (+ i 1))))
  309. ; (string-set! s i c)
  310. ; s)))))
  311. ; (string-copy! ans 0 base)
  312. ; ans))
  313. ;;; The strategy is to allocate a series of chunks into which we stash the
  314. ;;; chars as we generate them. Chunk size goes up in powers of two starting
  315. ;;; with 40 and levelling out at 4k, i.e.
  316. ;;; 40 40 80 160 320 640 1280 2560 4096 4096 4096 4096 4096...
  317. ;;; This should work pretty well for short strings, 1-line (80 char) strings,
  318. ;;; and longer ones. When done, we allocate an answer string and copy the
  319. ;;; chars over from the chunk buffers.
  320. (define (string-unfold p f g seed . base+make-final)
  321. (check-arg procedure? p string-unfold)
  322. (check-arg procedure? f string-unfold)
  323. (check-arg procedure? g string-unfold)
  324. (let-optionals* base+make-final
  325. ((base "" (string? base))
  326. (make-final (lambda (x) "") (procedure? make-final)))
  327. (let lp ((chunks '()) ; Previously filled chunks
  328. (nchars 0) ; Number of chars in CHUNKS
  329. (chunk (make-string 40)) ; Current chunk into which we write
  330. (chunk-len 40)
  331. (i 0) ; Number of chars written into CHUNK
  332. (seed seed))
  333. (let lp2 ((i i) (seed seed))
  334. (if (not (p seed))
  335. (let ((c (f seed))
  336. (seed (g seed)))
  337. (if (< i chunk-len)
  338. (begin (string-set! chunk i c)
  339. (lp2 (+ i 1) seed))
  340. (let* ((nchars2 (+ chunk-len nchars))
  341. (chunk-len2 (min 4096 nchars2))
  342. (new-chunk (make-string chunk-len2)))
  343. (string-set! new-chunk 0 c)
  344. (lp (cons chunk chunks) (+ nchars chunk-len)
  345. new-chunk chunk-len2 1 seed))))
  346. ;; We're done. Make the answer string & install the bits.
  347. (let* ((final (make-final seed))
  348. (flen (string-length final))
  349. (base-len (string-length base))
  350. (j (+ base-len nchars i))
  351. (ans (make-string (+ j flen))))
  352. (%string-copy! ans j final 0 flen) ; Install FINAL.
  353. (let ((j (- j i)))
  354. (%string-copy! ans j chunk 0 i) ; Install CHUNK[0,I).
  355. (let lp ((j j) (chunks chunks)) ; Install CHUNKS.
  356. (if (pair? chunks)
  357. (let* ((chunk (car chunks))
  358. (chunks (cdr chunks))
  359. (chunk-len (string-length chunk))
  360. (j (- j chunk-len)))
  361. (%string-copy! ans j chunk 0 chunk-len)
  362. (lp j chunks)))))
  363. (%string-copy! ans 0 base 0 base-len) ; Install BASE.
  364. ans))))))
  365. (define (string-unfold-right p f g seed . base+make-final)
  366. (let-optionals* base+make-final
  367. ((base "" (string? base))
  368. (make-final (lambda (x) "") (procedure? make-final)))
  369. (let lp ((chunks '()) ; Previously filled chunks
  370. (nchars 0) ; Number of chars in CHUNKS
  371. (chunk (make-string 40)) ; Current chunk into which we write
  372. (chunk-len 40)
  373. (i 40) ; Number of chars available in CHUNK
  374. (seed seed))
  375. (let lp2 ((i i) (seed seed)) ; Fill up CHUNK from right
  376. (if (not (p seed)) ; to left.
  377. (let ((c (f seed))
  378. (seed (g seed)))
  379. (if (> i 0)
  380. (let ((i (- i 1)))
  381. (string-set! chunk i c)
  382. (lp2 i seed))
  383. (let* ((nchars2 (+ chunk-len nchars))
  384. (chunk-len2 (min 4096 nchars2))
  385. (new-chunk (make-string chunk-len2))
  386. (i (- chunk-len2 1)))
  387. (string-set! new-chunk i c)
  388. (lp (cons chunk chunks) (+ nchars chunk-len)
  389. new-chunk chunk-len2 i seed))))
  390. ;; We're done. Make the answer string & install the bits.
  391. (let* ((final (make-final seed))
  392. (flen (string-length final))
  393. (base-len (string-length base))
  394. (chunk-used (- chunk-len i))
  395. (j (+ base-len nchars chunk-used))
  396. (ans (make-string (+ j flen))))
  397. (%string-copy! ans 0 final 0 flen) ; Install FINAL.
  398. (%string-copy! ans flen chunk i chunk-len); Install CHUNK[I,).
  399. (let lp ((j (+ flen chunk-used)) ; Install CHUNKS.
  400. (chunks chunks))
  401. (if (pair? chunks)
  402. (let* ((chunk (car chunks))
  403. (chunks (cdr chunks))
  404. (chunk-len (string-length chunk)))
  405. (%string-copy! ans j chunk 0 chunk-len)
  406. (lp (+ j chunk-len) chunks))
  407. (%string-copy! ans j base 0 base-len))); Install BASE.
  408. ans))))))
  409. (define (string-for-each proc s . maybe-start+end)
  410. (check-arg procedure? proc string-for-each)
  411. (let-string-start+end (start end) string-for-each s maybe-start+end
  412. (let lp ((i start))
  413. (if (< i end)
  414. (begin (proc (string-ref s i))
  415. (lp (+ i 1)))))))
  416. (define (string-for-each-index proc s . maybe-start+end)
  417. (check-arg procedure? proc string-for-each-index)
  418. (let-string-start+end (start end) string-for-each-index s maybe-start+end
  419. (let lp ((i start))
  420. (if (< i end) (begin (proc i) (lp (+ i 1)))))))
  421. (define (string-every criterion s . maybe-start+end)
  422. (let-string-start+end (start end) string-every s maybe-start+end
  423. (cond ((char? criterion)
  424. (let lp ((i start))
  425. (or (>= i end)
  426. (and (char=? criterion (string-ref s i))
  427. (lp (+ i 1))))))
  428. ((char-set? criterion)
  429. (let lp ((i start))
  430. (or (>= i end)
  431. (and (char-set-contains? criterion (string-ref s i))
  432. (lp (+ i 1))))))
  433. ((procedure? criterion) ; Slightly funky loop so that
  434. (or (= start end) ; final (PRED S[END-1]) call
  435. (let lp ((i start)) ; is a tail call.
  436. (let ((c (string-ref s i))
  437. (i1 (+ i 1)))
  438. (if (= i1 end) (criterion c) ; Tail call.
  439. (and (criterion c) (lp i1)))))))
  440. (else (error "Second param is neither char-set, char, or predicate procedure."
  441. string-every criterion)))))
  442. (define (string-any criterion s . maybe-start+end)
  443. (let-string-start+end (start end) string-any s maybe-start+end
  444. (cond ((char? criterion)
  445. (let lp ((i start))
  446. (and (< i end)
  447. (or (char=? criterion (string-ref s i))
  448. (lp (+ i 1))))))
  449. ((char-set? criterion)
  450. (let lp ((i start))
  451. (and (< i end)
  452. (or (char-set-contains? criterion (string-ref s i))
  453. (lp (+ i 1))))))
  454. ((procedure? criterion) ; Slightly funky loop so that
  455. (and (< start end) ; final (PRED S[END-1]) call
  456. (let lp ((i start)) ; is a tail call.
  457. (let ((c (string-ref s i))
  458. (i1 (+ i 1)))
  459. (if (= i1 end) (criterion c) ; Tail call
  460. (or (criterion c) (lp i1)))))))
  461. (else (error "Second param is neither char-set, char, or predicate procedure."
  462. string-any criterion)))))
  463. (define (string-tabulate proc len)
  464. (check-arg procedure? proc string-tabulate)
  465. (check-arg (lambda (val) (and (integer? val) (exact? val) (<= 0 val)))
  466. len string-tabulate)
  467. (let ((s (make-string len)))
  468. (do ((i (- len 1) (- i 1)))
  469. ((< i 0))
  470. (string-set! s i (proc i)))
  471. s))
  472. ;;; string-prefix-length[-ci] s1 s2 [start1 end1 start2 end2]
  473. ;;; string-suffix-length[-ci] s1 s2 [start1 end1 start2 end2]
  474. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  475. ;;; Find the length of the common prefix/suffix.
  476. ;;; It is not required that the two substrings passed be of equal length.
  477. ;;; This was microcode in MIT Scheme -- a very tightly bummed primitive.
  478. ;;; %STRING-PREFIX-LENGTH is the core routine of all string-comparisons,
  479. ;;; so should be as tense as possible.
  480. (define (%string-prefix-length s1 start1 end1 s2 start2 end2)
  481. (let* ((delta (min (- end1 start1) (- end2 start2)))
  482. (end1 (+ start1 delta)))
  483. (if (and (eq? s1 s2) (= start1 start2)) ; EQ fast path
  484. delta
  485. (let lp ((i start1) (j start2)) ; Regular path
  486. (if (or (>= i end1)
  487. (not (char=? (string-ref s1 i)
  488. (string-ref s2 j))))
  489. (- i start1)
  490. (lp (+ i 1) (+ j 1)))))))
  491. (define (%string-suffix-length s1 start1 end1 s2 start2 end2)
  492. (let* ((delta (min (- end1 start1) (- end2 start2)))
  493. (start1 (- end1 delta)))
  494. (if (and (eq? s1 s2) (= end1 end2)) ; EQ fast path
  495. delta
  496. (let lp ((i (- end1 1)) (j (- end2 1))) ; Regular path
  497. (if (or (< i start1)
  498. (not (char=? (string-ref s1 i)
  499. (string-ref s2 j))))
  500. (- (- end1 i) 1)
  501. (lp (- i 1) (- j 1)))))))
  502. (define (%string-prefix-length-ci s1 start1 end1 s2 start2 end2)
  503. (let* ((delta (min (- end1 start1) (- end2 start2)))
  504. (end1 (+ start1 delta)))
  505. (if (and (eq? s1 s2) (= start1 start2)) ; EQ fast path
  506. delta
  507. (let lp ((i start1) (j start2)) ; Regular path
  508. (if (or (>= i end1)
  509. (not (char-ci=? (string-ref s1 i)
  510. (string-ref s2 j))))
  511. (- i start1)
  512. (lp (+ i 1) (+ j 1)))))))
  513. (define (%string-suffix-length-ci s1 start1 end1 s2 start2 end2)
  514. (let* ((delta (min (- end1 start1) (- end2 start2)))
  515. (start1 (- end1 delta)))
  516. (if (and (eq? s1 s2) (= end1 end2)) ; EQ fast path
  517. delta
  518. (let lp ((i (- end1 1)) (j (- end2 1))) ; Regular path
  519. (if (or (< i start1)
  520. (not (char-ci=? (string-ref s1 i)
  521. (string-ref s2 j))))
  522. (- (- end1 i) 1)
  523. (lp (- i 1) (- j 1)))))))
  524. (define (string-prefix-length s1 s2 . maybe-starts+ends)
  525. (let-string-start+end2 (start1 end1 start2 end2)
  526. string-prefix-length s1 s2 maybe-starts+ends
  527. (%string-prefix-length s1 start1 end1 s2 start2 end2)))
  528. (define (string-suffix-length s1 s2 . maybe-starts+ends)
  529. (let-string-start+end2 (start1 end1 start2 end2)
  530. string-suffix-length s1 s2 maybe-starts+ends
  531. (%string-suffix-length s1 start1 end1 s2 start2 end2)))
  532. (define (string-prefix-length-ci s1 s2 . maybe-starts+ends)
  533. (let-string-start+end2 (start1 end1 start2 end2)
  534. string-prefix-length-ci s1 s2 maybe-starts+ends
  535. (%string-prefix-length-ci s1 start1 end1 s2 start2 end2)))
  536. (define (string-suffix-length-ci s1 s2 . maybe-starts+ends)
  537. (let-string-start+end2 (start1 end1 start2 end2)
  538. string-suffix-length-ci s1 s2 maybe-starts+ends
  539. (%string-suffix-length-ci s1 start1 end1 s2 start2 end2)))
  540. ;;; string-prefix? s1 s2 [start1 end1 start2 end2]
  541. ;;; string-suffix? s1 s2 [start1 end1 start2 end2]
  542. ;;; string-prefix-ci? s1 s2 [start1 end1 start2 end2]
  543. ;;; string-suffix-ci? s1 s2 [start1 end1 start2 end2]
  544. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  545. ;;; These are all simple derivatives of the previous counting funs.
  546. (define (string-prefix? s1 s2 . maybe-starts+ends)
  547. (let-string-start+end2 (start1 end1 start2 end2)
  548. string-prefix? s1 s2 maybe-starts+ends
  549. (%string-prefix? s1 start1 end1 s2 start2 end2)))
  550. (define (string-suffix? s1 s2 . maybe-starts+ends)
  551. (let-string-start+end2 (start1 end1 start2 end2)
  552. string-suffix? s1 s2 maybe-starts+ends
  553. (%string-suffix? s1 start1 end1 s2 start2 end2)))
  554. (define (string-prefix-ci? s1 s2 . maybe-starts+ends)
  555. (let-string-start+end2 (start1 end1 start2 end2)
  556. string-prefix-ci? s1 s2 maybe-starts+ends
  557. (%string-prefix-ci? s1 start1 end1 s2 start2 end2)))
  558. (define (string-suffix-ci? s1 s2 . maybe-starts+ends)
  559. (let-string-start+end2 (start1 end1 start2 end2)
  560. string-suffix-ci? s1 s2 maybe-starts+ends
  561. (%string-suffix-ci? s1 start1 end1 s2 start2 end2)))
  562. ;;; Here are the internal routines that do the real work.
  563. (define (%string-prefix? s1 start1 end1 s2 start2 end2)
  564. (let ((len1 (- end1 start1)))
  565. (and (<= len1 (- end2 start2)) ; Quick check
  566. (= (%string-prefix-length s1 start1 end1
  567. s2 start2 end2)
  568. len1))))
  569. (define (%string-suffix? s1 start1 end1 s2 start2 end2)
  570. (let ((len1 (- end1 start1)))
  571. (and (<= len1 (- end2 start2)) ; Quick check
  572. (= len1 (%string-suffix-length s1 start1 end1
  573. s2 start2 end2)))))
  574. (define (%string-prefix-ci? s1 start1 end1 s2 start2 end2)
  575. (let ((len1 (- end1 start1)))
  576. (and (<= len1 (- end2 start2)) ; Quick check
  577. (= len1 (%string-prefix-length-ci s1 start1 end1
  578. s2 start2 end2)))))
  579. (define (%string-suffix-ci? s1 start1 end1 s2 start2 end2)
  580. (let ((len1 (- end1 start1)))
  581. (and (<= len1 (- end2 start2)) ; Quick check
  582. (= len1 (%string-suffix-length-ci s1 start1 end1
  583. s2 start2 end2)))))
  584. ;;; string-compare s1 s2 proc< proc= proc> [start1 end1 start2 end2]
  585. ;;; string-compare-ci s1 s2 proc< proc= proc> [start1 end1 start2 end2]
  586. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  587. ;;; Primitive string-comparison functions.
  588. ;;; Continuation order is different from MIT Scheme.
  589. ;;; Continuations are applied to s1's mismatch index;
  590. ;;; in the case of equality, this is END1.
  591. (define (%string-compare s1 start1 end1 s2 start2 end2
  592. proc< proc= proc>)
  593. (let ((size1 (- end1 start1))
  594. (size2 (- end2 start2)))
  595. (let ((match (%string-prefix-length s1 start1 end1 s2 start2 end2)))
  596. (if (= match size1)
  597. ((if (= match size2) proc= proc<) end1)
  598. ((if (= match size2)
  599. proc>
  600. (if (char<? (string-ref s1 (+ start1 match))
  601. (string-ref s2 (+ start2 match)))
  602. proc< proc>))
  603. (+ match start1))))))
  604. (define (%string-compare-ci s1 start1 end1 s2 start2 end2
  605. proc< proc= proc>)
  606. (let ((size1 (- end1 start1))
  607. (size2 (- end2 start2)))
  608. (let ((match (%string-prefix-length-ci s1 start1 end1 s2 start2 end2)))
  609. (if (= match size1)
  610. ((if (= match size2) proc= proc<) end1)
  611. ((if (= match size2) proc>
  612. (if (char-ci<? (string-ref s1 (+ start1 match))
  613. (string-ref s2 (+ start2 match)))
  614. proc< proc>))
  615. (+ start1 match))))))
  616. (define (string-compare s1 s2 proc< proc= proc> . maybe-starts+ends)
  617. (check-arg procedure? proc< string-compare)
  618. (check-arg procedure? proc= string-compare)
  619. (check-arg procedure? proc> string-compare)
  620. (let-string-start+end2 (start1 end1 start2 end2)
  621. string-compare s1 s2 maybe-starts+ends
  622. (%string-compare s1 start1 end1 s2 start2 end2 proc< proc= proc>)))
  623. (define (string-compare-ci s1 s2 proc< proc= proc> . maybe-starts+ends)
  624. (check-arg procedure? proc< string-compare-ci)
  625. (check-arg procedure? proc= string-compare-ci)
  626. (check-arg procedure? proc> string-compare-ci)
  627. (let-string-start+end2 (start1 end1 start2 end2)
  628. string-compare-ci s1 s2 maybe-starts+ends
  629. (%string-compare-ci s1 start1 end1 s2 start2 end2 proc< proc= proc>)))
  630. ;;; string= string<> string-ci= string-ci<>
  631. ;;; string< string> string-ci< string-ci>
  632. ;;; string<= string>= string-ci<= string-ci>=
  633. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  634. ;;; Simple definitions in terms of the previous comparison funs.
  635. ;;; I sure hope the %STRING-COMPARE calls get integrated.
  636. (define (string= s1 s2 . maybe-starts+ends)
  637. (let-string-start+end2 (start1 end1 start2 end2)
  638. string= s1 s2 maybe-starts+ends
  639. (and (= (- end1 start1) (- end2 start2)) ; Quick filter
  640. (or (and (eq? s1 s2) (= start1 start2)) ; Fast path
  641. (%string-compare s1 start1 end1 s2 start2 end2 ; Real test
  642. (lambda (i) #f)
  643. values
  644. (lambda (i) #f))))))
  645. (define (string<> s1 s2 . maybe-starts+ends)
  646. (let-string-start+end2 (start1 end1 start2 end2)
  647. string<> s1 s2 maybe-starts+ends
  648. (or (not (= (- end1 start1) (- end2 start2))) ; Fast path
  649. (and (not (and (eq? s1 s2) (= start1 start2))) ; Quick filter
  650. (%string-compare s1 start1 end1 s2 start2 end2 ; Real test
  651. values
  652. (lambda (i) #f)
  653. values)))))
  654. (define (string< s1 s2 . maybe-starts+ends)
  655. (let-string-start+end2 (start1 end1 start2 end2)
  656. string< s1 s2 maybe-starts+ends
  657. (if (and (eq? s1 s2) (= start1 start2)) ; Fast path
  658. (< end1 end2)
  659. (%string-compare s1 start1 end1 s2 start2 end2 ; Real test
  660. values
  661. (lambda (i) #f)
  662. (lambda (i) #f)))))
  663. (define (string> s1 s2 . maybe-starts+ends)
  664. (let-string-start+end2 (start1 end1 start2 end2)
  665. string> s1 s2 maybe-starts+ends
  666. (if (and (eq? s1 s2) (= start1 start2)) ; Fast path
  667. (> end1 end2)
  668. (%string-compare s1 start1 end1 s2 start2 end2 ; Real test
  669. (lambda (i) #f)
  670. (lambda (i) #f)
  671. values))))
  672. (define (string<= s1 s2 . maybe-starts+ends)
  673. (let-string-start+end2 (start1 end1 start2 end2)
  674. string<= s1 s2 maybe-starts+ends
  675. (if (and (eq? s1 s2) (= start1 start2)) ; Fast path
  676. (<= end1 end2)
  677. (%string-compare s1 start1 end1 s2 start2 end2 ; Real test
  678. values
  679. values
  680. (lambda (i) #f)))))
  681. (define (string>= s1 s2 . maybe-starts+ends)
  682. (let-string-start+end2 (start1 end1 start2 end2)
  683. string>= s1 s2 maybe-starts+ends
  684. (if (and (eq? s1 s2) (= start1 start2)) ; Fast path
  685. (>= end1 end2)
  686. (%string-compare s1 start1 end1 s2 start2 end2 ; Real test
  687. (lambda (i) #f)
  688. values
  689. values))))
  690. (define (string-ci= s1 s2 . maybe-starts+ends)
  691. (let-string-start+end2 (start1 end1 start2 end2)
  692. string-ci= s1 s2 maybe-starts+ends
  693. (and (= (- end1 start1) (- end2 start2)) ; Quick filter
  694. (or (and (eq? s1 s2) (= start1 start2)) ; Fast path
  695. (%string-compare-ci s1 start1 end1 s2 start2 end2 ; Real test
  696. (lambda (i) #f)
  697. values
  698. (lambda (i) #f))))))
  699. (define (string-ci<> s1 s2 . maybe-starts+ends)
  700. (let-string-start+end2 (start1 end1 start2 end2)
  701. string-ci<> s1 s2 maybe-starts+ends
  702. (or (not (= (- end1 start1) (- end2 start2))) ; Fast path
  703. (and (not (and (eq? s1 s2) (= start1 start2))) ; Quick filter
  704. (%string-compare-ci s1 start1 end1 s2 start2 end2 ; Real test
  705. values
  706. (lambda (i) #f)
  707. values)))))
  708. (define (string-ci< s1 s2 . maybe-starts+ends)
  709. (let-string-start+end2 (start1 end1 start2 end2)
  710. string-ci< s1 s2 maybe-starts+ends
  711. (if (and (eq? s1 s2) (= start1 start2)) ; Fast path
  712. (< end1 end2)
  713. (%string-compare-ci s1 start1 end1 s2 start2 end2 ; Real test
  714. values
  715. (lambda (i) #f)
  716. (lambda (i) #f)))))
  717. (define (string-ci> s1 s2 . maybe-starts+ends)
  718. (let-string-start+end2 (start1 end1 start2 end2)
  719. string-ci> s1 s2 maybe-starts+ends
  720. (if (and (eq? s1 s2) (= start1 start2)) ; Fast path
  721. (> end1 end2)
  722. (%string-compare-ci s1 start1 end1 s2 start2 end2 ; Real test
  723. (lambda (i) #f)
  724. (lambda (i) #f)
  725. values))))
  726. (define (string-ci<= s1 s2 . maybe-starts+ends)
  727. (let-string-start+end2 (start1 end1 start2 end2)
  728. string-ci<= s1 s2 maybe-starts+ends
  729. (if (and (eq? s1 s2) (= start1 start2)) ; Fast path
  730. (<= end1 end2)
  731. (%string-compare-ci s1 start1 end1 s2 start2 end2 ; Real test
  732. values
  733. values
  734. (lambda (i) #f)))))
  735. (define (string-ci>= s1 s2 . maybe-starts+ends)
  736. (let-string-start+end2 (start1 end1 start2 end2)
  737. string-ci>= s1 s2 maybe-starts+ends
  738. (if (and (eq? s1 s2) (= start1 start2)) ; Fast path
  739. (>= end1 end2)
  740. (%string-compare-ci s1 start1 end1 s2 start2 end2 ; Real test
  741. (lambda (i) #f)
  742. values
  743. values))))
  744. ;;; Hash
  745. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  746. ;;; Compute (c + 37 c + 37^2 c + ...) modulo BOUND, with sleaze thrown in
  747. ;;; to keep the intermediate values small. (We do the calculation with just
  748. ;;; enough bits to represent BOUND, masking off high bits at each step in
  749. ;;; calculation. If this screws up any important properties of the hash
  750. ;;; function I'd like to hear about it. -Olin)
  751. ;;;
  752. ;;; If you keep BOUND small enough, the intermediate calculations will
  753. ;;; always be fixnums. How small is dependent on the underlying Scheme system;
  754. ;;; we use a default BOUND of 2^22 = 4194304, which should hack it in
  755. ;;; Schemes that give you at least 29 signed bits for fixnums. The core
  756. ;;; calculation that you don't want to overflow is, worst case,
  757. ;;; (+ 65535 (* 37 (- bound 1)))
  758. ;;; where 65535 is the max character code. Choose the default BOUND to be the
  759. ;;; biggest power of two that won't cause this expression to fixnum overflow,
  760. ;;; and everything will be copacetic.
  761. (define (%string-hash s char->int bound start end)
  762. (let ((iref (lambda (s i) (char->int (string-ref s i))))
  763. ;; Compute a 111...1 mask that will cover BOUND-1:
  764. (mask (let lp ((i #x10000)) ; Let's skip first 16 iterations, eh?
  765. (if (>= i bound) (- i 1) (lp (+ i i))))))
  766. (let lp ((i start) (ans 0))
  767. (if (>= i end) (modulo ans bound)
  768. (lp (+ i 1) (bitwise-and mask (+ (* 37 ans) (iref s i))))))))
  769. (define (string-hash s . maybe-bound+start+end)
  770. (let-optionals* maybe-bound+start+end ((bound 4194304 (and (integer? bound)
  771. (exact? bound)
  772. (<= 0 bound)))
  773. rest)
  774. (let ((bound (if (zero? bound) 4194304 bound))) ; 0 means default.
  775. (let-string-start+end (start end) string-hash s rest
  776. (%string-hash s char->integer bound start end)))))
  777. (define (string-hash-ci s . maybe-bound+start+end)
  778. (let-optionals* maybe-bound+start+end ((bound 4194304 (and (integer? bound)
  779. (exact? bound)
  780. (<= 0 bound)))
  781. rest)
  782. (let ((bound (if (zero? bound) 4194304 bound))) ; 0 means default.
  783. (let-string-start+end (start end) string-hash-ci s rest
  784. (%string-hash s (lambda (c) (char->integer (char-downcase c)))
  785. bound start end)))))
  786. ;;; Case hacking
  787. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  788. ;;; string-upcase s [start end]
  789. ;;; string-upcase! s [start end]
  790. ;;; string-downcase s [start end]
  791. ;;; string-downcase! s [start end]
  792. ;;;
  793. ;;; string-titlecase s [start end]
  794. ;;; string-titlecase! s [start end]
  795. ;;; Capitalize every contiguous alpha sequence: capitalise
  796. ;;; first char, lowercase rest.
  797. (define (string-upcase s . maybe-start+end)
  798. (let-string-start+end (start end) string-upcase s maybe-start+end
  799. (%string-map char-upcase s start end)))
  800. (define (string-upcase! s . maybe-start+end)
  801. (let-string-start+end (start end) string-upcase! s maybe-start+end
  802. (%string-map! char-upcase s start end)))
  803. (define (string-downcase s . maybe-start+end)
  804. (let-string-start+end (start end) string-downcase s maybe-start+end
  805. (%string-map char-downcase s start end)))
  806. (define (string-downcase! s . maybe-start+end)
  807. (let-string-start+end (start end) string-downcase! s maybe-start+end
  808. (%string-map! char-downcase s start end)))
  809. (define (%string-titlecase! s start end)
  810. (let lp ((i start))
  811. (cond ((string-index s char-cased? i end) =>
  812. (lambda (i)
  813. (string-set! s i (char-titlecase (string-ref s i)))
  814. (let ((i1 (+ i 1)))
  815. (cond ((string-skip s char-cased? i1 end) =>
  816. (lambda (j)
  817. (string-downcase! s i1 j)
  818. (lp (+ j 1))))
  819. (else (string-downcase! s i1 end)))))))))
  820. (define (string-titlecase! s . maybe-start+end)
  821. (let-string-start+end (start end) string-titlecase! s maybe-start+end
  822. (%string-titlecase! s start end)))
  823. (define (string-titlecase s . maybe-start+end)
  824. (let-string-start+end (start end) string-titlecase! s maybe-start+end
  825. (let ((ans (substring s start end)))
  826. (%string-titlecase! ans 0 (- end start))
  827. ans)))
  828. ;;; Cutting & pasting strings
  829. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  830. ;;; string-take string nchars
  831. ;;; string-drop string nchars
  832. ;;;
  833. ;;; string-take-right string nchars
  834. ;;; string-drop-right string nchars
  835. ;;;
  836. ;;; string-pad string k [char start end]
  837. ;;; string-pad-right string k [char start end]
  838. ;;;
  839. ;;; string-trim string [char/char-set/pred start end]
  840. ;;; string-trim-right string [char/char-set/pred start end]
  841. ;;; string-trim-both string [char/char-set/pred start end]
  842. ;;;
  843. ;;; These trimmers invert the char-set meaning from MIT Scheme -- you
  844. ;;; say what you want to trim.
  845. (define (string-take s n)
  846. (check-arg string? s string-take)
  847. (check-arg (lambda (val) (and (integer? n) (exact? n)
  848. (<= 0 n (string-length s))))
  849. n string-take)
  850. (%substring/shared s 0 n))
  851. (define (string-take-right s n)
  852. (check-arg string? s string-take-right)
  853. (let ((len (string-length s)))
  854. (check-arg (lambda (val) (and (integer? n) (exact? n) (<= 0 n len)))
  855. n string-take-right)
  856. (%substring/shared s (- len n) len)))
  857. (define (string-drop s n)
  858. (check-arg string? s string-drop)
  859. (let ((len (string-length s)))
  860. (check-arg (lambda (val) (and (integer? n) (exact? n) (<= 0 n len)))
  861. n string-drop)
  862. (%substring/shared s n len)))
  863. (define (string-drop-right s n)
  864. (check-arg string? s string-drop-right)
  865. (let ((len (string-length s)))
  866. (check-arg (lambda (val) (and (integer? n) (exact? n) (<= 0 n len)))
  867. n string-drop-right)
  868. (%substring/shared s 0 (- len n))))
  869. (define (string-trim s . criterion+start+end)
  870. (let-optionals* criterion+start+end ((criterion char-set:whitespace) rest)
  871. (let-string-start+end (start end) string-trim s rest
  872. (cond ((string-skip s criterion start end) =>
  873. (lambda (i) (%substring/shared s i end)))
  874. (else "")))))
  875. (define (string-trim-right s . criterion+start+end)
  876. (let-optionals* criterion+start+end ((criterion char-set:whitespace) rest)
  877. (let-string-start+end (start end) string-trim-right s rest
  878. (cond ((string-skip-right s criterion start end) =>
  879. (lambda (i) (%substring/shared s start (+ 1 i))))
  880. (else "")))))
  881. (define (string-trim-both s . criterion+start+end)
  882. (let-optionals* criterion+start+end ((criterion char-set:whitespace) rest)
  883. (let-string-start+end (start end) string-trim-both s rest
  884. (cond ((string-skip s criterion start end) =>
  885. (lambda (i)
  886. (%substring/shared s i (+ 1 (string-skip-right s criterion i end)))))
  887. (else "")))))
  888. (define (string-pad-right s n . char+start+end)
  889. (let-optionals* char+start+end ((char #\space (char? char)) rest)
  890. (let-string-start+end (start end) string-pad-right s rest
  891. (check-arg (lambda (n) (and (integer? n) (exact? n) (<= 0 n)))
  892. n string-pad-right)
  893. (let ((len (- end start)))
  894. (if (<= n len)
  895. (%substring/shared s start (+ start n))
  896. (let ((ans (make-string n char)))
  897. (%string-copy! ans 0 s start end)
  898. ans))))))
  899. (define (string-pad s n . char+start+end)
  900. (let-optionals* char+start+end ((char #\space (char? char)) rest)
  901. (let-string-start+end (start end) string-pad s rest
  902. (check-arg (lambda (n) (and (integer? n) (exact? n) (<= 0 n)))
  903. n string-pad)
  904. (let ((len (- end start)))
  905. (if (<= n len)
  906. (%substring/shared s (- end n) end)
  907. (let ((ans (make-string n char)))
  908. (%string-copy! ans (- n len) s start end)
  909. ans))))))
  910. ;;; Filtering strings
  911. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  912. ;;; string-delete char/char-set/pred string [start end]
  913. ;;; string-filter char/char-set/pred string [start end]
  914. ;;;
  915. ;;; If the criterion is a char or char-set, we scan the string twice with
  916. ;;; string-fold -- once to determine the length of the result string,
  917. ;;; and once to do the filtered copy.
  918. ;;; If the criterion is a predicate, we don't do this double-scan strategy,
  919. ;;; because the predicate might have side-effects or be very expensive to
  920. ;;; compute. So we preallocate a temp buffer pessimistically, and only do
  921. ;;; one scan over S. This is likely to be faster and more space-efficient
  922. ;;; than consing a list.
  923. (define (string-delete criterion s . maybe-start+end)
  924. (let-string-start+end (start end) string-delete s maybe-start+end
  925. (if (procedure? criterion)
  926. (let* ((slen (- end start))
  927. (temp (make-string slen))
  928. (ans-len (string-fold (lambda (c i)
  929. (if (criterion c) i
  930. (begin (string-set! temp i c)
  931. (+ i 1))))
  932. 0 s start end)))
  933. (if (= ans-len slen) temp (substring temp 0 ans-len)))
  934. (let* ((cset (cond ((char-set? criterion) criterion)
  935. ((char? criterion) (char-set criterion))
  936. (else (error "string-delete criterion not predicate, char or char-set" criterion))))
  937. (len (string-fold (lambda (c i) (if (char-set-contains? cset c)
  938. i
  939. (+ i 1)))
  940. 0 s start end))
  941. (ans (make-string len)))
  942. (string-fold (lambda (c i) (if (char-set-contains? cset c)
  943. i
  944. (begin (string-set! ans i c)
  945. (+ i 1))))
  946. 0 s start end)
  947. ans))))
  948. (define (string-filter criterion s . maybe-start+end)
  949. (let-string-start+end (start end) string-filter s maybe-start+end
  950. (if (procedure? criterion)
  951. (let* ((slen (- end start))
  952. (temp (make-string slen))
  953. (ans-len (string-fold (lambda (c i)
  954. (if (criterion c)
  955. (begin (string-set! temp i c)
  956. (+ i 1))
  957. i))
  958. 0 s start end)))
  959. (if (= ans-len slen) temp (substring temp 0 ans-len)))
  960. (let* ((cset (cond ((char-set? criterion) criterion)
  961. ((char? criterion) (char-set criterion))
  962. (else (error "string-delete criterion not predicate, char or char-set" criterion))))
  963. (len (string-fold (lambda (c i) (if (char-set-contains? cset c)
  964. (+ i 1)
  965. i))
  966. 0 s start end))
  967. (ans (make-string len)))
  968. (string-fold (lambda (c i) (if (char-set-contains? cset c)
  969. (begin (string-set! ans i c)
  970. (+ i 1))
  971. i))
  972. 0 s start end)
  973. ans))))
  974. ;;; String search
  975. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  976. ;;; string-index string char/char-set/pred [start end]
  977. ;;; string-index-right string char/char-set/pred [start end]
  978. ;;; string-skip string char/char-set/pred [start end]
  979. ;;; string-skip-right string char/char-set/pred [start end]
  980. ;;; string-count string char/char-set/pred [start end]
  981. ;;; There's a lot of replicated code here for efficiency.
  982. ;;; For example, the char/char-set/pred discrimination has
  983. ;;; been lifted above the inner loop of each proc.
  984. (define (string-index str criterion . maybe-start+end)
  985. (let-string-start+end (start end) string-index str maybe-start+end
  986. (cond ((char? criterion)
  987. (let lp ((i start))
  988. (and (< i end)
  989. (if (char=? criterion (string-ref str i)) i
  990. (lp (+ i 1))))))
  991. ((char-set? criterion)
  992. (let lp ((i start))
  993. (and (< i end)
  994. (if (char-set-contains? criterion (string-ref str i)) i
  995. (lp (+ i 1))))))
  996. ((procedure? criterion)
  997. (let lp ((i start))
  998. (and (< i end)
  999. (if (criterion (string-ref str i)) i
  1000. (lp (+ i 1))))))
  1001. (else (error "Second param is neither char-set, char, or predicate procedure."
  1002. string-index criterion)))))
  1003. (define (string-index-right str criterion . maybe-start+end)
  1004. (let-string-start+end (start end) string-index-right str maybe-start+end
  1005. (cond ((char? criterion)
  1006. (let lp ((i (- end 1)))
  1007. (and (>= i start)
  1008. (if (char=? criterion (string-ref str i)) i
  1009. (lp (- i 1))))))
  1010. ((char-set? criterion)
  1011. (let lp ((i (- end 1)))
  1012. (and (>= i start)
  1013. (if (char-set-contains? criterion (string-ref str i)) i
  1014. (lp (- i 1))))))
  1015. ((procedure? criterion)
  1016. (let lp ((i (- end 1)))
  1017. (and (>= i start)
  1018. (if (criterion (string-ref str i)) i
  1019. (lp (- i 1))))))
  1020. (else (error "Second param is neither char-set, char, or predicate procedure."
  1021. string-index-right criterion)))))
  1022. (define (string-skip str criterion . maybe-start+end)
  1023. (let-string-start+end (start end) string-skip str maybe-start+end
  1024. (cond ((char? criterion)
  1025. (let lp ((i start))
  1026. (and (< i end)
  1027. (if (char=? criterion (string-ref str i))
  1028. (lp (+ i 1))
  1029. i))))
  1030. ((char-set? criterion)
  1031. (let lp ((i start))
  1032. (and (< i end)
  1033. (if (char-set-contains? criterion (string-ref str i))
  1034. (lp (+ i 1))
  1035. i))))
  1036. ((procedure? criterion)
  1037. (let lp ((i start))
  1038. (and (< i end)
  1039. (if (criterion (string-ref str i)) (lp (+ i 1))
  1040. i))))
  1041. (else (error "Second param is neither char-set, char, or predicate procedure."
  1042. string-skip criterion)))))
  1043. (define (string-skip-right str criterion . maybe-start+end)
  1044. (let-string-start+end (start end) string-skip-right str maybe-start+end
  1045. (cond ((char? criterion)
  1046. (let lp ((i (- end 1)))
  1047. (and (>= i start)
  1048. (if (char=? criterion (string-ref str i))
  1049. (lp (- i 1))
  1050. i))))
  1051. ((char-set? criterion)
  1052. (let lp ((i (- end 1)))
  1053. (and (>= i start)
  1054. (if (char-set-contains? criterion (string-ref str i))
  1055. (lp (- i 1))
  1056. i))))
  1057. ((procedure? criterion)
  1058. (let lp ((i (- end 1)))
  1059. (and (>= i start)
  1060. (if (criterion (string-ref str i)) (lp (- i 1))
  1061. i))))
  1062. (else (error "CRITERION param is neither char-set or char."
  1063. string-skip-right criterion)))))
  1064. (define (string-count s criterion . maybe-start+end)
  1065. (let-string-start+end (start end) string-count s maybe-start+end
  1066. (cond ((char? criterion)
  1067. (do ((i start (+ i 1))
  1068. (count 0 (if (char=? criterion (string-ref s i))
  1069. (+ count 1)
  1070. count)))
  1071. ((>= i end) count)))
  1072. ((char-set? criterion)
  1073. (do ((i start (+ i 1))
  1074. (count 0 (if (char-set-contains? criterion (string-ref s i))
  1075. (+ count 1)
  1076. count)))
  1077. ((>= i end) count)))
  1078. ((procedure? criterion)
  1079. (do ((i start (+ i 1))
  1080. (count 0 (if (criterion (string-ref s i)) (+ count 1) count)))
  1081. ((>= i end) count)))
  1082. (else (error "CRITERION param is neither char-set or char."
  1083. string-count criterion)))))
  1084. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1085. ;;; string-fill! string char [start end]
  1086. ;;;
  1087. ;;; string-copy! to tstart from [fstart fend]
  1088. ;;; Guaranteed to work, even if s1 eq s2.
  1089. (define (string-fill! s char . maybe-start+end)
  1090. (check-arg char? char string-fill!)
  1091. (let-string-start+end (start end) string-fill! s maybe-start+end
  1092. (do ((i (- end 1) (- i 1)))
  1093. ((< i start))
  1094. (string-set! s i char))))
  1095. (define (string-copy! to tstart from . maybe-fstart+fend)
  1096. (let-string-start+end (fstart fend) string-copy! from maybe-fstart+fend
  1097. (check-arg integer? tstart string-copy!)
  1098. (check-substring-spec string-copy! to tstart (+ tstart (- fend fstart)))
  1099. (%string-copy! to tstart from fstart fend)))
  1100. ;;; Library-internal routine
  1101. (define (%string-copy! to tstart from fstart fend)
  1102. (if (> fstart tstart)
  1103. (do ((i fstart (+ i 1))
  1104. (j tstart (+ j 1)))
  1105. ((>= i fend))
  1106. (string-set! to j (string-ref from i)))
  1107. (do ((i (- fend 1) (- i 1))
  1108. (j (+ -1 tstart (- fend fstart)) (- j 1)))
  1109. ((< i fstart))
  1110. (string-set! to j (string-ref from i)))))
  1111. ;;; Returns starting-position in STRING or #f if not true.
  1112. ;;; This implementation is slow & simple. It is useful as a "spec" or for
  1113. ;;; comparison testing with fancier implementations.
  1114. ;;; See below for fast KMP version.
  1115. ;(define (string-contains string substring . maybe-starts+ends)
  1116. ; (let-string-start+end2 (start1 end1 start2 end2)
  1117. ; string-contains string substring maybe-starts+ends
  1118. ; (let* ((len (- end2 start2))
  1119. ; (i-bound (- end1 len)))
  1120. ; (let lp ((i start1))
  1121. ; (and (< i i-bound)
  1122. ; (if (string= string substring i (+ i len) start2 end2)
  1123. ; i
  1124. ; (lp (+ i 1))))))))
  1125. ;;; Searching for an occurrence of a substring
  1126. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1127. (define (string-contains text pattern . maybe-starts+ends)
  1128. (let-string-start+end2 (t-start t-end p-start p-end)
  1129. string-contains text pattern maybe-starts+ends
  1130. (%kmp-search pattern text char=? p-start p-end t-start t-end)))
  1131. (define (string-contains-ci text pattern . maybe-starts+ends)
  1132. (let-string-start+end2 (t-start t-end p-start p-end)
  1133. string-contains-ci text pattern maybe-starts+ends
  1134. (%kmp-search pattern text char-ci=? p-start p-end t-start t-end)))
  1135. ;;; Knuth-Morris-Pratt string searching
  1136. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1137. ;;; See
  1138. ;;; "Fast pattern matching in strings"
  1139. ;;; SIAM J. Computing 6(2):323-350 1977
  1140. ;;; D. E. Knuth, J. H. Morris and V. R. Pratt
  1141. ;;; also described in
  1142. ;;; "Pattern matching in strings"
  1143. ;;; Alfred V. Aho
  1144. ;;; Formal Language Theory - Perspectives and Open Problems
  1145. ;;; Ronald V. Brook (editor)
  1146. ;;; This algorithm is O(m + n) where m and n are the
  1147. ;;; lengths of the pattern and string respectively
  1148. ;;; KMP search source[start,end) for PATTERN. Return starting index of
  1149. ;;; leftmost match or #f.
  1150. (define (%kmp-search pattern text c= p-start p-end t-start t-end)
  1151. (let ((plen (- p-end p-start))
  1152. (rv (make-kmp-restart-vector pattern c= p-start p-end)))
  1153. ;; The search loop. TJ & PJ are redundant state.
  1154. (let lp ((ti t-start) (pi 0)
  1155. (tj (- t-end t-start)) ; (- tlen ti) -- how many chars left.
  1156. (pj plen)) ; (- plen pi) -- how many chars left.
  1157. (if (= pi plen)
  1158. (- ti plen) ; Win.
  1159. (and (<= pj tj) ; Lose.
  1160. (if (c= (string-ref text ti) ; Search.
  1161. (string-ref pattern (+ p-start pi)))
  1162. (lp (+ 1 ti) (+ 1 pi) (- tj 1) (- pj 1)) ; Advance.
  1163. (let ((pi (vector-ref rv pi))) ; Retreat.
  1164. (if (= pi -1)
  1165. (lp (+ ti 1) 0 (- tj 1) plen) ; Punt.
  1166. (lp ti pi tj (- plen pi))))))))))
  1167. ;;; (make-kmp-restart-vector pattern [c= start end]) -> integer-vector
  1168. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1169. ;;; Compute the KMP restart vector RV for string PATTERN. If
  1170. ;;; we have matched chars 0..i-1 of PATTERN against a search string S, and
  1171. ;;; PATTERN[i] doesn't match S[k], then reset i := RV[i], and try again to
  1172. ;;; match S[k]. If RV[i] = -1, then punt S[k] completely, and move on to
  1173. ;;; S[k+1] and PATTERN[0] -- no possible match of PAT[0..i] contains S[k].
  1174. ;;;
  1175. ;;; In other words, if you have matched the first i chars of PATTERN, but
  1176. ;;; the i+1'th char doesn't match, RV[i] tells you what the next-longest
  1177. ;;; prefix of PATTERN is that you have matched.
  1178. ;;;
  1179. ;;; - C= (default CHAR=?) is used to compare characters for equality.
  1180. ;;; Pass in CHAR-CI=? for case-folded string search.
  1181. ;;;
  1182. ;;; - START & END restrict the pattern to the indicated substring; the
  1183. ;;; returned vector will be of length END - START. The numbers stored
  1184. ;;; in the vector will be values in the range [0,END-START) -- that is,
  1185. ;;; they are valid indices into the restart vector; you have to add START
  1186. ;;; to them to use them as indices into PATTERN.
  1187. ;;;
  1188. ;;; I've split this out as a separate function in case other constant-string
  1189. ;;; searchers might want to use it.
  1190. ;;;
  1191. ;;; E.g.:
  1192. ;;; a b d a b x
  1193. ;;; #(-1 0 0 -1 1 2)
  1194. (define (make-kmp-restart-vector pattern . maybe-c=+start+end)
  1195. (let-optionals* maybe-c=+start+end
  1196. ((c= char=? (procedure? c=))
  1197. ((start end) (lambda (args)
  1198. (string-parse-start+end make-kmp-restart-vector
  1199. pattern args))))
  1200. (let* ((rvlen (- end start))
  1201. (rv (make-vector rvlen -1)))
  1202. (if (> rvlen 0)
  1203. (let ((rvlen-1 (- rvlen 1))
  1204. (c0 (string-ref pattern start)))
  1205. ;; Here's the main loop. We have set rv[0] ... rv[i].
  1206. ;; K = I + START -- it is the corresponding index into PATTERN.
  1207. (let lp1 ((i 0) (j -1) (k start))
  1208. (if (< i rvlen-1)
  1209. ;; lp2 invariant:
  1210. ;; pat[(k-j) .. k-1] matches pat[start .. start+j-1]
  1211. ;; or j = -1.
  1212. (let lp2 ((j j))
  1213. (cond ((= j -1)
  1214. (let ((i1 (+ 1 i)))
  1215. (if (not (c= (string-ref pattern (+ k 1)) c0))
  1216. (vector-set! rv i1 0))
  1217. (lp1 i1 0 (+ k 1))))
  1218. ;; pat[(k-j) .. k] matches pat[start..start+j].
  1219. ((c= (string-ref pattern k) (string-ref pattern (+ j start)))
  1220. (let* ((i1 (+ 1 i))
  1221. (j1 (+ 1 j)))
  1222. (vector-set! rv i1 j1)
  1223. (lp1 i1 j1 (+ k 1))))
  1224. (else (lp2 (vector-ref rv j)))))))))
  1225. rv)))
  1226. ;;; We've matched I chars from PAT. C is the next char from the search string.
  1227. ;;; Return the new I after handling C.
  1228. ;;;
  1229. ;;; The pattern is (VECTOR-LENGTH RV) chars long, beginning at index PAT-START
  1230. ;;; in PAT (PAT-START is usually 0). The I chars of the pattern we've matched
  1231. ;;; are
  1232. ;;; PAT[PAT-START .. PAT-START + I].
  1233. ;;;
  1234. ;;; It's *not* an oversight that there is no friendly error checking or
  1235. ;;; defaulting of arguments. This is a low-level, inner-loop procedure
  1236. ;;; that we want integrated/inlined into the point of call.
  1237. (define (kmp-step pat rv c i c= p-start)
  1238. (let lp ((i i))
  1239. (if (c= c (string-ref pat (+ i p-start))) ; Match =>
  1240. (+ i 1) ; Done.
  1241. (let ((i (vector-ref rv i))) ; Back up in PAT.
  1242. (if (= i -1) 0 ; Can't back up further.
  1243. (lp i)))))) ; Keep trying for match.
  1244. ;;; Zip through S[start,end), looking for a match of PAT. Assume we've
  1245. ;;; already matched the first I chars of PAT when we commence at S[start].
  1246. ;;; - <0: If we find a match *ending* at index J, return -J.
  1247. ;;; - >=0: If we get to the end of the S[start,end) span without finding
  1248. ;;; a complete match, return the number of chars from PAT we'd matched
  1249. ;;; when we ran off the end.
  1250. ;;;
  1251. ;;; This is useful for searching *across* buffers -- that is, when your
  1252. ;;; input comes in chunks of text. We hand-integrate the KMP-STEP loop
  1253. ;;; for speed.
  1254. (define (string-kmp-partial-search pat rv s i . c=+p-start+s-start+s-end)
  1255. (check-arg vector? rv string-kmp-partial-search)
  1256. (let-optionals* c=+p-start+s-start+s-end
  1257. ((c= char=? (procedure? c=))
  1258. (p-start 0 (and (integer? p-start) (exact? p-start) (<= 0 p-start)))
  1259. ((s-start s-end) (lambda (args)
  1260. (string-parse-start+end string-kmp-partial-search
  1261. s args))))
  1262. (let ((patlen (vector-length rv)))
  1263. (check-arg (lambda (i) (and (integer? i) (exact? i) (<= 0 i) (< i patlen)))
  1264. i string-kmp-partial-search)
  1265. ;; Enough prelude. Here's the actual code.
  1266. (let lp ((si s-start) ; An index into S.
  1267. (vi i)) ; An index into RV.
  1268. (cond ((= vi patlen) (- si)) ; Win.
  1269. ((= si s-end) vi) ; Ran off the end.
  1270. (else ; Match s[si] & loop.
  1271. (let ((c (string-ref s si)))
  1272. (lp (+ si 1)
  1273. (let lp2 ((vi vi)) ; This is just KMP-STEP.
  1274. (if (c= c (string-ref pat (+ vi p-start)))
  1275. (+ vi 1)
  1276. (let ((vi (vector-ref rv vi)))
  1277. (if (= vi -1) 0
  1278. (lp2 vi)))))))))))))
  1279. ;;; Misc
  1280. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1281. ;;; (string-null? s)
  1282. ;;; (string-reverse s [start end])
  1283. ;;; (string-reverse! s [start end])
  1284. ;;; (reverse-list->string clist)
  1285. ;;; (string->list s [start end])
  1286. (define (string-null? s) (zero? (string-length s)))
  1287. (define (string-reverse s . maybe-start+end)
  1288. (let-string-start+end (start end) string-reverse s maybe-start+end
  1289. (let* ((len (- end start))
  1290. (ans (make-string len)))
  1291. (do ((i start (+ i 1))
  1292. (j (- len 1) (- j 1)))
  1293. ((< j 0))
  1294. (string-set! ans j (string-ref s i)))
  1295. ans)))
  1296. (define (string-reverse! s . maybe-start+end)
  1297. (let-string-start+end (start end) string-reverse! s maybe-start+end
  1298. (do ((i (- end 1) (- i 1))
  1299. (j start (+ j 1)))
  1300. ((<= i j))
  1301. (let ((ci (string-ref s i)))
  1302. (string-set! s i (string-ref s j))
  1303. (string-set! s j ci)))))
  1304. (define (reverse-list->string clist)
  1305. (let* ((len (length clist))
  1306. (s (make-string len)))
  1307. (do ((i (- len 1) (- i 1)) (clist clist (cdr clist)))
  1308. ((not (pair? clist)))
  1309. (string-set! s i (car clist)))
  1310. s))
  1311. ;(define (string->list s . maybe-start+end)
  1312. ; (apply string-fold-right cons '() s maybe-start+end))
  1313. (define (string->list s . maybe-start+end)
  1314. (let-string-start+end (start end) string->list s maybe-start+end
  1315. (do ((i (- end 1) (- i 1))
  1316. (ans '() (cons (string-ref s i) ans)))
  1317. ((< i start) ans))))
  1318. ;;; Defined by R5RS, so commented out here.
  1319. ;(define (list->string lis) (string-unfold null? car cdr lis))
  1320. ;;; string-concatenate string-list -> string
  1321. ;;; string-concatenate/shared string-list -> string
  1322. ;;; string-append/shared s ... -> string
  1323. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1324. ;;; STRING-APPEND/SHARED has license to return a string that shares storage
  1325. ;;; with any of its arguments. In particular, if there is only one non-empty
  1326. ;;; string amongst its parameters, it is permitted to return that string as
  1327. ;;; its result. STRING-APPEND, by contrast, always allocates new storage.
  1328. ;;;
  1329. ;;; STRING-CONCATENATE & STRING-CONCATENATE/SHARED are passed a list of
  1330. ;;; strings, which they concatenate into a result string. STRING-CONCATENATE
  1331. ;;; always allocates a fresh string; STRING-CONCATENATE/SHARED may (or may
  1332. ;;; not) return a result that shares storage with any of its arguments. In
  1333. ;;; particular, if it is applied to a singleton list, it is permitted to
  1334. ;;; return the car of that list as its value.
  1335. (define (string-append/shared . strings) (string-concatenate/shared strings))
  1336. (define (string-concatenate/shared strings)
  1337. (let lp ((strings strings) (nchars 0) (first #f))
  1338. (cond ((pair? strings) ; Scan the args, add up total
  1339. (let* ((string (car strings)) ; length, remember 1st
  1340. (tail (cdr strings)) ; non-empty string.
  1341. (slen (string-length string)))
  1342. (if (zero? slen)
  1343. (lp tail nchars first)
  1344. (lp tail (+ nchars slen) (or first strings)))))
  1345. ((zero? nchars) "")
  1346. ;; Just one non-empty string! Return it.
  1347. ((= nchars (string-length (car first))) (car first))
  1348. (else (let ((ans (make-string nchars)))
  1349. (let lp ((strings first) (i 0))
  1350. (if (pair? strings)
  1351. (let* ((s (car strings))
  1352. (slen (string-length s)))
  1353. (%string-copy! ans i s 0 slen)
  1354. (lp (cdr strings) (+ i slen)))))
  1355. ans)))))
  1356. ; Alas, Scheme 48's APPLY blows up if you have many, many arguments.
  1357. ;(define (string-concatenate strings) (apply string-append strings))
  1358. ;;; Here it is written out. I avoid using REDUCE to add up string lengths
  1359. ;;; to avoid non-R5RS dependencies.
  1360. (define (string-concatenate strings)
  1361. (let* ((total (do ((strings strings (cdr strings))
  1362. (i 0 (+ i (string-length (car strings)))))
  1363. ((not (pair? strings)) i)))
  1364. (ans (make-string total)))
  1365. (let lp ((i 0) (strings strings))
  1366. (if (pair? strings)
  1367. (let* ((s (car strings))
  1368. (slen (string-length s)))
  1369. (%string-copy! ans i s 0 slen)
  1370. (lp (+ i slen) (cdr strings)))))
  1371. ans))
  1372. ;;; Defined by R5RS, so commented out here.
  1373. ;(define (string-append . strings) (string-concatenate strings))
  1374. ;;; string-concatenate-reverse string-list [final-string end] -> string
  1375. ;;; string-concatenate-reverse/shared string-list [final-string end] -> string
  1376. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1377. ;;; Return
  1378. ;;; (string-concatenate
  1379. ;;; (reverse
  1380. ;;; (cons (substring final-string 0 end) string-list)))
  1381. (define (string-concatenate-reverse string-list . maybe-final+end)
  1382. (let-optionals* maybe-final+end ((final "" (string? final))
  1383. (end (string-length final)
  1384. (and (integer? end)
  1385. (exact? end)
  1386. (<= 0 end (string-length final)))))
  1387. (let ((len (let lp ((sum 0) (lis string-list))
  1388. (if (pair? lis)
  1389. (lp (+ sum (string-length (car lis))) (cdr lis))
  1390. sum))))
  1391. (%finish-string-concatenate-reverse len string-list final end))))
  1392. (define (string-concatenate-reverse/shared string-list . maybe-final+end)
  1393. (let-optionals* maybe-final+end ((final "" (string? final))
  1394. (end (string-length final)
  1395. (and (integer? end)
  1396. (exact? end)
  1397. (<= 0 end (string-length final)))))
  1398. ;; Add up the lengths of all the strings in STRING-LIST; also get a
  1399. ;; pointer NZLIST into STRING-LIST showing where the first non-zero-length
  1400. ;; string starts.
  1401. (let lp ((len 0) (nzlist #f) (lis string-list))
  1402. (if (pair? lis)
  1403. (let ((slen (string-length (car lis))))
  1404. (lp (+ len slen)
  1405. (if (or nzlist (zero? slen)) nzlist lis)
  1406. (cdr lis)))
  1407. (cond ((zero? len) (substring/shared final 0 end))
  1408. ;; LEN > 0, so NZLIST is non-empty.
  1409. ((and (zero? end) (= len (string-length (car nzlist))))
  1410. (car nzlist))
  1411. (else (%finish-string-concatenate-reverse len nzlist final end)))))))
  1412. (define (%finish-string-concatenate-reverse len string-list final end)
  1413. (let ((ans (make-string (+ end len))))
  1414. (%string-copy! ans len final 0 end)
  1415. (let lp ((i len) (lis string-list))
  1416. (if (pair? lis)
  1417. (let* ((s (car lis))
  1418. (lis (cdr lis))
  1419. (slen (string-length s))
  1420. (i (- i slen)))
  1421. (%string-copy! ans i s 0 slen)
  1422. (lp i lis))))
  1423. ans))
  1424. ;;; string-replace s1 s2 start1 end1 [start2 end2] -> string
  1425. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1426. ;;; Replace S1[START1,END1) with S2[START2,END2).
  1427. (define (string-replace s1 s2 start1 end1 . maybe-start+end)
  1428. (check-substring-spec string-replace s1 start1 end1)
  1429. (let-string-start+end (start2 end2) string-replace s2 maybe-start+end
  1430. (let* ((slen1 (string-length s1))
  1431. (sublen2 (- end2 start2))
  1432. (alen (+ (- slen1 (- end1 start1)) sublen2))
  1433. (ans (make-string alen)))
  1434. (%string-copy! ans 0 s1 0 start1)
  1435. (%string-copy! ans start1 s2 start2 end2)
  1436. (%string-copy! ans (+ start1 sublen2) s1 end1 slen1)
  1437. ans)))
  1438. ;;; string-tokenize s [token-set start end] -> list
  1439. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1440. ;;; Break S up into a list of token strings, where a token is a maximal
  1441. ;;; non-empty contiguous sequence of chars belonging to TOKEN-SET.
  1442. ;;; (string-tokenize "hello, world") => ("hello," "world")
  1443. (define (string-tokenize s . token-chars+start+end)
  1444. (let-optionals* token-chars+start+end
  1445. ((token-chars char-set:graphic (char-set? token-chars)) rest)
  1446. (let-string-start+end (start end) string-tokenize s rest
  1447. (let lp ((i end) (ans '()))
  1448. (cond ((and (< start i) (string-index-right s token-chars start i)) =>
  1449. (lambda (tend-1)
  1450. (let ((tend (+ 1 tend-1)))
  1451. (cond ((string-skip-right s token-chars start tend-1) =>
  1452. (lambda (tstart-1)
  1453. (lp tstart-1
  1454. (cons (substring s (+ 1 tstart-1) tend)
  1455. ans))))
  1456. (else (cons (substring s start tend) ans))))))
  1457. (else ans))))))
  1458. ;;; xsubstring s from [to start end] -> string
  1459. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1460. ;;; S is a string; START and END are optional arguments that demarcate
  1461. ;;; a substring of S, defaulting to 0 and the length of S (e.g., the whole
  1462. ;;; string). Replicate this substring up and down index space, in both the
  1463. ;; positive and negative directions. For example, if S = "abcdefg", START=3,
  1464. ;;; and END=6, then we have the conceptual bidirectionally-infinite string
  1465. ;;; ... d e f d e f d e f d e f d e f d e f d e f ...
  1466. ;;; ... -9 -8 -7 -6 -5 -4 -3 -2 -1 0 1 2 3 4 5 6 7 8 9 ...
  1467. ;;; XSUBSTRING returns the substring of this string beginning at index FROM,
  1468. ;;; and ending at TO (which defaults to FROM+(END-START)).
  1469. ;;;
  1470. ;;; You can use XSUBSTRING in many ways:
  1471. ;;; - To rotate a string left: (xsubstring "abcdef" 2) => "cdefab"
  1472. ;;; - To rotate a string right: (xsubstring "abcdef" -2) => "efabcd"
  1473. ;;; - To replicate a string: (xsubstring "abc" 0 7) => "abcabca"
  1474. ;;;
  1475. ;;; Note that
  1476. ;;; - The FROM/TO indices give a half-open range -- the characters from
  1477. ;;; index FROM up to, but not including index TO.
  1478. ;;; - The FROM/TO indices are not in terms of the index space for string S.
  1479. ;;; They are in terms of the replicated index space of the substring
  1480. ;;; defined by S, START, and END.
  1481. ;;;
  1482. ;;; It is an error if START=END -- although this is allowed by special
  1483. ;;; dispensation when FROM=TO.
  1484. (define (xsubstring s from . maybe-to+start+end)
  1485. (check-arg (lambda (val) (and (integer? val) (exact? val)))
  1486. from xsubstring)
  1487. (receive (to start end)
  1488. (if (pair? maybe-to+start+end)
  1489. (let-string-start+end (start end) xsubstring s (cdr maybe-to+start+end)
  1490. (let ((to (car maybe-to+start+end)))
  1491. (check-arg (lambda (val) (and (integer? val)
  1492. (exact? val)
  1493. (<= from val)))
  1494. to xsubstring)
  1495. (values to start end)))
  1496. (let ((slen (string-length (check-arg string? s xsubstring))))
  1497. (values (+ from slen) 0 slen)))
  1498. (let ((slen (- end start))
  1499. (anslen (- to from)))
  1500. (cond ((zero? anslen) "")
  1501. ((zero? slen) (error "Cannot replicate empty (sub)string"
  1502. xsubstring s from to start end))
  1503. ((= 1 slen) ; Fast path for 1-char replication.
  1504. (make-string anslen (string-ref s start)))
  1505. ;; Selected text falls entirely within one span.
  1506. ((= (floor (/ from slen)) (floor (/ to slen)))
  1507. (substring s (+ start (modulo from slen))
  1508. (+ start (modulo to slen))))
  1509. ;; Selected text requires multiple spans.
  1510. (else (let ((ans (make-string anslen)))
  1511. (%multispan-repcopy! ans 0 s from to start end)
  1512. ans))))))
  1513. ;;; string-xcopy! target tstart s sfrom [sto start end] -> unspecific
  1514. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1515. ;;; Exactly the same as xsubstring, but the extracted text is written
  1516. ;;; into the string TARGET starting at index TSTART.
  1517. ;;; This operation is not defined if (EQ? TARGET S) -- you cannot copy
  1518. ;;; a string on top of itself.
  1519. (define (string-xcopy! target tstart s sfrom . maybe-sto+start+end)
  1520. (check-arg (lambda (val) (and (integer? val) (exact? val)))
  1521. sfrom string-xcopy!)
  1522. (receive (sto start end)
  1523. (if (pair? maybe-sto+start+end)
  1524. (let-string-start+end (start end) string-xcopy! s (cdr maybe-sto+start+end)
  1525. (let ((sto (car maybe-sto+start+end)))
  1526. (check-arg (lambda (val) (and (integer? val) (exact? val)))
  1527. sto string-xcopy!)
  1528. (values sto start end)))
  1529. (let ((slen (string-length s)))
  1530. (values (+ sfrom slen) 0 slen)))
  1531. (let* ((tocopy (- sto sfrom))
  1532. (tend (+ tstart tocopy))
  1533. (slen (- end start)))
  1534. (check-substring-spec string-xcopy! target tstart tend)
  1535. (cond ((zero? tocopy))
  1536. ((zero? slen) (error "Cannot replicate empty (sub)string"
  1537. string-xcopy!
  1538. target tstart s sfrom sto start end))
  1539. ((= 1 slen) ; Fast path for 1-char replication.
  1540. (string-fill! target (string-ref s start) tstart tend))
  1541. ;; Selected text falls entirely within one span.
  1542. ((= (floor (/ sfrom slen)) (floor (/ sto slen)))
  1543. (%string-copy! target tstart s
  1544. (+ start (modulo sfrom slen))
  1545. (+ start (modulo sto slen))))
  1546. ;; Multi-span copy.
  1547. (else (%multispan-repcopy! target tstart s sfrom sto start end))))))
  1548. ;;; This is the core copying loop for XSUBSTRING and STRING-XCOPY!
  1549. ;;; Internal -- not exported, no careful arg checking.
  1550. (define (%multispan-repcopy! target tstart s sfrom sto start end)
  1551. (let* ((slen (- end start))
  1552. (i0 (+ start (modulo sfrom slen)))
  1553. (total-chars (- sto sfrom)))
  1554. ;; Copy the partial span @ the beginning
  1555. (%string-copy! target tstart s i0 end)
  1556. (let* ((ncopied (- end i0)) ; We've copied this many.
  1557. (nleft (- total-chars ncopied)) ; # chars left to copy.
  1558. (nspans (quotient nleft slen))) ; # whole spans to copy
  1559. ;; Copy the whole spans in the middle.
  1560. (do ((i (+ tstart ncopied) (+ i slen)) ; Current target index.
  1561. (nspans nspans (- nspans 1))) ; # spans to copy
  1562. ((zero? nspans)
  1563. ;; Copy the partial-span @ the end & we're done.
  1564. (%string-copy! target i s start (+ start (- total-chars (- i tstart)))))
  1565. (%string-copy! target i s start end))))); Copy a whole span.
  1566. ;;; (string-join string-list [delimiter grammar]) => string
  1567. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1568. ;;; Paste strings together using the delimiter string.
  1569. ;;;
  1570. ;;; (join-strings '("foo" "bar" "baz") ":") => "foo:bar:baz"
  1571. ;;;
  1572. ;;; DELIMITER defaults to a single space " "
  1573. ;;; GRAMMAR is one of the symbols {prefix, infix, strict-infix, suffix}
  1574. ;;; and defaults to 'infix.
  1575. ;;;
  1576. ;;; I could rewrite this more efficiently -- precompute the length of the
  1577. ;;; answer string, then allocate & fill it in iteratively. Using
  1578. ;;; STRING-CONCATENATE is less efficient.
  1579. (define (string-join strings . delim+grammar)
  1580. (let-optionals* delim+grammar ((delim " " (string? delim))
  1581. (grammar 'infix))
  1582. (let ((buildit (lambda (lis final)
  1583. (let recur ((lis lis))
  1584. (if (pair? lis)
  1585. (cons delim (cons (car lis) (recur (cdr lis))))
  1586. final)))))
  1587. (cond ((pair? strings)
  1588. (string-concatenate
  1589. (case grammar
  1590. ((infix strict-infix)
  1591. (cons (car strings) (buildit (cdr strings) '())))
  1592. ((prefix) (buildit strings '()))
  1593. ((suffix)
  1594. (cons (car strings) (buildit (cdr strings) (list delim))))
  1595. (else (error "Illegal join grammar"
  1596. grammar string-join)))))
  1597. ((not (null? strings))
  1598. (error "STRINGS parameter not list." strings string-join))
  1599. ;; STRINGS is ()
  1600. ((eq? grammar 'strict-infix)
  1601. (error "Empty list cannot be joined with STRICT-INFIX grammar."
  1602. string-join))
  1603. (else ""))))) ; Special-cased for infix grammar.
  1604. ;;; Porting & performance-tuning notes
  1605. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1606. ;;; See the section at the beginning of this file on external dependencies.
  1607. ;;;
  1608. ;;; The biggest issue with respect to porting is the LET-OPTIONALS* macro.
  1609. ;;; There are many, many optional arguments in this library; the complexity
  1610. ;;; of parsing, defaulting & type-testing these parameters is handled with the
  1611. ;;; aid of this macro. There are about 15 uses of LET-OPTIONALS*. You can
  1612. ;;; rewrite the uses, port the hairy macro definition (which is implemented
  1613. ;;; using a Clinger-Rees low-level explicit-renaming macro system), or port
  1614. ;;; the simple, high-level definition, which is less efficient.
  1615. ;;;
  1616. ;;; There is a fair amount of argument checking. This is, strictly speaking,
  1617. ;;; unnecessary -- the actual body of the procedures will blow up if, say, a
  1618. ;;; START/END index is improper. However, the error message will not be as
  1619. ;;; good as if the error were caught at the "higher level." Also, a very, very
  1620. ;;; smart Scheme compiler may be able to exploit having the type checks done
  1621. ;;; early, so that the actual body of the procedures can assume proper values.
  1622. ;;; This isn't likely; this kind of compiler technology isn't common any
  1623. ;;; longer.
  1624. ;;;
  1625. ;;; The overhead of optional-argument parsing is irritating. The optional
  1626. ;;; arguments must be consed into a rest list on entry, and then parsed out.
  1627. ;;; Function call should be a matter of a few register moves and a jump; it
  1628. ;;; should not involve heap allocation! Your Scheme system may have a superior
  1629. ;;; non-R5RS optional-argument system that can eliminate this overhead. If so,
  1630. ;;; then this is a prime candidate for optimising these procedures,
  1631. ;;; *especially* the many optional START/END index parameters.
  1632. ;;;
  1633. ;;; Note that optional arguments are also a barrier to procedure integration.
  1634. ;;; If your Scheme system permits you to specify alternate entry points
  1635. ;;; for a call when the number of optional arguments is known in a manner
  1636. ;;; that enables inlining/integration, this can provide performance
  1637. ;;; improvements.
  1638. ;;;
  1639. ;;; There is enough *explicit* error checking that *all* string-index
  1640. ;;; operations should *never* produce a bounds error. Period. Feel like
  1641. ;;; living dangerously? *Big* performance win to be had by replacing
  1642. ;;; STRING-REF's and STRING-SET!'s with unsafe equivalents in the loops.
  1643. ;;; Similarly, fixnum-specific operators can speed up the arithmetic done on
  1644. ;;; the index values in the inner loops. The only arguments that are not
  1645. ;;; completely error checked are
  1646. ;;; - string lists (complete checking requires time proportional to the
  1647. ;;; length of the list)
  1648. ;;; - procedure arguments, such as char->char maps & predicates.
  1649. ;;; There is no way to check the range & domain of procedures in Scheme.
  1650. ;;; Procedures that take these parameters cannot fully check their
  1651. ;;; arguments. But all other types to all other procedures are fully
  1652. ;;; checked.
  1653. ;;;
  1654. ;;; This does open up the alternate possibility of simply *removing* these
  1655. ;;; checks, and letting the safe primitives raise the errors. On a dumb
  1656. ;;; Scheme system, this would provide speed (by eliminating the redundant
  1657. ;;; error checks) at the cost of error-message clarity.
  1658. ;;;
  1659. ;;; See the comments preceding the hash function code for notes on tuning
  1660. ;;; the default bound so that the code never overflows your implementation's
  1661. ;;; fixnum size into bignum calculation.
  1662. ;;;
  1663. ;;; In an interpreted Scheme, some of these procedures, or the internal
  1664. ;;; routines with % prefixes, are excellent candidates for being rewritten
  1665. ;;; in C. Consider STRING-HASH, %STRING-COMPARE, the
  1666. ;;; %STRING-{SUF,PRE}FIX-LENGTH routines, STRING-COPY!, STRING-INDEX &
  1667. ;;; STRING-SKIP (char-set & char cases), SUBSTRING and SUBSTRING/SHARED,
  1668. ;;; %KMP-SEARCH, and %MULTISPAN-REPCOPY!.
  1669. ;;;
  1670. ;;; It would also be nice to have the ability to mark some of these
  1671. ;;; routines as candidates for inlining/integration.
  1672. ;;;
  1673. ;;; All the %-prefixed routines in this source code are written
  1674. ;;; to be called internally to this library. They do *not* perform
  1675. ;;; friendly error checks on the inputs; they assume everything is
  1676. ;;; proper. They also do not take optional arguments. These two properties
  1677. ;;; save calling overhead and enable procedure integration -- but they
  1678. ;;; are not appropriate for exported routines.
  1679. ;;; Copyright details
  1680. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1681. ;;; The prefix/suffix and comparison routines in this code had (extremely
  1682. ;;; distant) origins in MIT Scheme's string lib, and was substantially
  1683. ;;; reworked by Olin Shivers (shivers@ai.mit.edu) 9/98. As such, it is
  1684. ;;; covered by MIT Scheme's open source copyright. See below for details.
  1685. ;;;
  1686. ;;; The KMP string-search code was influenced by implementations written
  1687. ;;; by Stephen Bevan, Brian Dehneyer and Will Fitzgerald. However, this
  1688. ;;; version was written from scratch by myself.
  1689. ;;;
  1690. ;;; The remainder of this code was written from scratch by myself for scsh.
  1691. ;;; The scsh copyright is a BSD-style open source copyright. See below for
  1692. ;;; details.
  1693. ;;; -Olin Shivers
  1694. ;;; MIT Scheme copyright terms
  1695. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1696. ;;; This material was developed by the Scheme project at the Massachusetts
  1697. ;;; Institute of Technology, Department of Electrical Engineering and
  1698. ;;; Computer Science. Permission to copy and modify this software, to
  1699. ;;; redistribute either the original software or a modified version, and
  1700. ;;; to use this software for any purpose is granted, subject to the
  1701. ;;; following restrictions and understandings.
  1702. ;;;
  1703. ;;; 1. Any copy made of this software must include this copyright notice
  1704. ;;; in full.
  1705. ;;;
  1706. ;;; 2. Users of this software agree to make their best efforts (a) to
  1707. ;;; return to the MIT Scheme project any improvements or extensions that
  1708. ;;; they make, so that these may be included in future releases; and (b)
  1709. ;;; to inform MIT of noteworthy uses of this software.
  1710. ;;;
  1711. ;;; 3. All materials developed as a consequence of the use of this
  1712. ;;; software shall duly acknowledge such use, in accordance with the usual
  1713. ;;; standards of acknowledging credit in academic research.
  1714. ;;;
  1715. ;;; 4. MIT has made no warrantee or representation that the operation of
  1716. ;;; this software will be error-free, and MIT is under no obligation to
  1717. ;;; provide any services, by way of maintenance, update, or otherwise.
  1718. ;;;
  1719. ;;; 5. In conjunction with products arising from the use of this material,
  1720. ;;; there shall be no use of the name of the Massachusetts Institute of
  1721. ;;; Technology nor of any adaptation thereof in any advertising,
  1722. ;;; promotional, or sales literature without prior written consent from
  1723. ;;; MIT in each case.
  1724. ;;; Scsh copyright terms
  1725. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1726. ;;; All rights reserved.
  1727. ;;;
  1728. ;;; Redistribution and use in source and binary forms, with or without
  1729. ;;; modification, are permitted provided that the following conditions
  1730. ;;; are met:
  1731. ;;; 1. Redistributions of source code must retain the above copyright
  1732. ;;; notice, this list of conditions and the following disclaimer.
  1733. ;;; 2. Redistributions in binary form must reproduce the above copyright
  1734. ;;; notice, this list of conditions and the following disclaimer in the
  1735. ;;; documentation and/or other materials provided with the distribution.
  1736. ;;; 3. The name of the authors may not be used to endorse or promote products
  1737. ;;; derived from this software without specific prior written permission.
  1738. ;;;
  1739. ;;; THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
  1740. ;;; IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  1741. ;;; OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  1742. ;;; IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  1743. ;;; INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  1744. ;;; NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  1745. ;;; DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  1746. ;;; THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  1747. ;;; (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  1748. ;;; THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.