srfi-13.c 96 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586
  1. /* srfi-13.c --- SRFI-13 procedures for Guile
  2. *
  3. * Copyright (C) 2001, 2004, 2005, 2006, 2008 Free Software Foundation, Inc.
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2.1 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  18. */
  19. #ifdef HAVE_CONFIG_H
  20. # include <config.h>
  21. #endif
  22. #include <string.h>
  23. #include <ctype.h>
  24. #include "libguile.h"
  25. #include "libguile/srfi-13.h"
  26. #include "libguile/srfi-14.h"
  27. /* SCM_VALIDATE_SUBSTRING_SPEC_COPY is deprecated since it encourages
  28. messing with the internal representation of strings. We define our
  29. own version since we use it so much and are messing with Guile
  30. internals anyway.
  31. */
  32. #define MY_VALIDATE_SUBSTRING_SPEC_COPY(pos_str, str, c_str, \
  33. pos_start, start, c_start, \
  34. pos_end, end, c_end) \
  35. do { \
  36. SCM_VALIDATE_STRING (pos_str, str); \
  37. c_str = scm_i_string_chars (str); \
  38. scm_i_get_substring_spec (scm_i_string_length (str), \
  39. start, &c_start, end, &c_end); \
  40. } while (0)
  41. /* Expecting "unsigned char *c_str" */
  42. #define MY_VALIDATE_SUBSTRING_SPEC_UCOPY(pos_str, str, c_str, \
  43. pos_start, start, c_start, \
  44. pos_end, end, c_end) \
  45. do { \
  46. const char *signed_c_str; \
  47. MY_VALIDATE_SUBSTRING_SPEC_COPY(pos_str, str, signed_c_str, \
  48. pos_start, start, c_start, \
  49. pos_end, end, c_end); \
  50. c_str = (unsigned char *) signed_c_str; \
  51. } while (0)
  52. #define MY_VALIDATE_SUBSTRING_SPEC(pos_str, str, \
  53. pos_start, start, c_start, \
  54. pos_end, end, c_end) \
  55. do { \
  56. SCM_VALIDATE_STRING (pos_str, str); \
  57. scm_i_get_substring_spec (scm_i_string_length (str), \
  58. start, &c_start, end, &c_end); \
  59. } while (0)
  60. SCM_DEFINE (scm_string_null_p, "string-null?", 1, 0, 0,
  61. (SCM str),
  62. "Return @code{#t} if @var{str}'s length is zero, and\n"
  63. "@code{#f} otherwise.\n"
  64. "@lisp\n"
  65. "(string-null? \"\") @result{} #t\n"
  66. "y @result{} \"foo\"\n"
  67. "(string-null? y) @result{} #f\n"
  68. "@end lisp")
  69. #define FUNC_NAME s_scm_string_null_p
  70. {
  71. SCM_VALIDATE_STRING (1, str);
  72. return scm_from_bool (scm_i_string_length (str) == 0);
  73. }
  74. #undef FUNC_NAME
  75. #if 0
  76. static void
  77. race_error ()
  78. {
  79. scm_misc_error (NULL, "race condition detected", SCM_EOL);
  80. }
  81. #endif
  82. SCM_DEFINE (scm_string_any, "string-any-c-code", 2, 2, 0,
  83. (SCM char_pred, SCM s, SCM start, SCM end),
  84. "Check if @var{char_pred} is true for any character in string @var{s}.\n"
  85. "\n"
  86. "@var{char_pred} can be a character to check for any equal to that, or\n"
  87. "a character set (@pxref{Character Sets}) to check for any in that set,\n"
  88. "or a predicate procedure to call.\n"
  89. "\n"
  90. "For a procedure, calls @code{(@var{char_pred} c)} are made\n"
  91. "successively on the characters from @var{start} to @var{end}. If\n"
  92. "@var{char_pred} returns true (ie.@: non-@code{#f}), @code{string-any}\n"
  93. "stops and that return value is the return from @code{string-any}. The\n"
  94. "call on the last character (ie.@: at @math{@var{end}-1}), if that\n"
  95. "point is reached, is a tail call.\n"
  96. "\n"
  97. "If there are no characters in @var{s} (ie.@: @var{start} equals\n"
  98. "@var{end}) then the return is @code{#f}.\n")
  99. #define FUNC_NAME s_scm_string_any
  100. {
  101. const char *cstr;
  102. size_t cstart, cend;
  103. SCM res = SCM_BOOL_F;
  104. MY_VALIDATE_SUBSTRING_SPEC_COPY (2, s, cstr,
  105. 3, start, cstart,
  106. 4, end, cend);
  107. if (SCM_CHARP (char_pred))
  108. {
  109. res = (memchr (cstr+cstart, (int) SCM_CHAR (char_pred),
  110. cend-cstart) == NULL
  111. ? SCM_BOOL_F : SCM_BOOL_T);
  112. }
  113. else if (SCM_CHARSETP (char_pred))
  114. {
  115. size_t i;
  116. for (i = cstart; i < cend; i++)
  117. if (SCM_CHARSET_GET (char_pred, cstr[i]))
  118. {
  119. res = SCM_BOOL_T;
  120. break;
  121. }
  122. }
  123. else
  124. {
  125. scm_t_trampoline_1 pred_tramp = scm_trampoline_1 (char_pred);
  126. SCM_ASSERT (pred_tramp, char_pred, SCM_ARG1, FUNC_NAME);
  127. while (cstart < cend)
  128. {
  129. res = pred_tramp (char_pred, SCM_MAKE_CHAR (cstr[cstart]));
  130. if (scm_is_true (res))
  131. break;
  132. cstr = scm_i_string_chars (s);
  133. cstart++;
  134. }
  135. }
  136. scm_remember_upto_here_1 (s);
  137. return res;
  138. }
  139. #undef FUNC_NAME
  140. SCM_DEFINE (scm_string_every, "string-every-c-code", 2, 2, 0,
  141. (SCM char_pred, SCM s, SCM start, SCM end),
  142. "Check if @var{char_pred} is true for every character in string\n"
  143. "@var{s}.\n"
  144. "\n"
  145. "@var{char_pred} can be a character to check for every character equal\n"
  146. "to that, or a character set (@pxref{Character Sets}) to check for\n"
  147. "every character being in that set, or a predicate procedure to call.\n"
  148. "\n"
  149. "For a procedure, calls @code{(@var{char_pred} c)} are made\n"
  150. "successively on the characters from @var{start} to @var{end}. If\n"
  151. "@var{char_pred} returns @code{#f}, @code{string-every} stops and\n"
  152. "returns @code{#f}. The call on the last character (ie.@: at\n"
  153. "@math{@var{end}-1}), if that point is reached, is a tail call and the\n"
  154. "return from that call is the return from @code{string-every}.\n"
  155. "\n"
  156. "If there are no characters in @var{s} (ie.@: @var{start} equals\n"
  157. "@var{end}) then the return is @code{#t}.\n")
  158. #define FUNC_NAME s_scm_string_every
  159. {
  160. const char *cstr;
  161. size_t cstart, cend;
  162. SCM res = SCM_BOOL_T;
  163. MY_VALIDATE_SUBSTRING_SPEC_COPY (2, s, cstr,
  164. 3, start, cstart,
  165. 4, end, cend);
  166. if (SCM_CHARP (char_pred))
  167. {
  168. char cchr = SCM_CHAR (char_pred);
  169. size_t i;
  170. for (i = cstart; i < cend; i++)
  171. if (cstr[i] != cchr)
  172. {
  173. res = SCM_BOOL_F;
  174. break;
  175. }
  176. }
  177. else if (SCM_CHARSETP (char_pred))
  178. {
  179. size_t i;
  180. for (i = cstart; i < cend; i++)
  181. if (!SCM_CHARSET_GET (char_pred, cstr[i]))
  182. {
  183. res = SCM_BOOL_F;
  184. break;
  185. }
  186. }
  187. else
  188. {
  189. scm_t_trampoline_1 pred_tramp = scm_trampoline_1 (char_pred);
  190. SCM_ASSERT (pred_tramp, char_pred, SCM_ARG1, FUNC_NAME);
  191. while (cstart < cend)
  192. {
  193. res = pred_tramp (char_pred, SCM_MAKE_CHAR (cstr[cstart]));
  194. if (scm_is_false (res))
  195. break;
  196. cstr = scm_i_string_chars (s);
  197. cstart++;
  198. }
  199. }
  200. scm_remember_upto_here_1 (s);
  201. return res;
  202. }
  203. #undef FUNC_NAME
  204. SCM_DEFINE (scm_string_tabulate, "string-tabulate", 2, 0, 0,
  205. (SCM proc, SCM len),
  206. "@var{proc} is an integer->char procedure. Construct a string\n"
  207. "of size @var{len} by applying @var{proc} to each index to\n"
  208. "produce the corresponding string element. The order in which\n"
  209. "@var{proc} is applied to the indices is not specified.")
  210. #define FUNC_NAME s_scm_string_tabulate
  211. {
  212. size_t clen, i;
  213. SCM res;
  214. SCM ch;
  215. char *p;
  216. scm_t_trampoline_1 proc_tramp;
  217. proc_tramp = scm_trampoline_1 (proc);
  218. SCM_ASSERT (proc_tramp, proc, SCM_ARG1, FUNC_NAME);
  219. clen = scm_to_size_t (len);
  220. SCM_ASSERT_RANGE (2, len, clen >= 0);
  221. res = scm_i_make_string (clen, &p);
  222. i = 0;
  223. while (i < clen)
  224. {
  225. /* The RES string remains untouched since nobody knows about it
  226. yet. No need to refetch P.
  227. */
  228. ch = proc_tramp (proc, scm_from_size_t (i));
  229. if (!SCM_CHARP (ch))
  230. SCM_MISC_ERROR ("procedure ~S returned non-char", scm_list_1 (proc));
  231. *p++ = SCM_CHAR (ch);
  232. i++;
  233. }
  234. return res;
  235. }
  236. #undef FUNC_NAME
  237. SCM_DEFINE (scm_substring_to_list, "string->list", 1, 2, 0,
  238. (SCM str, SCM start, SCM end),
  239. "Convert the string @var{str} into a list of characters.")
  240. #define FUNC_NAME s_scm_substring_to_list
  241. {
  242. const char *cstr;
  243. size_t cstart, cend;
  244. SCM result = SCM_EOL;
  245. MY_VALIDATE_SUBSTRING_SPEC_COPY (1, str, cstr,
  246. 2, start, cstart,
  247. 3, end, cend);
  248. while (cstart < cend)
  249. {
  250. cend--;
  251. result = scm_cons (SCM_MAKE_CHAR (cstr[cend]), result);
  252. cstr = scm_i_string_chars (str);
  253. }
  254. scm_remember_upto_here_1 (str);
  255. return result;
  256. }
  257. #undef FUNC_NAME
  258. /* We export scm_substring_to_list as "string->list" since it is
  259. compatible and more general. This function remains for the benefit
  260. of C code that used it.
  261. */
  262. SCM
  263. scm_string_to_list (SCM str)
  264. {
  265. return scm_substring_to_list (str, SCM_UNDEFINED, SCM_UNDEFINED);
  266. }
  267. SCM_DEFINE (scm_reverse_list_to_string, "reverse-list->string", 1, 0, 0,
  268. (SCM chrs),
  269. "An efficient implementation of @code{(compose string->list\n"
  270. "reverse)}:\n"
  271. "\n"
  272. "@smalllisp\n"
  273. "(reverse-list->string '(#\\a #\\B #\\c)) @result{} \"cBa\"\n"
  274. "@end smalllisp")
  275. #define FUNC_NAME s_scm_reverse_list_to_string
  276. {
  277. SCM result;
  278. long i = scm_ilength (chrs);
  279. char *data;
  280. if (i < 0)
  281. SCM_WRONG_TYPE_ARG (1, chrs);
  282. result = scm_i_make_string (i, &data);
  283. {
  284. data += i;
  285. while (i > 0 && scm_is_pair (chrs))
  286. {
  287. SCM elt = SCM_CAR (chrs);
  288. SCM_VALIDATE_CHAR (SCM_ARGn, elt);
  289. data--;
  290. *data = SCM_CHAR (elt);
  291. chrs = SCM_CDR (chrs);
  292. i--;
  293. }
  294. }
  295. return result;
  296. }
  297. #undef FUNC_NAME
  298. SCM_SYMBOL (scm_sym_infix, "infix");
  299. SCM_SYMBOL (scm_sym_strict_infix, "strict-infix");
  300. SCM_SYMBOL (scm_sym_suffix, "suffix");
  301. SCM_SYMBOL (scm_sym_prefix, "prefix");
  302. static void
  303. append_string (char **sp, size_t *lp, SCM str)
  304. {
  305. size_t len;
  306. len = scm_c_string_length (str);
  307. if (len > *lp)
  308. len = *lp;
  309. memcpy (*sp, scm_i_string_chars (str), len);
  310. *lp -= len;
  311. *sp += len;
  312. }
  313. SCM_DEFINE (scm_string_join, "string-join", 1, 2, 0,
  314. (SCM ls, SCM delimiter, SCM grammar),
  315. "Append the string in the string list @var{ls}, using the string\n"
  316. "@var{delim} as a delimiter between the elements of @var{ls}.\n"
  317. "@var{grammar} is a symbol which specifies how the delimiter is\n"
  318. "placed between the strings, and defaults to the symbol\n"
  319. "@code{infix}.\n"
  320. "\n"
  321. "@table @code\n"
  322. "@item infix\n"
  323. "Insert the separator between list elements. An empty string\n"
  324. "will produce an empty list.\n"
  325. "@item string-infix\n"
  326. "Like @code{infix}, but will raise an error if given the empty\n"
  327. "list.\n"
  328. "@item suffix\n"
  329. "Insert the separator after every list element.\n"
  330. "@item prefix\n"
  331. "Insert the separator before each list element.\n"
  332. "@end table")
  333. #define FUNC_NAME s_scm_string_join
  334. {
  335. #define GRAM_INFIX 0
  336. #define GRAM_STRICT_INFIX 1
  337. #define GRAM_SUFFIX 2
  338. #define GRAM_PREFIX 3
  339. SCM tmp;
  340. SCM result;
  341. int gram = GRAM_INFIX;
  342. size_t del_len = 0;
  343. size_t len = 0;
  344. char *p;
  345. long strings = scm_ilength (ls);
  346. /* Validate the string list. */
  347. if (strings < 0)
  348. SCM_WRONG_TYPE_ARG (1, ls);
  349. /* Validate the delimiter and record its length. */
  350. if (SCM_UNBNDP (delimiter))
  351. {
  352. delimiter = scm_from_locale_string (" ");
  353. del_len = 1;
  354. }
  355. else
  356. del_len = scm_c_string_length (delimiter);
  357. /* Validate the grammar symbol and remember the grammar. */
  358. if (SCM_UNBNDP (grammar))
  359. gram = GRAM_INFIX;
  360. else if (scm_is_eq (grammar, scm_sym_infix))
  361. gram = GRAM_INFIX;
  362. else if (scm_is_eq (grammar, scm_sym_strict_infix))
  363. gram = GRAM_STRICT_INFIX;
  364. else if (scm_is_eq (grammar, scm_sym_suffix))
  365. gram = GRAM_SUFFIX;
  366. else if (scm_is_eq (grammar, scm_sym_prefix))
  367. gram = GRAM_PREFIX;
  368. else
  369. SCM_WRONG_TYPE_ARG (3, grammar);
  370. /* Check grammar constraints and calculate the space required for
  371. the delimiter(s). */
  372. switch (gram)
  373. {
  374. case GRAM_INFIX:
  375. if (!scm_is_null (ls))
  376. len = (strings > 0) ? ((strings - 1) * del_len) : 0;
  377. break;
  378. case GRAM_STRICT_INFIX:
  379. if (strings == 0)
  380. SCM_MISC_ERROR ("strict-infix grammar requires non-empty list",
  381. SCM_EOL);
  382. len = (strings - 1) * del_len;
  383. break;
  384. default:
  385. len = strings * del_len;
  386. break;
  387. }
  388. tmp = ls;
  389. while (scm_is_pair (tmp))
  390. {
  391. len += scm_c_string_length (SCM_CAR (tmp));
  392. tmp = SCM_CDR (tmp);
  393. }
  394. result = scm_i_make_string (len, &p);
  395. tmp = ls;
  396. switch (gram)
  397. {
  398. case GRAM_INFIX:
  399. case GRAM_STRICT_INFIX:
  400. while (scm_is_pair (tmp))
  401. {
  402. append_string (&p, &len, SCM_CAR (tmp));
  403. if (!scm_is_null (SCM_CDR (tmp)) && del_len > 0)
  404. append_string (&p, &len, delimiter);
  405. tmp = SCM_CDR (tmp);
  406. }
  407. break;
  408. case GRAM_SUFFIX:
  409. while (scm_is_pair (tmp))
  410. {
  411. append_string (&p, &len, SCM_CAR (tmp));
  412. if (del_len > 0)
  413. append_string (&p, &len, delimiter);
  414. tmp = SCM_CDR (tmp);
  415. }
  416. break;
  417. case GRAM_PREFIX:
  418. while (scm_is_pair (tmp))
  419. {
  420. if (del_len > 0)
  421. append_string (&p, &len, delimiter);
  422. append_string (&p, &len, SCM_CAR (tmp));
  423. tmp = SCM_CDR (tmp);
  424. }
  425. break;
  426. }
  427. return result;
  428. #undef GRAM_INFIX
  429. #undef GRAM_STRICT_INFIX
  430. #undef GRAM_SUFFIX
  431. #undef GRAM_PREFIX
  432. }
  433. #undef FUNC_NAME
  434. /* There are a number of functions to consider here for Scheme and C:
  435. string-copy STR [start [end]] ;; SRFI-13 variant of R5RS string-copy
  436. substring/copy STR start [end] ;; Guile variant of R5RS substring
  437. scm_string_copy (str) ;; Old function from Guile
  438. scm_substring_copy (str, [start, [end]])
  439. ;; C version of SRFI-13 string-copy
  440. ;; and C version of substring/copy
  441. The C function underlying string-copy is not exported to C
  442. programs. scm_substring_copy is defined in strings.c as the
  443. underlying function of substring/copy and allows an optional START
  444. argument.
  445. */
  446. SCM scm_srfi13_substring_copy (SCM str, SCM start, SCM end);
  447. SCM_DEFINE (scm_srfi13_substring_copy, "string-copy", 1, 2, 0,
  448. (SCM str, SCM start, SCM end),
  449. "Return a freshly allocated copy of the string @var{str}. If\n"
  450. "given, @var{start} and @var{end} delimit the portion of\n"
  451. "@var{str} which is copied.")
  452. #define FUNC_NAME s_scm_srfi13_substring_copy
  453. {
  454. const char *cstr;
  455. size_t cstart, cend;
  456. MY_VALIDATE_SUBSTRING_SPEC_COPY (1, str, cstr,
  457. 2, start, cstart,
  458. 3, end, cend);
  459. return scm_c_substring_copy (str, cstart, cend);
  460. }
  461. #undef FUNC_NAME
  462. SCM
  463. scm_string_copy (SCM str)
  464. {
  465. return scm_c_substring (str, 0, scm_c_string_length (str));
  466. }
  467. SCM_DEFINE (scm_string_copy_x, "string-copy!", 3, 2, 0,
  468. (SCM target, SCM tstart, SCM s, SCM start, SCM end),
  469. "Copy the sequence of characters from index range [@var{start},\n"
  470. "@var{end}) in string @var{s} to string @var{target}, beginning\n"
  471. "at index @var{tstart}. The characters are copied left-to-right\n"
  472. "or right-to-left as needed -- the copy is guaranteed to work,\n"
  473. "even if @var{target} and @var{s} are the same string. It is an\n"
  474. "error if the copy operation runs off the end of the target\n"
  475. "string.")
  476. #define FUNC_NAME s_scm_string_copy_x
  477. {
  478. const char *cstr;
  479. char *ctarget;
  480. size_t cstart, cend, ctstart, dummy, len;
  481. SCM sdummy = SCM_UNDEFINED;
  482. MY_VALIDATE_SUBSTRING_SPEC (1, target,
  483. 2, tstart, ctstart,
  484. 2, sdummy, dummy);
  485. MY_VALIDATE_SUBSTRING_SPEC_COPY (3, s, cstr,
  486. 4, start, cstart,
  487. 5, end, cend);
  488. len = cend - cstart;
  489. SCM_ASSERT_RANGE (3, s, len <= scm_i_string_length (target) - ctstart);
  490. ctarget = scm_i_string_writable_chars (target);
  491. memmove (ctarget + ctstart, cstr + cstart, len);
  492. scm_i_string_stop_writing ();
  493. scm_remember_upto_here_1 (target);
  494. return SCM_UNSPECIFIED;
  495. }
  496. #undef FUNC_NAME
  497. SCM_DEFINE (scm_substring_move_x, "substring-move!", 5, 0, 0,
  498. (SCM str1, SCM start1, SCM end1, SCM str2, SCM start2),
  499. "Copy the substring of @var{str1} bounded by @var{start1} and @var{end1}\n"
  500. "into @var{str2} beginning at position @var{start2}.\n"
  501. "@var{str1} and @var{str2} can be the same string.")
  502. #define FUNC_NAME s_scm_substring_move_x
  503. {
  504. return scm_string_copy_x (str2, start2, str1, start1, end1);
  505. }
  506. #undef FUNC_NAME
  507. SCM_DEFINE (scm_string_take, "string-take", 2, 0, 0,
  508. (SCM s, SCM n),
  509. "Return the @var{n} first characters of @var{s}.")
  510. #define FUNC_NAME s_scm_string_take
  511. {
  512. return scm_substring (s, SCM_INUM0, n);
  513. }
  514. #undef FUNC_NAME
  515. SCM_DEFINE (scm_string_drop, "string-drop", 2, 0, 0,
  516. (SCM s, SCM n),
  517. "Return all but the first @var{n} characters of @var{s}.")
  518. #define FUNC_NAME s_scm_string_drop
  519. {
  520. return scm_substring (s, n, SCM_UNDEFINED);
  521. }
  522. #undef FUNC_NAME
  523. SCM_DEFINE (scm_string_take_right, "string-take-right", 2, 0, 0,
  524. (SCM s, SCM n),
  525. "Return the @var{n} last characters of @var{s}.")
  526. #define FUNC_NAME s_scm_string_take_right
  527. {
  528. return scm_substring (s,
  529. scm_difference (scm_string_length (s), n),
  530. SCM_UNDEFINED);
  531. }
  532. #undef FUNC_NAME
  533. SCM_DEFINE (scm_string_drop_right, "string-drop-right", 2, 0, 0,
  534. (SCM s, SCM n),
  535. "Return all but the last @var{n} characters of @var{s}.")
  536. #define FUNC_NAME s_scm_string_drop_right
  537. {
  538. return scm_substring (s,
  539. SCM_INUM0,
  540. scm_difference (scm_string_length (s), n));
  541. }
  542. #undef FUNC_NAME
  543. SCM_DEFINE (scm_string_pad, "string-pad", 2, 3, 0,
  544. (SCM s, SCM len, SCM chr, SCM start, SCM end),
  545. "Take that characters from @var{start} to @var{end} from the\n"
  546. "string @var{s} and return a new string, right-padded by the\n"
  547. "character @var{chr} to length @var{len}. If the resulting\n"
  548. "string is longer than @var{len}, it is truncated on the right.")
  549. #define FUNC_NAME s_scm_string_pad
  550. {
  551. char cchr;
  552. size_t cstart, cend, clen;
  553. MY_VALIDATE_SUBSTRING_SPEC (1, s,
  554. 4, start, cstart,
  555. 5, end, cend);
  556. clen = scm_to_size_t (len);
  557. if (SCM_UNBNDP (chr))
  558. cchr = ' ';
  559. else
  560. {
  561. SCM_VALIDATE_CHAR (3, chr);
  562. cchr = SCM_CHAR (chr);
  563. }
  564. if (clen < (cend - cstart))
  565. return scm_c_substring (s, cend - clen, cend);
  566. else
  567. {
  568. SCM result;
  569. char *dst;
  570. result = scm_i_make_string (clen, &dst);
  571. memset (dst, cchr, (clen - (cend - cstart)));
  572. memmove (dst + clen - (cend - cstart),
  573. scm_i_string_chars (s) + cstart, cend - cstart);
  574. return result;
  575. }
  576. }
  577. #undef FUNC_NAME
  578. SCM_DEFINE (scm_string_pad_right, "string-pad-right", 2, 3, 0,
  579. (SCM s, SCM len, SCM chr, SCM start, SCM end),
  580. "Take that characters from @var{start} to @var{end} from the\n"
  581. "string @var{s} and return a new string, left-padded by the\n"
  582. "character @var{chr} to length @var{len}. If the resulting\n"
  583. "string is longer than @var{len}, it is truncated on the left.")
  584. #define FUNC_NAME s_scm_string_pad_right
  585. {
  586. char cchr;
  587. size_t cstart, cend, clen;
  588. MY_VALIDATE_SUBSTRING_SPEC (1, s,
  589. 4, start, cstart,
  590. 5, end, cend);
  591. clen = scm_to_size_t (len);
  592. if (SCM_UNBNDP (chr))
  593. cchr = ' ';
  594. else
  595. {
  596. SCM_VALIDATE_CHAR (3, chr);
  597. cchr = SCM_CHAR (chr);
  598. }
  599. if (clen < (cend - cstart))
  600. return scm_c_substring (s, cstart, cstart + clen);
  601. else
  602. {
  603. SCM result;
  604. char *dst;
  605. result = scm_i_make_string (clen, &dst);
  606. memset (dst + (cend - cstart), cchr, clen - (cend - cstart));
  607. memmove (dst, scm_i_string_chars (s) + cstart, cend - cstart);
  608. return result;
  609. }
  610. }
  611. #undef FUNC_NAME
  612. SCM_DEFINE (scm_string_trim, "string-trim", 1, 3, 0,
  613. (SCM s, SCM char_pred, SCM start, SCM end),
  614. "Trim @var{s} by skipping over all characters on the left\n"
  615. "that satisfy the parameter @var{char_pred}:\n"
  616. "\n"
  617. "@itemize @bullet\n"
  618. "@item\n"
  619. "if it is the character @var{ch}, characters equal to\n"
  620. "@var{ch} are trimmed,\n"
  621. "\n"
  622. "@item\n"
  623. "if it is a procedure @var{pred} characters that\n"
  624. "satisfy @var{pred} are trimmed,\n"
  625. "\n"
  626. "@item\n"
  627. "if it is a character set, characters in that set are trimmed.\n"
  628. "@end itemize\n"
  629. "\n"
  630. "If called without a @var{char_pred} argument, all whitespace is\n"
  631. "trimmed.")
  632. #define FUNC_NAME s_scm_string_trim
  633. {
  634. const char *cstr;
  635. size_t cstart, cend;
  636. MY_VALIDATE_SUBSTRING_SPEC_COPY (1, s, cstr,
  637. 3, start, cstart,
  638. 4, end, cend);
  639. if (SCM_UNBNDP (char_pred))
  640. {
  641. while (cstart < cend)
  642. {
  643. if (!isspace((int) (unsigned char) cstr[cstart]))
  644. break;
  645. cstart++;
  646. }
  647. }
  648. else if (SCM_CHARP (char_pred))
  649. {
  650. char chr = SCM_CHAR (char_pred);
  651. while (cstart < cend)
  652. {
  653. if (chr != cstr[cstart])
  654. break;
  655. cstart++;
  656. }
  657. }
  658. else if (SCM_CHARSETP (char_pred))
  659. {
  660. while (cstart < cend)
  661. {
  662. if (!SCM_CHARSET_GET (char_pred, cstr[cstart]))
  663. break;
  664. cstart++;
  665. }
  666. }
  667. else
  668. {
  669. scm_t_trampoline_1 pred_tramp = scm_trampoline_1 (char_pred);
  670. SCM_ASSERT (pred_tramp, char_pred, SCM_ARG2, FUNC_NAME);
  671. while (cstart < cend)
  672. {
  673. SCM res;
  674. res = pred_tramp (char_pred, SCM_MAKE_CHAR (cstr[cstart]));
  675. if (scm_is_false (res))
  676. break;
  677. cstr = scm_i_string_chars (s);
  678. cstart++;
  679. }
  680. }
  681. return scm_c_substring (s, cstart, cend);
  682. }
  683. #undef FUNC_NAME
  684. SCM_DEFINE (scm_string_trim_right, "string-trim-right", 1, 3, 0,
  685. (SCM s, SCM char_pred, SCM start, SCM end),
  686. "Trim @var{s} by skipping over all characters on the rightt\n"
  687. "that satisfy the parameter @var{char_pred}:\n"
  688. "\n"
  689. "@itemize @bullet\n"
  690. "@item\n"
  691. "if it is the character @var{ch}, characters equal to @var{ch}\n"
  692. "are trimmed,\n"
  693. "\n"
  694. "@item\n"
  695. "if it is a procedure @var{pred} characters that satisfy\n"
  696. "@var{pred} are trimmed,\n"
  697. "\n"
  698. "@item\n"
  699. "if it is a character sets, all characters in that set are\n"
  700. "trimmed.\n"
  701. "@end itemize\n"
  702. "\n"
  703. "If called without a @var{char_pred} argument, all whitespace is\n"
  704. "trimmed.")
  705. #define FUNC_NAME s_scm_string_trim_right
  706. {
  707. const char *cstr;
  708. size_t cstart, cend;
  709. MY_VALIDATE_SUBSTRING_SPEC_COPY (1, s, cstr,
  710. 3, start, cstart,
  711. 4, end, cend);
  712. if (SCM_UNBNDP (char_pred))
  713. {
  714. while (cstart < cend)
  715. {
  716. if (!isspace((int) (unsigned char) cstr[cend - 1]))
  717. break;
  718. cend--;
  719. }
  720. }
  721. else if (SCM_CHARP (char_pred))
  722. {
  723. char chr = SCM_CHAR (char_pred);
  724. while (cstart < cend)
  725. {
  726. if (chr != cstr[cend - 1])
  727. break;
  728. cend--;
  729. }
  730. }
  731. else if (SCM_CHARSETP (char_pred))
  732. {
  733. while (cstart < cend)
  734. {
  735. if (!SCM_CHARSET_GET (char_pred, cstr[cend - 1]))
  736. break;
  737. cend--;
  738. }
  739. }
  740. else
  741. {
  742. scm_t_trampoline_1 pred_tramp = scm_trampoline_1 (char_pred);
  743. SCM_ASSERT (pred_tramp, char_pred, SCM_ARG2, FUNC_NAME);
  744. while (cstart < cend)
  745. {
  746. SCM res;
  747. res = pred_tramp (char_pred, SCM_MAKE_CHAR (cstr[cend - 1]));
  748. if (scm_is_false (res))
  749. break;
  750. cstr = scm_i_string_chars (s);
  751. cend--;
  752. }
  753. }
  754. return scm_c_substring (s, cstart, cend);
  755. }
  756. #undef FUNC_NAME
  757. SCM_DEFINE (scm_string_trim_both, "string-trim-both", 1, 3, 0,
  758. (SCM s, SCM char_pred, SCM start, SCM end),
  759. "Trim @var{s} by skipping over all characters on both sides of\n"
  760. "the string that satisfy the parameter @var{char_pred}:\n"
  761. "\n"
  762. "@itemize @bullet\n"
  763. "@item\n"
  764. "if it is the character @var{ch}, characters equal to @var{ch}\n"
  765. "are trimmed,\n"
  766. "\n"
  767. "@item\n"
  768. "if it is a procedure @var{pred} characters that satisfy\n"
  769. "@var{pred} are trimmed,\n"
  770. "\n"
  771. "@item\n"
  772. "if it is a character set, the characters in the set are\n"
  773. "trimmed.\n"
  774. "@end itemize\n"
  775. "\n"
  776. "If called without a @var{char_pred} argument, all whitespace is\n"
  777. "trimmed.")
  778. #define FUNC_NAME s_scm_string_trim_both
  779. {
  780. const char *cstr;
  781. size_t cstart, cend;
  782. MY_VALIDATE_SUBSTRING_SPEC_COPY (1, s, cstr,
  783. 3, start, cstart,
  784. 4, end, cend);
  785. if (SCM_UNBNDP (char_pred))
  786. {
  787. while (cstart < cend)
  788. {
  789. if (!isspace((int) (unsigned char) cstr[cstart]))
  790. break;
  791. cstart++;
  792. }
  793. while (cstart < cend)
  794. {
  795. if (!isspace((int) (unsigned char) cstr[cend - 1]))
  796. break;
  797. cend--;
  798. }
  799. }
  800. else if (SCM_CHARP (char_pred))
  801. {
  802. char chr = SCM_CHAR (char_pred);
  803. while (cstart < cend)
  804. {
  805. if (chr != cstr[cstart])
  806. break;
  807. cstart++;
  808. }
  809. while (cstart < cend)
  810. {
  811. if (chr != cstr[cend - 1])
  812. break;
  813. cend--;
  814. }
  815. }
  816. else if (SCM_CHARSETP (char_pred))
  817. {
  818. while (cstart < cend)
  819. {
  820. if (!SCM_CHARSET_GET (char_pred, cstr[cstart]))
  821. break;
  822. cstart++;
  823. }
  824. while (cstart < cend)
  825. {
  826. if (!SCM_CHARSET_GET (char_pred, cstr[cend - 1]))
  827. break;
  828. cend--;
  829. }
  830. }
  831. else
  832. {
  833. scm_t_trampoline_1 pred_tramp = scm_trampoline_1 (char_pred);
  834. SCM_ASSERT (pred_tramp, char_pred, SCM_ARG2, FUNC_NAME);
  835. while (cstart < cend)
  836. {
  837. SCM res;
  838. res = pred_tramp (char_pred, SCM_MAKE_CHAR (cstr[cstart]));
  839. if (scm_is_false (res))
  840. break;
  841. cstr = scm_i_string_chars (s);
  842. cstart++;
  843. }
  844. while (cstart < cend)
  845. {
  846. SCM res;
  847. res = pred_tramp (char_pred, SCM_MAKE_CHAR (cstr[cend - 1]));
  848. if (scm_is_false (res))
  849. break;
  850. cstr = scm_i_string_chars (s);
  851. cend--;
  852. }
  853. }
  854. return scm_c_substring (s, cstart, cend);
  855. }
  856. #undef FUNC_NAME
  857. SCM_DEFINE (scm_substring_fill_x, "string-fill!", 2, 2, 0,
  858. (SCM str, SCM chr, SCM start, SCM end),
  859. "Stores @var{chr} in every element of the given @var{str} and\n"
  860. "returns an unspecified value.")
  861. #define FUNC_NAME s_scm_substring_fill_x
  862. {
  863. char *cstr;
  864. size_t cstart, cend;
  865. int c;
  866. size_t k;
  867. /* Older versions of Guile provided the function
  868. scm_substring_fill_x with the following order of arguments:
  869. str, start, end, chr
  870. We accomodate this here by detecting such a usage and reordering
  871. the arguments.
  872. */
  873. if (SCM_CHARP (end))
  874. {
  875. SCM tmp = end;
  876. end = start;
  877. start = chr;
  878. chr = tmp;
  879. }
  880. MY_VALIDATE_SUBSTRING_SPEC (1, str,
  881. 3, start, cstart,
  882. 4, end, cend);
  883. SCM_VALIDATE_CHAR_COPY (2, chr, c);
  884. cstr = scm_i_string_writable_chars (str);
  885. for (k = cstart; k < cend; k++)
  886. cstr[k] = c;
  887. scm_i_string_stop_writing ();
  888. scm_remember_upto_here_1 (str);
  889. return SCM_UNSPECIFIED;
  890. }
  891. #undef FUNC_NAME
  892. SCM
  893. scm_string_fill_x (SCM str, SCM chr)
  894. {
  895. return scm_substring_fill_x (str, chr, SCM_UNDEFINED, SCM_UNDEFINED);
  896. }
  897. SCM_DEFINE (scm_string_compare, "string-compare", 5, 4, 0,
  898. (SCM s1, SCM s2, SCM proc_lt, SCM proc_eq, SCM proc_gt, SCM start1, SCM end1, SCM start2, SCM end2),
  899. "Apply @var{proc_lt}, @var{proc_eq}, @var{proc_gt} to the\n"
  900. "mismatch index, depending upon whether @var{s1} is less than,\n"
  901. "equal to, or greater than @var{s2}. The mismatch index is the\n"
  902. "largest index @var{i} such that for every 0 <= @var{j} <\n"
  903. "@var{i}, @var{s1}[@var{j}] = @var{s2}[@var{j}] -- that is,\n"
  904. "@var{i} is the first position that does not match.")
  905. #define FUNC_NAME s_scm_string_compare
  906. {
  907. const unsigned char *cstr1, *cstr2;
  908. size_t cstart1, cend1, cstart2, cend2;
  909. SCM proc;
  910. MY_VALIDATE_SUBSTRING_SPEC_UCOPY (1, s1, cstr1,
  911. 6, start1, cstart1,
  912. 7, end1, cend1);
  913. MY_VALIDATE_SUBSTRING_SPEC_UCOPY (2, s2, cstr2,
  914. 8, start2, cstart2,
  915. 9, end2, cend2);
  916. SCM_VALIDATE_PROC (3, proc_lt);
  917. SCM_VALIDATE_PROC (4, proc_eq);
  918. SCM_VALIDATE_PROC (5, proc_gt);
  919. while (cstart1 < cend1 && cstart2 < cend2)
  920. {
  921. if (cstr1[cstart1] < cstr2[cstart2])
  922. {
  923. proc = proc_lt;
  924. goto ret;
  925. }
  926. else if (cstr1[cstart1] > cstr2[cstart2])
  927. {
  928. proc = proc_gt;
  929. goto ret;
  930. }
  931. cstart1++;
  932. cstart2++;
  933. }
  934. if (cstart1 < cend1)
  935. proc = proc_gt;
  936. else if (cstart2 < cend2)
  937. proc = proc_lt;
  938. else
  939. proc = proc_eq;
  940. ret:
  941. scm_remember_upto_here_2 (s1, s2);
  942. return scm_call_1 (proc, scm_from_size_t (cstart1));
  943. }
  944. #undef FUNC_NAME
  945. SCM_DEFINE (scm_string_compare_ci, "string-compare-ci", 5, 4, 0,
  946. (SCM s1, SCM s2, SCM proc_lt, SCM proc_eq, SCM proc_gt, SCM start1, SCM end1, SCM start2, SCM end2),
  947. "Apply @var{proc_lt}, @var{proc_eq}, @var{proc_gt} to the\n"
  948. "mismatch index, depending upon whether @var{s1} is less than,\n"
  949. "equal to, or greater than @var{s2}. The mismatch index is the\n"
  950. "largest index @var{i} such that for every 0 <= @var{j} <\n"
  951. "@var{i}, @var{s1}[@var{j}] = @var{s2}[@var{j}] -- that is,\n"
  952. "@var{i} is the first position that does not match. The\n"
  953. "character comparison is done case-insensitively.")
  954. #define FUNC_NAME s_scm_string_compare_ci
  955. {
  956. const unsigned char *cstr1, *cstr2;
  957. size_t cstart1, cend1, cstart2, cend2;
  958. SCM proc;
  959. MY_VALIDATE_SUBSTRING_SPEC_UCOPY (1, s1, cstr1,
  960. 6, start1, cstart1,
  961. 7, end1, cend1);
  962. MY_VALIDATE_SUBSTRING_SPEC_UCOPY (2, s2, cstr2,
  963. 8, start2, cstart2,
  964. 9, end2, cend2);
  965. SCM_VALIDATE_PROC (3, proc_lt);
  966. SCM_VALIDATE_PROC (4, proc_eq);
  967. SCM_VALIDATE_PROC (5, proc_gt);
  968. while (cstart1 < cend1 && cstart2 < cend2)
  969. {
  970. if (scm_c_downcase (cstr1[cstart1]) < scm_c_downcase (cstr2[cstart2]))
  971. {
  972. proc = proc_lt;
  973. goto ret;
  974. }
  975. else if (scm_c_downcase (cstr1[cstart1])
  976. > scm_c_downcase (cstr2[cstart2]))
  977. {
  978. proc = proc_gt;
  979. goto ret;
  980. }
  981. cstart1++;
  982. cstart2++;
  983. }
  984. if (cstart1 < cend1)
  985. proc = proc_gt;
  986. else if (cstart2 < cend2)
  987. proc = proc_lt;
  988. else
  989. proc = proc_eq;
  990. ret:
  991. scm_remember_upto_here (s1, s2);
  992. return scm_call_1 (proc, scm_from_size_t (cstart1));
  993. }
  994. #undef FUNC_NAME
  995. SCM_DEFINE (scm_string_eq, "string=", 2, 4, 0,
  996. (SCM s1, SCM s2, SCM start1, SCM end1, SCM start2, SCM end2),
  997. "Return @code{#f} if @var{s1} and @var{s2} are not equal, a true\n"
  998. "value otherwise.")
  999. #define FUNC_NAME s_scm_string_eq
  1000. {
  1001. const char *cstr1, *cstr2;
  1002. size_t cstart1, cend1, cstart2, cend2;
  1003. MY_VALIDATE_SUBSTRING_SPEC_COPY (1, s1, cstr1,
  1004. 3, start1, cstart1,
  1005. 4, end1, cend1);
  1006. MY_VALIDATE_SUBSTRING_SPEC_COPY (2, s2, cstr2,
  1007. 5, start2, cstart2,
  1008. 6, end2, cend2);
  1009. if ((cend1 - cstart1) != (cend2 - cstart2))
  1010. goto false;
  1011. while (cstart1 < cend1)
  1012. {
  1013. if (cstr1[cstart1] < cstr2[cstart2])
  1014. goto false;
  1015. else if (cstr1[cstart1] > cstr2[cstart2])
  1016. goto false;
  1017. cstart1++;
  1018. cstart2++;
  1019. }
  1020. scm_remember_upto_here_2 (s1, s2);
  1021. return scm_from_size_t (cstart1);
  1022. false:
  1023. scm_remember_upto_here_2 (s1, s2);
  1024. return SCM_BOOL_F;
  1025. }
  1026. #undef FUNC_NAME
  1027. SCM_DEFINE (scm_string_neq, "string<>", 2, 4, 0,
  1028. (SCM s1, SCM s2, SCM start1, SCM end1, SCM start2, SCM end2),
  1029. "Return @code{#f} if @var{s1} and @var{s2} are equal, a true\n"
  1030. "value otherwise.")
  1031. #define FUNC_NAME s_scm_string_neq
  1032. {
  1033. const char *cstr1, *cstr2;
  1034. size_t cstart1, cend1, cstart2, cend2;
  1035. MY_VALIDATE_SUBSTRING_SPEC_COPY (1, s1, cstr1,
  1036. 3, start1, cstart1,
  1037. 4, end1, cend1);
  1038. MY_VALIDATE_SUBSTRING_SPEC_COPY (2, s2, cstr2,
  1039. 5, start2, cstart2,
  1040. 6, end2, cend2);
  1041. while (cstart1 < cend1 && cstart2 < cend2)
  1042. {
  1043. if (cstr1[cstart1] < cstr2[cstart2])
  1044. goto true;
  1045. else if (cstr1[cstart1] > cstr2[cstart2])
  1046. goto true;
  1047. cstart1++;
  1048. cstart2++;
  1049. }
  1050. if (cstart1 < cend1)
  1051. goto true;
  1052. else if (cstart2 < cend2)
  1053. goto true;
  1054. else
  1055. goto false;
  1056. true:
  1057. scm_remember_upto_here_2 (s1, s2);
  1058. return scm_from_size_t (cstart1);
  1059. false:
  1060. scm_remember_upto_here_2 (s1, s2);
  1061. return SCM_BOOL_F;
  1062. }
  1063. #undef FUNC_NAME
  1064. SCM_DEFINE (scm_string_lt, "string<", 2, 4, 0,
  1065. (SCM s1, SCM s2, SCM start1, SCM end1, SCM start2, SCM end2),
  1066. "Return @code{#f} if @var{s1} is greater or equal to @var{s2}, a\n"
  1067. "true value otherwise.")
  1068. #define FUNC_NAME s_scm_string_lt
  1069. {
  1070. const unsigned char *cstr1, *cstr2;
  1071. size_t cstart1, cend1, cstart2, cend2;
  1072. MY_VALIDATE_SUBSTRING_SPEC_UCOPY (1, s1, cstr1,
  1073. 3, start1, cstart1,
  1074. 4, end1, cend1);
  1075. MY_VALIDATE_SUBSTRING_SPEC_UCOPY (2, s2, cstr2,
  1076. 5, start2, cstart2,
  1077. 6, end2, cend2);
  1078. while (cstart1 < cend1 && cstart2 < cend2)
  1079. {
  1080. if (cstr1[cstart1] < cstr2[cstart2])
  1081. goto true;
  1082. else if (cstr1[cstart1] > cstr2[cstart2])
  1083. goto false;
  1084. cstart1++;
  1085. cstart2++;
  1086. }
  1087. if (cstart1 < cend1)
  1088. goto false;
  1089. else if (cstart2 < cend2)
  1090. goto true;
  1091. else
  1092. goto false;
  1093. true:
  1094. scm_remember_upto_here_2 (s1, s2);
  1095. return scm_from_size_t (cstart1);
  1096. false:
  1097. scm_remember_upto_here_2 (s1, s2);
  1098. return SCM_BOOL_F;
  1099. }
  1100. #undef FUNC_NAME
  1101. SCM_DEFINE (scm_string_gt, "string>", 2, 4, 0,
  1102. (SCM s1, SCM s2, SCM start1, SCM end1, SCM start2, SCM end2),
  1103. "Return @code{#f} if @var{s1} is less or equal to @var{s2}, a\n"
  1104. "true value otherwise.")
  1105. #define FUNC_NAME s_scm_string_gt
  1106. {
  1107. const unsigned char *cstr1, *cstr2;
  1108. size_t cstart1, cend1, cstart2, cend2;
  1109. MY_VALIDATE_SUBSTRING_SPEC_UCOPY (1, s1, cstr1,
  1110. 3, start1, cstart1,
  1111. 4, end1, cend1);
  1112. MY_VALIDATE_SUBSTRING_SPEC_UCOPY (2, s2, cstr2,
  1113. 5, start2, cstart2,
  1114. 6, end2, cend2);
  1115. while (cstart1 < cend1 && cstart2 < cend2)
  1116. {
  1117. if (cstr1[cstart1] < cstr2[cstart2])
  1118. goto false;
  1119. else if (cstr1[cstart1] > cstr2[cstart2])
  1120. goto true;
  1121. cstart1++;
  1122. cstart2++;
  1123. }
  1124. if (cstart1 < cend1)
  1125. goto true;
  1126. else if (cstart2 < cend2)
  1127. goto false;
  1128. else
  1129. goto false;
  1130. true:
  1131. scm_remember_upto_here_2 (s1, s2);
  1132. return scm_from_size_t (cstart1);
  1133. false:
  1134. scm_remember_upto_here_2 (s1, s2);
  1135. return SCM_BOOL_F;
  1136. }
  1137. #undef FUNC_NAME
  1138. SCM_DEFINE (scm_string_le, "string<=", 2, 4, 0,
  1139. (SCM s1, SCM s2, SCM start1, SCM end1, SCM start2, SCM end2),
  1140. "Return @code{#f} if @var{s1} is greater to @var{s2}, a true\n"
  1141. "value otherwise.")
  1142. #define FUNC_NAME s_scm_string_le
  1143. {
  1144. const unsigned char *cstr1, *cstr2;
  1145. size_t cstart1, cend1, cstart2, cend2;
  1146. MY_VALIDATE_SUBSTRING_SPEC_UCOPY (1, s1, cstr1,
  1147. 3, start1, cstart1,
  1148. 4, end1, cend1);
  1149. MY_VALIDATE_SUBSTRING_SPEC_UCOPY (2, s2, cstr2,
  1150. 5, start2, cstart2,
  1151. 6, end2, cend2);
  1152. while (cstart1 < cend1 && cstart2 < cend2)
  1153. {
  1154. if (cstr1[cstart1] < cstr2[cstart2])
  1155. goto true;
  1156. else if (cstr1[cstart1] > cstr2[cstart2])
  1157. goto false;
  1158. cstart1++;
  1159. cstart2++;
  1160. }
  1161. if (cstart1 < cend1)
  1162. goto false;
  1163. else if (cstart2 < cend2)
  1164. goto true;
  1165. else
  1166. goto true;
  1167. true:
  1168. scm_remember_upto_here_2 (s1, s2);
  1169. return scm_from_size_t (cstart1);
  1170. false:
  1171. scm_remember_upto_here_2 (s1, s2);
  1172. return SCM_BOOL_F;
  1173. }
  1174. #undef FUNC_NAME
  1175. SCM_DEFINE (scm_string_ge, "string>=", 2, 4, 0,
  1176. (SCM s1, SCM s2, SCM start1, SCM end1, SCM start2, SCM end2),
  1177. "Return @code{#f} if @var{s1} is less to @var{s2}, a true value\n"
  1178. "otherwise.")
  1179. #define FUNC_NAME s_scm_string_ge
  1180. {
  1181. const unsigned char *cstr1, *cstr2;
  1182. size_t cstart1, cend1, cstart2, cend2;
  1183. MY_VALIDATE_SUBSTRING_SPEC_UCOPY (1, s1, cstr1,
  1184. 3, start1, cstart1,
  1185. 4, end1, cend1);
  1186. MY_VALIDATE_SUBSTRING_SPEC_UCOPY (2, s2, cstr2,
  1187. 5, start2, cstart2,
  1188. 6, end2, cend2);
  1189. while (cstart1 < cend1 && cstart2 < cend2)
  1190. {
  1191. if (cstr1[cstart1] < cstr2[cstart2])
  1192. goto false;
  1193. else if (cstr1[cstart1] > cstr2[cstart2])
  1194. goto true;
  1195. cstart1++;
  1196. cstart2++;
  1197. }
  1198. if (cstart1 < cend1)
  1199. goto true;
  1200. else if (cstart2 < cend2)
  1201. goto false;
  1202. else
  1203. goto true;
  1204. true:
  1205. scm_remember_upto_here_2 (s1, s2);
  1206. return scm_from_size_t (cstart1);
  1207. false:
  1208. scm_remember_upto_here_2 (s1, s2);
  1209. return SCM_BOOL_F;
  1210. }
  1211. #undef FUNC_NAME
  1212. SCM_DEFINE (scm_string_ci_eq, "string-ci=", 2, 4, 0,
  1213. (SCM s1, SCM s2, SCM start1, SCM end1, SCM start2, SCM end2),
  1214. "Return @code{#f} if @var{s1} and @var{s2} are not equal, a true\n"
  1215. "value otherwise. The character comparison is done\n"
  1216. "case-insensitively.")
  1217. #define FUNC_NAME s_scm_string_ci_eq
  1218. {
  1219. const char *cstr1, *cstr2;
  1220. size_t cstart1, cend1, cstart2, cend2;
  1221. MY_VALIDATE_SUBSTRING_SPEC_COPY (1, s1, cstr1,
  1222. 3, start1, cstart1,
  1223. 4, end1, cend1);
  1224. MY_VALIDATE_SUBSTRING_SPEC_COPY (2, s2, cstr2,
  1225. 5, start2, cstart2,
  1226. 6, end2, cend2);
  1227. while (cstart1 < cend1 && cstart2 < cend2)
  1228. {
  1229. if (scm_c_downcase (cstr1[cstart1]) < scm_c_downcase (cstr2[cstart2]))
  1230. goto false;
  1231. else if (scm_c_downcase (cstr1[cstart1]) > scm_c_downcase (cstr2[cstart2]))
  1232. goto false;
  1233. cstart1++;
  1234. cstart2++;
  1235. }
  1236. if (cstart1 < cend1)
  1237. goto false;
  1238. else if (cstart2 < cend2)
  1239. goto false;
  1240. else
  1241. goto true;
  1242. true:
  1243. scm_remember_upto_here_2 (s1, s2);
  1244. return scm_from_size_t (cstart1);
  1245. false:
  1246. scm_remember_upto_here_2 (s1, s2);
  1247. return SCM_BOOL_F;
  1248. }
  1249. #undef FUNC_NAME
  1250. SCM_DEFINE (scm_string_ci_neq, "string-ci<>", 2, 4, 0,
  1251. (SCM s1, SCM s2, SCM start1, SCM end1, SCM start2, SCM end2),
  1252. "Return @code{#f} if @var{s1} and @var{s2} are equal, a true\n"
  1253. "value otherwise. The character comparison is done\n"
  1254. "case-insensitively.")
  1255. #define FUNC_NAME s_scm_string_ci_neq
  1256. {
  1257. const char *cstr1, *cstr2;
  1258. size_t cstart1, cend1, cstart2, cend2;
  1259. MY_VALIDATE_SUBSTRING_SPEC_COPY (1, s1, cstr1,
  1260. 3, start1, cstart1,
  1261. 4, end1, cend1);
  1262. MY_VALIDATE_SUBSTRING_SPEC_COPY (2, s2, cstr2,
  1263. 5, start2, cstart2,
  1264. 6, end2, cend2);
  1265. while (cstart1 < cend1 && cstart2 < cend2)
  1266. {
  1267. if (scm_c_downcase (cstr1[cstart1]) < scm_c_downcase (cstr2[cstart2]))
  1268. goto true;
  1269. else if (scm_c_downcase (cstr1[cstart1]) > scm_c_downcase (cstr2[cstart2]))
  1270. goto true;
  1271. cstart1++;
  1272. cstart2++;
  1273. }
  1274. if (cstart1 < cend1)
  1275. goto true;
  1276. else if (cstart2 < cend2)
  1277. goto true;
  1278. else
  1279. goto false;
  1280. true:
  1281. scm_remember_upto_here_2 (s1, s2);
  1282. return scm_from_size_t (cstart1);
  1283. false:
  1284. scm_remember_upto_here_2 (s1, s2);
  1285. return SCM_BOOL_F;
  1286. }
  1287. #undef FUNC_NAME
  1288. SCM_DEFINE (scm_string_ci_lt, "string-ci<", 2, 4, 0,
  1289. (SCM s1, SCM s2, SCM start1, SCM end1, SCM start2, SCM end2),
  1290. "Return @code{#f} if @var{s1} is greater or equal to @var{s2}, a\n"
  1291. "true value otherwise. The character comparison is done\n"
  1292. "case-insensitively.")
  1293. #define FUNC_NAME s_scm_string_ci_lt
  1294. {
  1295. const unsigned char *cstr1, *cstr2;
  1296. size_t cstart1, cend1, cstart2, cend2;
  1297. MY_VALIDATE_SUBSTRING_SPEC_UCOPY (1, s1, cstr1,
  1298. 3, start1, cstart1,
  1299. 4, end1, cend1);
  1300. MY_VALIDATE_SUBSTRING_SPEC_UCOPY (2, s2, cstr2,
  1301. 5, start2, cstart2,
  1302. 6, end2, cend2);
  1303. while (cstart1 < cend1 && cstart2 < cend2)
  1304. {
  1305. if (scm_c_downcase (cstr1[cstart1]) < scm_c_downcase (cstr2[cstart2]))
  1306. goto true;
  1307. else if (scm_c_downcase (cstr1[cstart1]) > scm_c_downcase (cstr2[cstart2]))
  1308. goto false;
  1309. cstart1++;
  1310. cstart2++;
  1311. }
  1312. if (cstart1 < cend1)
  1313. goto false;
  1314. else if (cstart2 < cend2)
  1315. goto true;
  1316. else
  1317. goto false;
  1318. true:
  1319. scm_remember_upto_here_2 (s1, s2);
  1320. return scm_from_size_t (cstart1);
  1321. false:
  1322. scm_remember_upto_here_2 (s1, s2);
  1323. return SCM_BOOL_F;
  1324. }
  1325. #undef FUNC_NAME
  1326. SCM_DEFINE (scm_string_ci_gt, "string-ci>", 2, 4, 0,
  1327. (SCM s1, SCM s2, SCM start1, SCM end1, SCM start2, SCM end2),
  1328. "Return @code{#f} if @var{s1} is less or equal to @var{s2}, a\n"
  1329. "true value otherwise. The character comparison is done\n"
  1330. "case-insensitively.")
  1331. #define FUNC_NAME s_scm_string_ci_gt
  1332. {
  1333. const unsigned char *cstr1, *cstr2;
  1334. size_t cstart1, cend1, cstart2, cend2;
  1335. MY_VALIDATE_SUBSTRING_SPEC_UCOPY (1, s1, cstr1,
  1336. 3, start1, cstart1,
  1337. 4, end1, cend1);
  1338. MY_VALIDATE_SUBSTRING_SPEC_UCOPY (2, s2, cstr2,
  1339. 5, start2, cstart2,
  1340. 6, end2, cend2);
  1341. while (cstart1 < cend1 && cstart2 < cend2)
  1342. {
  1343. if (scm_c_downcase (cstr1[cstart1]) < scm_c_downcase (cstr2[cstart2]))
  1344. goto false;
  1345. else if (scm_c_downcase (cstr1[cstart1]) > scm_c_downcase (cstr2[cstart2]))
  1346. goto true;
  1347. cstart1++;
  1348. cstart2++;
  1349. }
  1350. if (cstart1 < cend1)
  1351. goto true;
  1352. else if (cstart2 < cend2)
  1353. goto false;
  1354. else
  1355. goto false;
  1356. true:
  1357. scm_remember_upto_here_2 (s1, s2);
  1358. return scm_from_size_t (cstart1);
  1359. false:
  1360. scm_remember_upto_here_2 (s1, s2);
  1361. return SCM_BOOL_F;
  1362. }
  1363. #undef FUNC_NAME
  1364. SCM_DEFINE (scm_string_ci_le, "string-ci<=", 2, 4, 0,
  1365. (SCM s1, SCM s2, SCM start1, SCM end1, SCM start2, SCM end2),
  1366. "Return @code{#f} if @var{s1} is greater to @var{s2}, a true\n"
  1367. "value otherwise. The character comparison is done\n"
  1368. "case-insensitively.")
  1369. #define FUNC_NAME s_scm_string_ci_le
  1370. {
  1371. const unsigned char *cstr1, *cstr2;
  1372. size_t cstart1, cend1, cstart2, cend2;
  1373. MY_VALIDATE_SUBSTRING_SPEC_UCOPY (1, s1, cstr1,
  1374. 3, start1, cstart1,
  1375. 4, end1, cend1);
  1376. MY_VALIDATE_SUBSTRING_SPEC_UCOPY (2, s2, cstr2,
  1377. 5, start2, cstart2,
  1378. 6, end2, cend2);
  1379. while (cstart1 < cend1 && cstart2 < cend2)
  1380. {
  1381. if (scm_c_downcase (cstr1[cstart1]) < scm_c_downcase (cstr2[cstart2]))
  1382. goto true;
  1383. else if (scm_c_downcase (cstr1[cstart1]) > scm_c_downcase (cstr2[cstart2]))
  1384. goto false;
  1385. cstart1++;
  1386. cstart2++;
  1387. }
  1388. if (cstart1 < cend1)
  1389. goto false;
  1390. else if (cstart2 < cend2)
  1391. goto true;
  1392. else
  1393. goto true;
  1394. true:
  1395. scm_remember_upto_here_2 (s1, s2);
  1396. return scm_from_size_t (cstart1);
  1397. false:
  1398. scm_remember_upto_here_2 (s1, s2);
  1399. return SCM_BOOL_F;
  1400. }
  1401. #undef FUNC_NAME
  1402. SCM_DEFINE (scm_string_ci_ge, "string-ci>=", 2, 4, 0,
  1403. (SCM s1, SCM s2, SCM start1, SCM end1, SCM start2, SCM end2),
  1404. "Return @code{#f} if @var{s1} is less to @var{s2}, a true value\n"
  1405. "otherwise. The character comparison is done\n"
  1406. "case-insensitively.")
  1407. #define FUNC_NAME s_scm_string_ci_ge
  1408. {
  1409. const unsigned char *cstr1, *cstr2;
  1410. size_t cstart1, cend1, cstart2, cend2;
  1411. MY_VALIDATE_SUBSTRING_SPEC_UCOPY (1, s1, cstr1,
  1412. 3, start1, cstart1,
  1413. 4, end1, cend1);
  1414. MY_VALIDATE_SUBSTRING_SPEC_UCOPY (2, s2, cstr2,
  1415. 5, start2, cstart2,
  1416. 6, end2, cend2);
  1417. while (cstart1 < cend1 && cstart2 < cend2)
  1418. {
  1419. if (scm_c_downcase (cstr1[cstart1]) < scm_c_downcase (cstr2[cstart2]))
  1420. goto false;
  1421. else if (scm_c_downcase (cstr1[cstart1]) > scm_c_downcase (cstr2[cstart2]))
  1422. goto true;
  1423. cstart1++;
  1424. cstart2++;
  1425. }
  1426. if (cstart1 < cend1)
  1427. goto true;
  1428. else if (cstart2 < cend2)
  1429. goto false;
  1430. else
  1431. goto true;
  1432. true:
  1433. scm_remember_upto_here_2 (s1, s2);
  1434. return scm_from_size_t (cstart1);
  1435. false:
  1436. scm_remember_upto_here_2 (s1, s2);
  1437. return SCM_BOOL_F;
  1438. }
  1439. #undef FUNC_NAME
  1440. SCM_DEFINE (scm_substring_hash, "string-hash", 1, 3, 0,
  1441. (SCM s, SCM bound, SCM start, SCM end),
  1442. "Compute a hash value for @var{S}. the optional argument "
  1443. "@var{bound} is a non-negative exact "
  1444. "integer specifying the range of the hash function. "
  1445. "A positive value restricts the return value to the "
  1446. "range [0,bound).")
  1447. #define FUNC_NAME s_scm_substring_hash
  1448. {
  1449. if (SCM_UNBNDP (bound))
  1450. bound = scm_from_intmax (SCM_MOST_POSITIVE_FIXNUM);
  1451. if (SCM_UNBNDP (start))
  1452. start = SCM_INUM0;
  1453. return scm_hash (scm_substring_shared (s, start, end), bound);
  1454. }
  1455. #undef FUNC_NAME
  1456. SCM_DEFINE (scm_substring_hash_ci, "string-hash-ci", 1, 3, 0,
  1457. (SCM s, SCM bound, SCM start, SCM end),
  1458. "Compute a hash value for @var{S}. the optional argument "
  1459. "@var{bound} is a non-negative exact "
  1460. "integer specifying the range of the hash function. "
  1461. "A positive value restricts the return value to the "
  1462. "range [0,bound).")
  1463. #define FUNC_NAME s_scm_substring_hash_ci
  1464. {
  1465. return scm_substring_hash (scm_substring_downcase (s, start, end),
  1466. bound,
  1467. SCM_UNDEFINED, SCM_UNDEFINED);
  1468. }
  1469. #undef FUNC_NAME
  1470. SCM_DEFINE (scm_string_prefix_length, "string-prefix-length", 2, 4, 0,
  1471. (SCM s1, SCM s2, SCM start1, SCM end1, SCM start2, SCM end2),
  1472. "Return the length of the longest common prefix of the two\n"
  1473. "strings.")
  1474. #define FUNC_NAME s_scm_string_prefix_length
  1475. {
  1476. const char *cstr1, *cstr2;
  1477. size_t cstart1, cend1, cstart2, cend2;
  1478. size_t len = 0;
  1479. MY_VALIDATE_SUBSTRING_SPEC_COPY (1, s1, cstr1,
  1480. 3, start1, cstart1,
  1481. 4, end1, cend1);
  1482. MY_VALIDATE_SUBSTRING_SPEC_COPY (2, s2, cstr2,
  1483. 5, start2, cstart2,
  1484. 6, end2, cend2);
  1485. while (cstart1 < cend1 && cstart2 < cend2)
  1486. {
  1487. if (cstr1[cstart1] != cstr2[cstart2])
  1488. goto ret;
  1489. len++;
  1490. cstart1++;
  1491. cstart2++;
  1492. }
  1493. ret:
  1494. scm_remember_upto_here_2 (s1, s2);
  1495. return scm_from_size_t (len);
  1496. }
  1497. #undef FUNC_NAME
  1498. SCM_DEFINE (scm_string_prefix_length_ci, "string-prefix-length-ci", 2, 4, 0,
  1499. (SCM s1, SCM s2, SCM start1, SCM end1, SCM start2, SCM end2),
  1500. "Return the length of the longest common prefix of the two\n"
  1501. "strings, ignoring character case.")
  1502. #define FUNC_NAME s_scm_string_prefix_length_ci
  1503. {
  1504. const char *cstr1, *cstr2;
  1505. size_t cstart1, cend1, cstart2, cend2;
  1506. size_t len = 0;
  1507. MY_VALIDATE_SUBSTRING_SPEC_COPY (1, s1, cstr1,
  1508. 3, start1, cstart1,
  1509. 4, end1, cend1);
  1510. MY_VALIDATE_SUBSTRING_SPEC_COPY (2, s2, cstr2,
  1511. 5, start2, cstart2,
  1512. 6, end2, cend2);
  1513. while (cstart1 < cend1 && cstart2 < cend2)
  1514. {
  1515. if (scm_c_downcase (cstr1[cstart1]) != scm_c_downcase (cstr2[cstart2]))
  1516. goto ret;
  1517. len++;
  1518. cstart1++;
  1519. cstart2++;
  1520. }
  1521. ret:
  1522. scm_remember_upto_here_2 (s1, s2);
  1523. return scm_from_size_t (len);
  1524. }
  1525. #undef FUNC_NAME
  1526. SCM_DEFINE (scm_string_suffix_length, "string-suffix-length", 2, 4, 0,
  1527. (SCM s1, SCM s2, SCM start1, SCM end1, SCM start2, SCM end2),
  1528. "Return the length of the longest common suffix of the two\n"
  1529. "strings.")
  1530. #define FUNC_NAME s_scm_string_suffix_length
  1531. {
  1532. const char *cstr1, *cstr2;
  1533. size_t cstart1, cend1, cstart2, cend2;
  1534. size_t len = 0;
  1535. MY_VALIDATE_SUBSTRING_SPEC_COPY (1, s1, cstr1,
  1536. 3, start1, cstart1,
  1537. 4, end1, cend1);
  1538. MY_VALIDATE_SUBSTRING_SPEC_COPY (2, s2, cstr2,
  1539. 5, start2, cstart2,
  1540. 6, end2, cend2);
  1541. while (cstart1 < cend1 && cstart2 < cend2)
  1542. {
  1543. cend1--;
  1544. cend2--;
  1545. if (cstr1[cend1] != cstr2[cend2])
  1546. goto ret;
  1547. len++;
  1548. }
  1549. ret:
  1550. scm_remember_upto_here_2 (s1, s2);
  1551. return scm_from_size_t (len);
  1552. }
  1553. #undef FUNC_NAME
  1554. SCM_DEFINE (scm_string_suffix_length_ci, "string-suffix-length-ci", 2, 4, 0,
  1555. (SCM s1, SCM s2, SCM start1, SCM end1, SCM start2, SCM end2),
  1556. "Return the length of the longest common suffix of the two\n"
  1557. "strings, ignoring character case.")
  1558. #define FUNC_NAME s_scm_string_suffix_length_ci
  1559. {
  1560. const char *cstr1, *cstr2;
  1561. size_t cstart1, cend1, cstart2, cend2;
  1562. size_t len = 0;
  1563. MY_VALIDATE_SUBSTRING_SPEC_COPY (1, s1, cstr1,
  1564. 3, start1, cstart1,
  1565. 4, end1, cend1);
  1566. MY_VALIDATE_SUBSTRING_SPEC_COPY (2, s2, cstr2,
  1567. 5, start2, cstart2,
  1568. 6, end2, cend2);
  1569. while (cstart1 < cend1 && cstart2 < cend2)
  1570. {
  1571. cend1--;
  1572. cend2--;
  1573. if (scm_c_downcase (cstr1[cend1]) != scm_c_downcase (cstr2[cend2]))
  1574. goto ret;
  1575. len++;
  1576. }
  1577. ret:
  1578. scm_remember_upto_here_2 (s1, s2);
  1579. return scm_from_size_t (len);
  1580. }
  1581. #undef FUNC_NAME
  1582. SCM_DEFINE (scm_string_prefix_p, "string-prefix?", 2, 4, 0,
  1583. (SCM s1, SCM s2, SCM start1, SCM end1, SCM start2, SCM end2),
  1584. "Is @var{s1} a prefix of @var{s2}?")
  1585. #define FUNC_NAME s_scm_string_prefix_p
  1586. {
  1587. const char *cstr1, *cstr2;
  1588. size_t cstart1, cend1, cstart2, cend2;
  1589. size_t len = 0, len1;
  1590. MY_VALIDATE_SUBSTRING_SPEC_COPY (1, s1, cstr1,
  1591. 3, start1, cstart1,
  1592. 4, end1, cend1);
  1593. MY_VALIDATE_SUBSTRING_SPEC_COPY (2, s2, cstr2,
  1594. 5, start2, cstart2,
  1595. 6, end2, cend2);
  1596. len1 = cend1 - cstart1;
  1597. while (cstart1 < cend1 && cstart2 < cend2)
  1598. {
  1599. if (cstr1[cstart1] != cstr2[cstart2])
  1600. goto ret;
  1601. len++;
  1602. cstart1++;
  1603. cstart2++;
  1604. }
  1605. ret:
  1606. scm_remember_upto_here_2 (s1, s2);
  1607. return scm_from_bool (len == len1);
  1608. }
  1609. #undef FUNC_NAME
  1610. SCM_DEFINE (scm_string_prefix_ci_p, "string-prefix-ci?", 2, 4, 0,
  1611. (SCM s1, SCM s2, SCM start1, SCM end1, SCM start2, SCM end2),
  1612. "Is @var{s1} a prefix of @var{s2}, ignoring character case?")
  1613. #define FUNC_NAME s_scm_string_prefix_ci_p
  1614. {
  1615. const char *cstr1, *cstr2;
  1616. size_t cstart1, cend1, cstart2, cend2;
  1617. size_t len = 0, len1;
  1618. MY_VALIDATE_SUBSTRING_SPEC_COPY (1, s1, cstr1,
  1619. 3, start1, cstart1,
  1620. 4, end1, cend1);
  1621. MY_VALIDATE_SUBSTRING_SPEC_COPY (2, s2, cstr2,
  1622. 5, start2, cstart2,
  1623. 6, end2, cend2);
  1624. len1 = cend1 - cstart1;
  1625. while (cstart1 < cend1 && cstart2 < cend2)
  1626. {
  1627. if (scm_c_downcase (cstr1[cstart1]) != scm_c_downcase (cstr2[cstart2]))
  1628. goto ret;
  1629. len++;
  1630. cstart1++;
  1631. cstart2++;
  1632. }
  1633. ret:
  1634. scm_remember_upto_here_2 (s1, s2);
  1635. return scm_from_bool (len == len1);
  1636. }
  1637. #undef FUNC_NAME
  1638. SCM_DEFINE (scm_string_suffix_p, "string-suffix?", 2, 4, 0,
  1639. (SCM s1, SCM s2, SCM start1, SCM end1, SCM start2, SCM end2),
  1640. "Is @var{s1} a suffix of @var{s2}?")
  1641. #define FUNC_NAME s_scm_string_suffix_p
  1642. {
  1643. const char *cstr1, *cstr2;
  1644. size_t cstart1, cend1, cstart2, cend2;
  1645. size_t len = 0, len1;
  1646. MY_VALIDATE_SUBSTRING_SPEC_COPY (1, s1, cstr1,
  1647. 3, start1, cstart1,
  1648. 4, end1, cend1);
  1649. MY_VALIDATE_SUBSTRING_SPEC_COPY (2, s2, cstr2,
  1650. 5, start2, cstart2,
  1651. 6, end2, cend2);
  1652. len1 = cend1 - cstart1;
  1653. while (cstart1 < cend1 && cstart2 < cend2)
  1654. {
  1655. cend1--;
  1656. cend2--;
  1657. if (cstr1[cend1] != cstr2[cend2])
  1658. goto ret;
  1659. len++;
  1660. }
  1661. ret:
  1662. scm_remember_upto_here_2 (s1, s2);
  1663. return scm_from_bool (len == len1);
  1664. }
  1665. #undef FUNC_NAME
  1666. SCM_DEFINE (scm_string_suffix_ci_p, "string-suffix-ci?", 2, 4, 0,
  1667. (SCM s1, SCM s2, SCM start1, SCM end1, SCM start2, SCM end2),
  1668. "Is @var{s1} a suffix of @var{s2}, ignoring character case?")
  1669. #define FUNC_NAME s_scm_string_suffix_ci_p
  1670. {
  1671. const char *cstr1, *cstr2;
  1672. size_t cstart1, cend1, cstart2, cend2;
  1673. size_t len = 0, len1;
  1674. MY_VALIDATE_SUBSTRING_SPEC_COPY (1, s1, cstr1,
  1675. 3, start1, cstart1,
  1676. 4, end1, cend1);
  1677. MY_VALIDATE_SUBSTRING_SPEC_COPY (2, s2, cstr2,
  1678. 5, start2, cstart2,
  1679. 6, end2, cend2);
  1680. len1 = cend1 - cstart1;
  1681. while (cstart1 < cend1 && cstart2 < cend2)
  1682. {
  1683. cend1--;
  1684. cend2--;
  1685. if (scm_c_downcase (cstr1[cend1]) != scm_c_downcase (cstr2[cend2]))
  1686. goto ret;
  1687. len++;
  1688. }
  1689. ret:
  1690. scm_remember_upto_here_2 (s1, s2);
  1691. return scm_from_bool (len == len1);
  1692. }
  1693. #undef FUNC_NAME
  1694. SCM_DEFINE (scm_string_index, "string-index", 2, 2, 0,
  1695. (SCM s, SCM char_pred, SCM start, SCM end),
  1696. "Search through the string @var{s} from left to right, returning\n"
  1697. "the index of the first occurence of a character which\n"
  1698. "\n"
  1699. "@itemize @bullet\n"
  1700. "@item\n"
  1701. "equals @var{char_pred}, if it is character,\n"
  1702. "\n"
  1703. "@item\n"
  1704. "satisifies the predicate @var{char_pred}, if it is a procedure,\n"
  1705. "\n"
  1706. "@item\n"
  1707. "is in the set @var{char_pred}, if it is a character set.\n"
  1708. "@end itemize")
  1709. #define FUNC_NAME s_scm_string_index
  1710. {
  1711. const char *cstr;
  1712. size_t cstart, cend;
  1713. MY_VALIDATE_SUBSTRING_SPEC_COPY (1, s, cstr,
  1714. 3, start, cstart,
  1715. 4, end, cend);
  1716. if (SCM_CHARP (char_pred))
  1717. {
  1718. char cchr = SCM_CHAR (char_pred);
  1719. while (cstart < cend)
  1720. {
  1721. if (cchr == cstr[cstart])
  1722. goto found;
  1723. cstart++;
  1724. }
  1725. }
  1726. else if (SCM_CHARSETP (char_pred))
  1727. {
  1728. while (cstart < cend)
  1729. {
  1730. if (SCM_CHARSET_GET (char_pred, cstr[cstart]))
  1731. goto found;
  1732. cstart++;
  1733. }
  1734. }
  1735. else
  1736. {
  1737. scm_t_trampoline_1 pred_tramp = scm_trampoline_1 (char_pred);
  1738. SCM_ASSERT (pred_tramp, char_pred, SCM_ARG2, FUNC_NAME);
  1739. while (cstart < cend)
  1740. {
  1741. SCM res;
  1742. res = pred_tramp (char_pred, SCM_MAKE_CHAR (cstr[cstart]));
  1743. if (scm_is_true (res))
  1744. goto found;
  1745. cstr = scm_i_string_chars (s);
  1746. cstart++;
  1747. }
  1748. }
  1749. scm_remember_upto_here_1 (s);
  1750. return SCM_BOOL_F;
  1751. found:
  1752. scm_remember_upto_here_1 (s);
  1753. return scm_from_size_t (cstart);
  1754. }
  1755. #undef FUNC_NAME
  1756. SCM_DEFINE (scm_string_index_right, "string-index-right", 2, 2, 0,
  1757. (SCM s, SCM char_pred, SCM start, SCM end),
  1758. "Search through the string @var{s} from right to left, returning\n"
  1759. "the index of the last occurence of a character which\n"
  1760. "\n"
  1761. "@itemize @bullet\n"
  1762. "@item\n"
  1763. "equals @var{char_pred}, if it is character,\n"
  1764. "\n"
  1765. "@item\n"
  1766. "satisifies the predicate @var{char_pred}, if it is a procedure,\n"
  1767. "\n"
  1768. "@item\n"
  1769. "is in the set if @var{char_pred} is a character set.\n"
  1770. "@end itemize")
  1771. #define FUNC_NAME s_scm_string_index_right
  1772. {
  1773. const char *cstr;
  1774. size_t cstart, cend;
  1775. MY_VALIDATE_SUBSTRING_SPEC_COPY (1, s, cstr,
  1776. 3, start, cstart,
  1777. 4, end, cend);
  1778. if (SCM_CHARP (char_pred))
  1779. {
  1780. char cchr = SCM_CHAR (char_pred);
  1781. while (cstart < cend)
  1782. {
  1783. cend--;
  1784. if (cchr == cstr[cend])
  1785. goto found;
  1786. }
  1787. }
  1788. else if (SCM_CHARSETP (char_pred))
  1789. {
  1790. while (cstart < cend)
  1791. {
  1792. cend--;
  1793. if (SCM_CHARSET_GET (char_pred, cstr[cend]))
  1794. goto found;
  1795. }
  1796. }
  1797. else
  1798. {
  1799. scm_t_trampoline_1 pred_tramp = scm_trampoline_1 (char_pred);
  1800. SCM_ASSERT (pred_tramp, char_pred, SCM_ARG2, FUNC_NAME);
  1801. while (cstart < cend)
  1802. {
  1803. SCM res;
  1804. cend--;
  1805. res = pred_tramp (char_pred, SCM_MAKE_CHAR (cstr[cend]));
  1806. if (scm_is_true (res))
  1807. goto found;
  1808. cstr = scm_i_string_chars (s);
  1809. }
  1810. }
  1811. scm_remember_upto_here_1 (s);
  1812. return SCM_BOOL_F;
  1813. found:
  1814. scm_remember_upto_here_1 (s);
  1815. return scm_from_size_t (cend);
  1816. }
  1817. #undef FUNC_NAME
  1818. SCM_DEFINE (scm_string_rindex, "string-rindex", 2, 2, 0,
  1819. (SCM s, SCM char_pred, SCM start, SCM end),
  1820. "Search through the string @var{s} from right to left, returning\n"
  1821. "the index of the last occurence of a character which\n"
  1822. "\n"
  1823. "@itemize @bullet\n"
  1824. "@item\n"
  1825. "equals @var{char_pred}, if it is character,\n"
  1826. "\n"
  1827. "@item\n"
  1828. "satisifies the predicate @var{char_pred}, if it is a procedure,\n"
  1829. "\n"
  1830. "@item\n"
  1831. "is in the set if @var{char_pred} is a character set.\n"
  1832. "@end itemize")
  1833. #define FUNC_NAME s_scm_string_rindex
  1834. {
  1835. return scm_string_index_right (s, char_pred, start, end);
  1836. }
  1837. #undef FUNC_NAME
  1838. SCM_DEFINE (scm_string_skip, "string-skip", 2, 2, 0,
  1839. (SCM s, SCM char_pred, SCM start, SCM end),
  1840. "Search through the string @var{s} from left to right, returning\n"
  1841. "the index of the first occurence of a character which\n"
  1842. "\n"
  1843. "@itemize @bullet\n"
  1844. "@item\n"
  1845. "does not equal @var{char_pred}, if it is character,\n"
  1846. "\n"
  1847. "@item\n"
  1848. "does not satisify the predicate @var{char_pred}, if it is a\n"
  1849. "procedure,\n"
  1850. "\n"
  1851. "@item\n"
  1852. "is not in the set if @var{char_pred} is a character set.\n"
  1853. "@end itemize")
  1854. #define FUNC_NAME s_scm_string_skip
  1855. {
  1856. const char *cstr;
  1857. size_t cstart, cend;
  1858. MY_VALIDATE_SUBSTRING_SPEC_COPY (1, s, cstr,
  1859. 3, start, cstart,
  1860. 4, end, cend);
  1861. if (SCM_CHARP (char_pred))
  1862. {
  1863. char cchr = SCM_CHAR (char_pred);
  1864. while (cstart < cend)
  1865. {
  1866. if (cchr != cstr[cstart])
  1867. goto found;
  1868. cstart++;
  1869. }
  1870. }
  1871. else if (SCM_CHARSETP (char_pred))
  1872. {
  1873. while (cstart < cend)
  1874. {
  1875. if (!SCM_CHARSET_GET (char_pred, cstr[cstart]))
  1876. goto found;
  1877. cstart++;
  1878. }
  1879. }
  1880. else
  1881. {
  1882. scm_t_trampoline_1 pred_tramp = scm_trampoline_1 (char_pred);
  1883. SCM_ASSERT (pred_tramp, char_pred, SCM_ARG2, FUNC_NAME);
  1884. while (cstart < cend)
  1885. {
  1886. SCM res;
  1887. res = pred_tramp (char_pred, SCM_MAKE_CHAR (cstr[cstart]));
  1888. if (scm_is_false (res))
  1889. goto found;
  1890. cstr = scm_i_string_chars (s);
  1891. cstart++;
  1892. }
  1893. }
  1894. scm_remember_upto_here_1 (s);
  1895. return SCM_BOOL_F;
  1896. found:
  1897. scm_remember_upto_here_1 (s);
  1898. return scm_from_size_t (cstart);
  1899. }
  1900. #undef FUNC_NAME
  1901. SCM_DEFINE (scm_string_skip_right, "string-skip-right", 2, 2, 0,
  1902. (SCM s, SCM char_pred, SCM start, SCM end),
  1903. "Search through the string @var{s} from right to left, returning\n"
  1904. "the index of the last occurence of a character which\n"
  1905. "\n"
  1906. "@itemize @bullet\n"
  1907. "@item\n"
  1908. "does not equal @var{char_pred}, if it is character,\n"
  1909. "\n"
  1910. "@item\n"
  1911. "does not satisfy the predicate @var{char_pred}, if it is a\n"
  1912. "procedure,\n"
  1913. "\n"
  1914. "@item\n"
  1915. "is not in the set if @var{char_pred} is a character set.\n"
  1916. "@end itemize")
  1917. #define FUNC_NAME s_scm_string_skip_right
  1918. {
  1919. const char *cstr;
  1920. size_t cstart, cend;
  1921. MY_VALIDATE_SUBSTRING_SPEC_COPY (1, s, cstr,
  1922. 3, start, cstart,
  1923. 4, end, cend);
  1924. if (SCM_CHARP (char_pred))
  1925. {
  1926. char cchr = SCM_CHAR (char_pred);
  1927. while (cstart < cend)
  1928. {
  1929. cend--;
  1930. if (cchr != cstr[cend])
  1931. goto found;
  1932. }
  1933. }
  1934. else if (SCM_CHARSETP (char_pred))
  1935. {
  1936. while (cstart < cend)
  1937. {
  1938. cend--;
  1939. if (!SCM_CHARSET_GET (char_pred, cstr[cend]))
  1940. goto found;
  1941. }
  1942. }
  1943. else
  1944. {
  1945. scm_t_trampoline_1 pred_tramp = scm_trampoline_1 (char_pred);
  1946. SCM_ASSERT (pred_tramp, char_pred, SCM_ARG2, FUNC_NAME);
  1947. while (cstart < cend)
  1948. {
  1949. SCM res;
  1950. cend--;
  1951. res = pred_tramp (char_pred, SCM_MAKE_CHAR (cstr[cend]));
  1952. if (scm_is_false (res))
  1953. goto found;
  1954. cstr = scm_i_string_chars (s);
  1955. }
  1956. }
  1957. scm_remember_upto_here_1 (s);
  1958. return SCM_BOOL_F;
  1959. found:
  1960. scm_remember_upto_here_1 (s);
  1961. return scm_from_size_t (cend);
  1962. }
  1963. #undef FUNC_NAME
  1964. SCM_DEFINE (scm_string_count, "string-count", 2, 2, 0,
  1965. (SCM s, SCM char_pred, SCM start, SCM end),
  1966. "Return the count of the number of characters in the string\n"
  1967. "@var{s} which\n"
  1968. "\n"
  1969. "@itemize @bullet\n"
  1970. "@item\n"
  1971. "equals @var{char_pred}, if it is character,\n"
  1972. "\n"
  1973. "@item\n"
  1974. "satisifies the predicate @var{char_pred}, if it is a procedure.\n"
  1975. "\n"
  1976. "@item\n"
  1977. "is in the set @var{char_pred}, if it is a character set.\n"
  1978. "@end itemize")
  1979. #define FUNC_NAME s_scm_string_count
  1980. {
  1981. const char *cstr;
  1982. size_t cstart, cend;
  1983. size_t count = 0;
  1984. MY_VALIDATE_SUBSTRING_SPEC_COPY (1, s, cstr,
  1985. 3, start, cstart,
  1986. 4, end, cend);
  1987. if (SCM_CHARP (char_pred))
  1988. {
  1989. char cchr = SCM_CHAR (char_pred);
  1990. while (cstart < cend)
  1991. {
  1992. if (cchr == cstr[cstart])
  1993. count++;
  1994. cstart++;
  1995. }
  1996. }
  1997. else if (SCM_CHARSETP (char_pred))
  1998. {
  1999. while (cstart < cend)
  2000. {
  2001. if (SCM_CHARSET_GET (char_pred, cstr[cstart]))
  2002. count++;
  2003. cstart++;
  2004. }
  2005. }
  2006. else
  2007. {
  2008. scm_t_trampoline_1 pred_tramp = scm_trampoline_1 (char_pred);
  2009. SCM_ASSERT (pred_tramp, char_pred, SCM_ARG2, FUNC_NAME);
  2010. while (cstart < cend)
  2011. {
  2012. SCM res;
  2013. res = pred_tramp (char_pred, SCM_MAKE_CHAR (cstr[cstart]));
  2014. if (scm_is_true (res))
  2015. count++;
  2016. cstr = scm_i_string_chars (s);
  2017. cstart++;
  2018. }
  2019. }
  2020. scm_remember_upto_here_1 (s);
  2021. return scm_from_size_t (count);
  2022. }
  2023. #undef FUNC_NAME
  2024. /* FIXME::martin: This should definitely get implemented more
  2025. efficiently -- maybe with Knuth-Morris-Pratt, like in the reference
  2026. implementation. */
  2027. SCM_DEFINE (scm_string_contains, "string-contains", 2, 4, 0,
  2028. (SCM s1, SCM s2, SCM start1, SCM end1, SCM start2, SCM end2),
  2029. "Does string @var{s1} contain string @var{s2}? Return the index\n"
  2030. "in @var{s1} where @var{s2} occurs as a substring, or false.\n"
  2031. "The optional start/end indices restrict the operation to the\n"
  2032. "indicated substrings.")
  2033. #define FUNC_NAME s_scm_string_contains
  2034. {
  2035. const char *cs1, * cs2;
  2036. size_t cstart1, cend1, cstart2, cend2;
  2037. size_t len2, i, j;
  2038. MY_VALIDATE_SUBSTRING_SPEC_COPY (1, s1, cs1,
  2039. 3, start1, cstart1,
  2040. 4, end1, cend1);
  2041. MY_VALIDATE_SUBSTRING_SPEC_COPY (2, s2, cs2,
  2042. 5, start2, cstart2,
  2043. 6, end2, cend2);
  2044. len2 = cend2 - cstart2;
  2045. if (cend1 - cstart1 >= len2)
  2046. while (cstart1 <= cend1 - len2)
  2047. {
  2048. i = cstart1;
  2049. j = cstart2;
  2050. while (i < cend1 && j < cend2 && cs1[i] == cs2[j])
  2051. {
  2052. i++;
  2053. j++;
  2054. }
  2055. if (j == cend2)
  2056. {
  2057. scm_remember_upto_here_2 (s1, s2);
  2058. return scm_from_size_t (cstart1);
  2059. }
  2060. cstart1++;
  2061. }
  2062. scm_remember_upto_here_2 (s1, s2);
  2063. return SCM_BOOL_F;
  2064. }
  2065. #undef FUNC_NAME
  2066. /* FIXME::martin: This should definitely get implemented more
  2067. efficiently -- maybe with Knuth-Morris-Pratt, like in the reference
  2068. implementation. */
  2069. SCM_DEFINE (scm_string_contains_ci, "string-contains-ci", 2, 4, 0,
  2070. (SCM s1, SCM s2, SCM start1, SCM end1, SCM start2, SCM end2),
  2071. "Does string @var{s1} contain string @var{s2}? Return the index\n"
  2072. "in @var{s1} where @var{s2} occurs as a substring, or false.\n"
  2073. "The optional start/end indices restrict the operation to the\n"
  2074. "indicated substrings. Character comparison is done\n"
  2075. "case-insensitively.")
  2076. #define FUNC_NAME s_scm_string_contains_ci
  2077. {
  2078. const char *cs1, * cs2;
  2079. size_t cstart1, cend1, cstart2, cend2;
  2080. size_t len2, i, j;
  2081. MY_VALIDATE_SUBSTRING_SPEC_COPY (1, s1, cs1,
  2082. 3, start1, cstart1,
  2083. 4, end1, cend1);
  2084. MY_VALIDATE_SUBSTRING_SPEC_COPY (2, s2, cs2,
  2085. 5, start2, cstart2,
  2086. 6, end2, cend2);
  2087. len2 = cend2 - cstart2;
  2088. if (cend1 - cstart1 >= len2)
  2089. while (cstart1 <= cend1 - len2)
  2090. {
  2091. i = cstart1;
  2092. j = cstart2;
  2093. while (i < cend1 && j < cend2 &&
  2094. scm_c_downcase (cs1[i]) == scm_c_downcase (cs2[j]))
  2095. {
  2096. i++;
  2097. j++;
  2098. }
  2099. if (j == cend2)
  2100. {
  2101. scm_remember_upto_here_2 (s1, s2);
  2102. return scm_from_size_t (cstart1);
  2103. }
  2104. cstart1++;
  2105. }
  2106. scm_remember_upto_here_2 (s1, s2);
  2107. return SCM_BOOL_F;
  2108. }
  2109. #undef FUNC_NAME
  2110. /* Helper function for the string uppercase conversion functions.
  2111. * No argument checking is performed. */
  2112. static SCM
  2113. string_upcase_x (SCM v, size_t start, size_t end)
  2114. {
  2115. size_t k;
  2116. char *dst;
  2117. dst = scm_i_string_writable_chars (v);
  2118. for (k = start; k < end; ++k)
  2119. dst[k] = scm_c_upcase (dst[k]);
  2120. scm_i_string_stop_writing ();
  2121. scm_remember_upto_here_1 (v);
  2122. return v;
  2123. }
  2124. SCM_DEFINE (scm_substring_upcase_x, "string-upcase!", 1, 2, 0,
  2125. (SCM str, SCM start, SCM end),
  2126. "Destructively upcase every character in @code{str}.\n"
  2127. "\n"
  2128. "@lisp\n"
  2129. "(string-upcase! y)\n"
  2130. "@result{} \"ARRDEFG\"\n"
  2131. "y\n"
  2132. "@result{} \"ARRDEFG\"\n"
  2133. "@end lisp")
  2134. #define FUNC_NAME s_scm_substring_upcase_x
  2135. {
  2136. const char *cstr;
  2137. size_t cstart, cend;
  2138. MY_VALIDATE_SUBSTRING_SPEC_COPY (1, str, cstr,
  2139. 2, start, cstart,
  2140. 3, end, cend);
  2141. return string_upcase_x (str, cstart, cend);
  2142. }
  2143. #undef FUNC_NAME
  2144. SCM
  2145. scm_string_upcase_x (SCM str)
  2146. {
  2147. return scm_substring_upcase_x (str, SCM_UNDEFINED, SCM_UNDEFINED);
  2148. }
  2149. SCM_DEFINE (scm_substring_upcase, "string-upcase", 1, 2, 0,
  2150. (SCM str, SCM start, SCM end),
  2151. "Upcase every character in @code{str}.")
  2152. #define FUNC_NAME s_scm_substring_upcase
  2153. {
  2154. const char *cstr;
  2155. size_t cstart, cend;
  2156. MY_VALIDATE_SUBSTRING_SPEC_COPY (1, str, cstr,
  2157. 2, start, cstart,
  2158. 3, end, cend);
  2159. return string_upcase_x (scm_string_copy (str), cstart, cend);
  2160. }
  2161. #undef FUNC_NAME
  2162. SCM
  2163. scm_string_upcase (SCM str)
  2164. {
  2165. return scm_substring_upcase (str, SCM_UNDEFINED, SCM_UNDEFINED);
  2166. }
  2167. /* Helper function for the string lowercase conversion functions.
  2168. * No argument checking is performed. */
  2169. static SCM
  2170. string_downcase_x (SCM v, size_t start, size_t end)
  2171. {
  2172. size_t k;
  2173. char *dst;
  2174. dst = scm_i_string_writable_chars (v);
  2175. for (k = start; k < end; ++k)
  2176. dst[k] = scm_c_downcase (dst[k]);
  2177. scm_i_string_stop_writing ();
  2178. scm_remember_upto_here_1 (v);
  2179. return v;
  2180. }
  2181. SCM_DEFINE (scm_substring_downcase_x, "string-downcase!", 1, 2, 0,
  2182. (SCM str, SCM start, SCM end),
  2183. "Destructively downcase every character in @var{str}.\n"
  2184. "\n"
  2185. "@lisp\n"
  2186. "y\n"
  2187. "@result{} \"ARRDEFG\"\n"
  2188. "(string-downcase! y)\n"
  2189. "@result{} \"arrdefg\"\n"
  2190. "y\n"
  2191. "@result{} \"arrdefg\"\n"
  2192. "@end lisp")
  2193. #define FUNC_NAME s_scm_substring_downcase_x
  2194. {
  2195. const char *cstr;
  2196. size_t cstart, cend;
  2197. MY_VALIDATE_SUBSTRING_SPEC_COPY (1, str, cstr,
  2198. 2, start, cstart,
  2199. 3, end, cend);
  2200. return string_downcase_x (str, cstart, cend);
  2201. }
  2202. #undef FUNC_NAME
  2203. SCM
  2204. scm_string_downcase_x (SCM str)
  2205. {
  2206. return scm_substring_downcase_x (str, SCM_UNDEFINED, SCM_UNDEFINED);
  2207. }
  2208. SCM_DEFINE (scm_substring_downcase, "string-downcase", 1, 2, 0,
  2209. (SCM str, SCM start, SCM end),
  2210. "Downcase every character in @var{str}.")
  2211. #define FUNC_NAME s_scm_substring_downcase
  2212. {
  2213. const char *cstr;
  2214. size_t cstart, cend;
  2215. MY_VALIDATE_SUBSTRING_SPEC_COPY (1, str, cstr,
  2216. 2, start, cstart,
  2217. 3, end, cend);
  2218. return string_downcase_x (scm_string_copy (str), cstart, cend);
  2219. }
  2220. #undef FUNC_NAME
  2221. SCM
  2222. scm_string_downcase (SCM str)
  2223. {
  2224. return scm_substring_downcase (str, SCM_UNDEFINED, SCM_UNDEFINED);
  2225. }
  2226. /* Helper function for the string capitalization functions.
  2227. * No argument checking is performed. */
  2228. static SCM
  2229. string_titlecase_x (SCM str, size_t start, size_t end)
  2230. {
  2231. unsigned char *sz;
  2232. size_t i;
  2233. int in_word = 0;
  2234. sz = (unsigned char *) scm_i_string_writable_chars (str);
  2235. for(i = start; i < end; i++)
  2236. {
  2237. if (scm_is_true (scm_char_alphabetic_p (SCM_MAKE_CHAR (sz[i]))))
  2238. {
  2239. if (!in_word)
  2240. {
  2241. sz[i] = scm_c_upcase(sz[i]);
  2242. in_word = 1;
  2243. }
  2244. else
  2245. {
  2246. sz[i] = scm_c_downcase(sz[i]);
  2247. }
  2248. }
  2249. else
  2250. in_word = 0;
  2251. }
  2252. scm_i_string_stop_writing ();
  2253. scm_remember_upto_here_1 (str);
  2254. return str;
  2255. }
  2256. SCM_DEFINE (scm_string_titlecase_x, "string-titlecase!", 1, 2, 0,
  2257. (SCM str, SCM start, SCM end),
  2258. "Destructively titlecase every first character in a word in\n"
  2259. "@var{str}.")
  2260. #define FUNC_NAME s_scm_string_titlecase_x
  2261. {
  2262. const char *cstr;
  2263. size_t cstart, cend;
  2264. MY_VALIDATE_SUBSTRING_SPEC_COPY (1, str, cstr,
  2265. 2, start, cstart,
  2266. 3, end, cend);
  2267. return string_titlecase_x (str, cstart, cend);
  2268. }
  2269. #undef FUNC_NAME
  2270. SCM_DEFINE (scm_string_titlecase, "string-titlecase", 1, 2, 0,
  2271. (SCM str, SCM start, SCM end),
  2272. "Titlecase every first character in a word in @var{str}.")
  2273. #define FUNC_NAME s_scm_string_titlecase
  2274. {
  2275. const char *cstr;
  2276. size_t cstart, cend;
  2277. MY_VALIDATE_SUBSTRING_SPEC_COPY (1, str, cstr,
  2278. 2, start, cstart,
  2279. 3, end, cend);
  2280. return string_titlecase_x (scm_string_copy (str), cstart, cend);
  2281. }
  2282. #undef FUNC_NAME
  2283. SCM_DEFINE (scm_string_capitalize_x, "string-capitalize!", 1, 0, 0,
  2284. (SCM str),
  2285. "Upcase the first character of every word in @var{str}\n"
  2286. "destructively and return @var{str}.\n"
  2287. "\n"
  2288. "@lisp\n"
  2289. "y @result{} \"hello world\"\n"
  2290. "(string-capitalize! y) @result{} \"Hello World\"\n"
  2291. "y @result{} \"Hello World\"\n"
  2292. "@end lisp")
  2293. #define FUNC_NAME s_scm_string_capitalize_x
  2294. {
  2295. return scm_string_titlecase_x (str, SCM_UNDEFINED, SCM_UNDEFINED);
  2296. }
  2297. #undef FUNC_NAME
  2298. SCM_DEFINE (scm_string_capitalize, "string-capitalize", 1, 0, 0,
  2299. (SCM str),
  2300. "Return a freshly allocated string with the characters in\n"
  2301. "@var{str}, where the first character of every word is\n"
  2302. "capitalized.")
  2303. #define FUNC_NAME s_scm_string_capitalize
  2304. {
  2305. return scm_string_capitalize_x (scm_string_copy (str));
  2306. }
  2307. #undef FUNC_NAME
  2308. /* Reverse the portion of @var{str} between str[cstart] (including)
  2309. and str[cend] excluding. */
  2310. static void
  2311. string_reverse_x (char * str, size_t cstart, size_t cend)
  2312. {
  2313. char tmp;
  2314. if (cend > 0)
  2315. {
  2316. cend--;
  2317. while (cstart < cend)
  2318. {
  2319. tmp = str[cstart];
  2320. str[cstart] = str[cend];
  2321. str[cend] = tmp;
  2322. cstart++;
  2323. cend--;
  2324. }
  2325. }
  2326. }
  2327. SCM_DEFINE (scm_string_reverse, "string-reverse", 1, 2, 0,
  2328. (SCM str, SCM start, SCM end),
  2329. "Reverse the string @var{str}. The optional arguments\n"
  2330. "@var{start} and @var{end} delimit the region of @var{str} to\n"
  2331. "operate on.")
  2332. #define FUNC_NAME s_scm_string_reverse
  2333. {
  2334. const char *cstr;
  2335. char *ctarget;
  2336. size_t cstart, cend;
  2337. SCM result;
  2338. MY_VALIDATE_SUBSTRING_SPEC_COPY (1, str, cstr,
  2339. 2, start, cstart,
  2340. 3, end, cend);
  2341. result = scm_string_copy (str);
  2342. ctarget = scm_i_string_writable_chars (result);
  2343. string_reverse_x (ctarget, cstart, cend);
  2344. scm_i_string_stop_writing ();
  2345. scm_remember_upto_here_1 (str);
  2346. return result;
  2347. }
  2348. #undef FUNC_NAME
  2349. SCM_DEFINE (scm_string_reverse_x, "string-reverse!", 1, 2, 0,
  2350. (SCM str, SCM start, SCM end),
  2351. "Reverse the string @var{str} in-place. The optional arguments\n"
  2352. "@var{start} and @var{end} delimit the region of @var{str} to\n"
  2353. "operate on. The return value is unspecified.")
  2354. #define FUNC_NAME s_scm_string_reverse_x
  2355. {
  2356. char *cstr;
  2357. size_t cstart, cend;
  2358. MY_VALIDATE_SUBSTRING_SPEC (1, str,
  2359. 2, start, cstart,
  2360. 3, end, cend);
  2361. cstr = scm_i_string_writable_chars (str);
  2362. string_reverse_x (cstr, cstart, cend);
  2363. scm_i_string_stop_writing ();
  2364. scm_remember_upto_here_1 (str);
  2365. return SCM_UNSPECIFIED;
  2366. }
  2367. #undef FUNC_NAME
  2368. SCM_DEFINE (scm_string_append_shared, "string-append/shared", 0, 0, 1,
  2369. (SCM rest),
  2370. "Like @code{string-append}, but the result may share memory\n"
  2371. "with the argument strings.")
  2372. #define FUNC_NAME s_scm_string_append_shared
  2373. {
  2374. /* If "rest" contains just one non-empty string, return that.
  2375. If it's entirely empty strings, then return scm_nullstr.
  2376. Otherwise use scm_string_concatenate. */
  2377. SCM ret = scm_nullstr;
  2378. int seen_nonempty = 0;
  2379. SCM l, s;
  2380. SCM_VALIDATE_REST_ARGUMENT (rest);
  2381. for (l = rest; scm_is_pair (l); l = SCM_CDR (l))
  2382. {
  2383. s = SCM_CAR (l);
  2384. if (scm_c_string_length (s) != 0)
  2385. {
  2386. if (seen_nonempty)
  2387. /* two or more non-empty strings, need full concat */
  2388. return scm_string_append (rest);
  2389. seen_nonempty = 1;
  2390. ret = s;
  2391. }
  2392. }
  2393. return ret;
  2394. }
  2395. #undef FUNC_NAME
  2396. SCM_DEFINE (scm_string_concatenate, "string-concatenate", 1, 0, 0,
  2397. (SCM ls),
  2398. "Append the elements of @var{ls} (which must be strings)\n"
  2399. "together into a single string. Guaranteed to return a freshly\n"
  2400. "allocated string.")
  2401. #define FUNC_NAME s_scm_string_concatenate
  2402. {
  2403. SCM_VALIDATE_LIST (SCM_ARG1, ls);
  2404. return scm_string_append (ls);
  2405. }
  2406. #undef FUNC_NAME
  2407. SCM_DEFINE (scm_string_concatenate_reverse, "string-concatenate-reverse", 1, 2, 0,
  2408. (SCM ls, SCM final_string, SCM end),
  2409. "Without optional arguments, this procedure is equivalent to\n"
  2410. "\n"
  2411. "@smalllisp\n"
  2412. "(string-concatenate (reverse ls))\n"
  2413. "@end smalllisp\n"
  2414. "\n"
  2415. "If the optional argument @var{final_string} is specified, it is\n"
  2416. "consed onto the beginning to @var{ls} before performing the\n"
  2417. "list-reverse and string-concatenate operations. If @var{end}\n"
  2418. "is given, only the characters of @var{final_string} up to index\n"
  2419. "@var{end} are used.\n"
  2420. "\n"
  2421. "Guaranteed to return a freshly allocated string.")
  2422. #define FUNC_NAME s_scm_string_concatenate_reverse
  2423. {
  2424. if (!SCM_UNBNDP (end))
  2425. final_string = scm_substring (final_string, SCM_INUM0, end);
  2426. if (!SCM_UNBNDP (final_string))
  2427. ls = scm_cons (final_string, ls);
  2428. return scm_string_concatenate (scm_reverse (ls));
  2429. }
  2430. #undef FUNC_NAME
  2431. SCM_DEFINE (scm_string_concatenate_shared, "string-concatenate/shared", 1, 0, 0,
  2432. (SCM ls),
  2433. "Like @code{string-concatenate}, but the result may share memory\n"
  2434. "with the strings in the list @var{ls}.")
  2435. #define FUNC_NAME s_scm_string_concatenate_shared
  2436. {
  2437. SCM_VALIDATE_LIST (SCM_ARG1, ls);
  2438. return scm_string_append_shared (ls);
  2439. }
  2440. #undef FUNC_NAME
  2441. SCM_DEFINE (scm_string_concatenate_reverse_shared, "string-concatenate-reverse/shared", 1, 2, 0,
  2442. (SCM ls, SCM final_string, SCM end),
  2443. "Like @code{string-concatenate-reverse}, but the result may\n"
  2444. "share memory with the the strings in the @var{ls} arguments.")
  2445. #define FUNC_NAME s_scm_string_concatenate_reverse_shared
  2446. {
  2447. /* Just call the non-sharing version. */
  2448. return scm_string_concatenate_reverse (ls, final_string, end);
  2449. }
  2450. #undef FUNC_NAME
  2451. SCM_DEFINE (scm_string_map, "string-map", 2, 2, 0,
  2452. (SCM proc, SCM s, SCM start, SCM end),
  2453. "@var{proc} is a char->char procedure, it is mapped over\n"
  2454. "@var{s}. The order in which the procedure is applied to the\n"
  2455. "string elements is not specified.")
  2456. #define FUNC_NAME s_scm_string_map
  2457. {
  2458. char *p;
  2459. size_t cstart, cend;
  2460. SCM result;
  2461. scm_t_trampoline_1 proc_tramp = scm_trampoline_1 (proc);
  2462. SCM_ASSERT (proc_tramp, proc, SCM_ARG1, FUNC_NAME);
  2463. MY_VALIDATE_SUBSTRING_SPEC (2, s,
  2464. 3, start, cstart,
  2465. 4, end, cend);
  2466. result = scm_i_make_string (cend - cstart, &p);
  2467. while (cstart < cend)
  2468. {
  2469. SCM ch = proc_tramp (proc, scm_c_string_ref (s, cstart));
  2470. if (!SCM_CHARP (ch))
  2471. SCM_MISC_ERROR ("procedure ~S returned non-char", scm_list_1 (proc));
  2472. cstart++;
  2473. *p++ = SCM_CHAR (ch);
  2474. }
  2475. return result;
  2476. }
  2477. #undef FUNC_NAME
  2478. SCM_DEFINE (scm_string_map_x, "string-map!", 2, 2, 0,
  2479. (SCM proc, SCM s, SCM start, SCM end),
  2480. "@var{proc} is a char->char procedure, it is mapped over\n"
  2481. "@var{s}. The order in which the procedure is applied to the\n"
  2482. "string elements is not specified. The string @var{s} is\n"
  2483. "modified in-place, the return value is not specified.")
  2484. #define FUNC_NAME s_scm_string_map_x
  2485. {
  2486. size_t cstart, cend;
  2487. scm_t_trampoline_1 proc_tramp = scm_trampoline_1 (proc);
  2488. SCM_ASSERT (proc_tramp, proc, SCM_ARG1, FUNC_NAME);
  2489. MY_VALIDATE_SUBSTRING_SPEC (2, s,
  2490. 3, start, cstart,
  2491. 4, end, cend);
  2492. while (cstart < cend)
  2493. {
  2494. SCM ch = proc_tramp (proc, scm_c_string_ref (s, cstart));
  2495. if (!SCM_CHARP (ch))
  2496. SCM_MISC_ERROR ("procedure ~S returned non-char", scm_list_1 (proc));
  2497. scm_c_string_set_x (s, cstart, ch);
  2498. cstart++;
  2499. }
  2500. return SCM_UNSPECIFIED;
  2501. }
  2502. #undef FUNC_NAME
  2503. SCM_DEFINE (scm_string_fold, "string-fold", 3, 2, 0,
  2504. (SCM kons, SCM knil, SCM s, SCM start, SCM end),
  2505. "Fold @var{kons} over the characters of @var{s}, with @var{knil}\n"
  2506. "as the terminating element, from left to right. @var{kons}\n"
  2507. "must expect two arguments: The actual character and the last\n"
  2508. "result of @var{kons}' application.")
  2509. #define FUNC_NAME s_scm_string_fold
  2510. {
  2511. const char *cstr;
  2512. size_t cstart, cend;
  2513. SCM result;
  2514. SCM_VALIDATE_PROC (1, kons);
  2515. MY_VALIDATE_SUBSTRING_SPEC_COPY (3, s, cstr,
  2516. 4, start, cstart,
  2517. 5, end, cend);
  2518. result = knil;
  2519. while (cstart < cend)
  2520. {
  2521. unsigned int c = (unsigned char) cstr[cstart];
  2522. result = scm_call_2 (kons, SCM_MAKE_CHAR (c), result);
  2523. cstr = scm_i_string_chars (s);
  2524. cstart++;
  2525. }
  2526. scm_remember_upto_here_1 (s);
  2527. return result;
  2528. }
  2529. #undef FUNC_NAME
  2530. SCM_DEFINE (scm_string_fold_right, "string-fold-right", 3, 2, 0,
  2531. (SCM kons, SCM knil, SCM s, SCM start, SCM end),
  2532. "Fold @var{kons} over the characters of @var{s}, with @var{knil}\n"
  2533. "as the terminating element, from right to left. @var{kons}\n"
  2534. "must expect two arguments: The actual character and the last\n"
  2535. "result of @var{kons}' application.")
  2536. #define FUNC_NAME s_scm_string_fold_right
  2537. {
  2538. const char *cstr;
  2539. size_t cstart, cend;
  2540. SCM result;
  2541. SCM_VALIDATE_PROC (1, kons);
  2542. MY_VALIDATE_SUBSTRING_SPEC_COPY (3, s, cstr,
  2543. 4, start, cstart,
  2544. 5, end, cend);
  2545. result = knil;
  2546. while (cstart < cend)
  2547. {
  2548. unsigned int c = (unsigned char) cstr[cend - 1];
  2549. result = scm_call_2 (kons, SCM_MAKE_CHAR (c), result);
  2550. cstr = scm_i_string_chars (s);
  2551. cend--;
  2552. }
  2553. scm_remember_upto_here_1 (s);
  2554. return result;
  2555. }
  2556. #undef FUNC_NAME
  2557. SCM_DEFINE (scm_string_unfold, "string-unfold", 4, 2, 0,
  2558. (SCM p, SCM f, SCM g, SCM seed, SCM base, SCM make_final),
  2559. "@itemize @bullet\n"
  2560. "@item @var{g} is used to generate a series of @emph{seed}\n"
  2561. "values from the initial @var{seed}: @var{seed}, (@var{g}\n"
  2562. "@var{seed}), (@var{g}^2 @var{seed}), (@var{g}^3 @var{seed}),\n"
  2563. "@dots{}\n"
  2564. "@item @var{p} tells us when to stop -- when it returns true\n"
  2565. "when applied to one of these seed values.\n"
  2566. "@item @var{f} maps each seed value to the corresponding\n"
  2567. "character in the result string. These chars are assembled\n"
  2568. "into the string in a left-to-right order.\n"
  2569. "@item @var{base} is the optional initial/leftmost portion\n"
  2570. "of the constructed string; it default to the empty\n"
  2571. "string.\n"
  2572. "@item @var{make_final} is applied to the terminal seed\n"
  2573. "value (on which @var{p} returns true) to produce\n"
  2574. "the final/rightmost portion of the constructed string.\n"
  2575. "It defaults to @code{(lambda (x) "")}.\n"
  2576. "@end itemize")
  2577. #define FUNC_NAME s_scm_string_unfold
  2578. {
  2579. SCM res, ans;
  2580. SCM_VALIDATE_PROC (1, p);
  2581. SCM_VALIDATE_PROC (2, f);
  2582. SCM_VALIDATE_PROC (3, g);
  2583. if (!SCM_UNBNDP (base))
  2584. {
  2585. SCM_VALIDATE_STRING (5, base);
  2586. ans = base;
  2587. }
  2588. else
  2589. ans = scm_i_make_string (0, NULL);
  2590. if (!SCM_UNBNDP (make_final))
  2591. SCM_VALIDATE_PROC (6, make_final);
  2592. res = scm_call_1 (p, seed);
  2593. while (scm_is_false (res))
  2594. {
  2595. SCM str;
  2596. char *ptr;
  2597. SCM ch = scm_call_1 (f, seed);
  2598. if (!SCM_CHARP (ch))
  2599. SCM_MISC_ERROR ("procedure ~S returned non-char", scm_list_1 (f));
  2600. str = scm_i_make_string (1, &ptr);
  2601. *ptr = SCM_CHAR (ch);
  2602. ans = scm_string_append (scm_list_2 (ans, str));
  2603. seed = scm_call_1 (g, seed);
  2604. res = scm_call_1 (p, seed);
  2605. }
  2606. if (!SCM_UNBNDP (make_final))
  2607. {
  2608. res = scm_call_1 (make_final, seed);
  2609. return scm_string_append (scm_list_2 (ans, res));
  2610. }
  2611. else
  2612. return ans;
  2613. }
  2614. #undef FUNC_NAME
  2615. SCM_DEFINE (scm_string_unfold_right, "string-unfold-right", 4, 2, 0,
  2616. (SCM p, SCM f, SCM g, SCM seed, SCM base, SCM make_final),
  2617. "@itemize @bullet\n"
  2618. "@item @var{g} is used to generate a series of @emph{seed}\n"
  2619. "values from the initial @var{seed}: @var{seed}, (@var{g}\n"
  2620. "@var{seed}), (@var{g}^2 @var{seed}), (@var{g}^3 @var{seed}),\n"
  2621. "@dots{}\n"
  2622. "@item @var{p} tells us when to stop -- when it returns true\n"
  2623. "when applied to one of these seed values.\n"
  2624. "@item @var{f} maps each seed value to the corresponding\n"
  2625. "character in the result string. These chars are assembled\n"
  2626. "into the string in a right-to-left order.\n"
  2627. "@item @var{base} is the optional initial/rightmost portion\n"
  2628. "of the constructed string; it default to the empty\n"
  2629. "string.\n"
  2630. "@item @var{make_final} is applied to the terminal seed\n"
  2631. "value (on which @var{p} returns true) to produce\n"
  2632. "the final/leftmost portion of the constructed string.\n"
  2633. "It defaults to @code{(lambda (x) "")}.\n"
  2634. "@end itemize")
  2635. #define FUNC_NAME s_scm_string_unfold_right
  2636. {
  2637. SCM res, ans;
  2638. SCM_VALIDATE_PROC (1, p);
  2639. SCM_VALIDATE_PROC (2, f);
  2640. SCM_VALIDATE_PROC (3, g);
  2641. if (!SCM_UNBNDP (base))
  2642. {
  2643. SCM_VALIDATE_STRING (5, base);
  2644. ans = base;
  2645. }
  2646. else
  2647. ans = scm_i_make_string (0, NULL);
  2648. if (!SCM_UNBNDP (make_final))
  2649. SCM_VALIDATE_PROC (6, make_final);
  2650. res = scm_call_1 (p, seed);
  2651. while (scm_is_false (res))
  2652. {
  2653. SCM str;
  2654. char *ptr;
  2655. SCM ch = scm_call_1 (f, seed);
  2656. if (!SCM_CHARP (ch))
  2657. SCM_MISC_ERROR ("procedure ~S returned non-char", scm_list_1 (f));
  2658. str = scm_i_make_string (1, &ptr);
  2659. *ptr = SCM_CHAR (ch);
  2660. ans = scm_string_append (scm_list_2 (str, ans));
  2661. seed = scm_call_1 (g, seed);
  2662. res = scm_call_1 (p, seed);
  2663. }
  2664. if (!SCM_UNBNDP (make_final))
  2665. {
  2666. res = scm_call_1 (make_final, seed);
  2667. return scm_string_append (scm_list_2 (res, ans));
  2668. }
  2669. else
  2670. return ans;
  2671. }
  2672. #undef FUNC_NAME
  2673. SCM_DEFINE (scm_string_for_each, "string-for-each", 2, 2, 0,
  2674. (SCM proc, SCM s, SCM start, SCM end),
  2675. "@var{proc} is mapped over @var{s} in left-to-right order. The\n"
  2676. "return value is not specified.")
  2677. #define FUNC_NAME s_scm_string_for_each
  2678. {
  2679. const char *cstr;
  2680. size_t cstart, cend;
  2681. scm_t_trampoline_1 proc_tramp = scm_trampoline_1 (proc);
  2682. SCM_ASSERT (proc_tramp, proc, SCM_ARG1, FUNC_NAME);
  2683. MY_VALIDATE_SUBSTRING_SPEC_COPY (2, s, cstr,
  2684. 3, start, cstart,
  2685. 4, end, cend);
  2686. while (cstart < cend)
  2687. {
  2688. unsigned int c = (unsigned char) cstr[cstart];
  2689. proc_tramp (proc, SCM_MAKE_CHAR (c));
  2690. cstr = scm_i_string_chars (s);
  2691. cstart++;
  2692. }
  2693. scm_remember_upto_here_1 (s);
  2694. return SCM_UNSPECIFIED;
  2695. }
  2696. #undef FUNC_NAME
  2697. SCM_DEFINE (scm_string_for_each_index, "string-for-each-index", 2, 2, 0,
  2698. (SCM proc, SCM s, SCM start, SCM end),
  2699. "Call @code{(@var{proc} i)} for each index i in @var{s}, from\n"
  2700. "left to right.\n"
  2701. "\n"
  2702. "For example, to change characters to alternately upper and\n"
  2703. "lower case,\n"
  2704. "\n"
  2705. "@example\n"
  2706. "(define str (string-copy \"studly\"))\n"
  2707. "(string-for-each-index\n"
  2708. " (lambda (i)\n"
  2709. " (string-set! str i\n"
  2710. " ((if (even? i) char-upcase char-downcase)\n"
  2711. " (string-ref str i))))\n"
  2712. " str)\n"
  2713. "str @result{} \"StUdLy\"\n"
  2714. "@end example")
  2715. #define FUNC_NAME s_scm_string_for_each_index
  2716. {
  2717. size_t cstart, cend;
  2718. scm_t_trampoline_1 proc_tramp = scm_trampoline_1 (proc);
  2719. SCM_ASSERT (proc_tramp, proc, SCM_ARG1, FUNC_NAME);
  2720. MY_VALIDATE_SUBSTRING_SPEC (2, s,
  2721. 3, start, cstart,
  2722. 4, end, cend);
  2723. while (cstart < cend)
  2724. {
  2725. proc_tramp (proc, scm_from_size_t (cstart));
  2726. cstart++;
  2727. }
  2728. scm_remember_upto_here_1 (s);
  2729. return SCM_UNSPECIFIED;
  2730. }
  2731. #undef FUNC_NAME
  2732. SCM_DEFINE (scm_xsubstring, "xsubstring", 2, 3, 0,
  2733. (SCM s, SCM from, SCM to, SCM start, SCM end),
  2734. "This is the @emph{extended substring} procedure that implements\n"
  2735. "replicated copying of a substring of some string.\n"
  2736. "\n"
  2737. "@var{s} is a string, @var{start} and @var{end} are optional\n"
  2738. "arguments that demarcate a substring of @var{s}, defaulting to\n"
  2739. "0 and the length of @var{s}. Replicate this substring up and\n"
  2740. "down index space, in both the positive and negative directions.\n"
  2741. "@code{xsubstring} returns the substring of this string\n"
  2742. "beginning at index @var{from}, and ending at @var{to}, which\n"
  2743. "defaults to @var{from} + (@var{end} - @var{start}).")
  2744. #define FUNC_NAME s_scm_xsubstring
  2745. {
  2746. const char *cs;
  2747. char *p;
  2748. size_t cstart, cend;
  2749. int cfrom, cto;
  2750. SCM result;
  2751. MY_VALIDATE_SUBSTRING_SPEC (1, s,
  2752. 4, start, cstart,
  2753. 5, end, cend);
  2754. cfrom = scm_to_int (from);
  2755. if (SCM_UNBNDP (to))
  2756. cto = cfrom + (cend - cstart);
  2757. else
  2758. cto = scm_to_int (to);
  2759. if (cstart == cend && cfrom != cto)
  2760. SCM_MISC_ERROR ("start and end indices must not be equal", SCM_EOL);
  2761. result = scm_i_make_string (cto - cfrom, &p);
  2762. cs = scm_i_string_chars (s);
  2763. while (cfrom < cto)
  2764. {
  2765. size_t t = ((cfrom < 0) ? -cfrom : cfrom) % (cend - cstart);
  2766. if (cfrom < 0)
  2767. *p = cs[(cend - cstart) - t];
  2768. else
  2769. *p = cs[t];
  2770. cfrom++;
  2771. p++;
  2772. }
  2773. scm_remember_upto_here_1 (s);
  2774. return result;
  2775. }
  2776. #undef FUNC_NAME
  2777. SCM_DEFINE (scm_string_xcopy_x, "string-xcopy!", 4, 3, 0,
  2778. (SCM target, SCM tstart, SCM s, SCM sfrom, SCM sto, SCM start, SCM end),
  2779. "Exactly the same as @code{xsubstring}, but the extracted text\n"
  2780. "is written into the string @var{target} starting at index\n"
  2781. "@var{tstart}. The operation is not defined if @code{(eq?\n"
  2782. "@var{target} @var{s})} or these arguments share storage -- you\n"
  2783. "cannot copy a string on top of itself.")
  2784. #define FUNC_NAME s_scm_string_xcopy_x
  2785. {
  2786. char *p;
  2787. const char *cs;
  2788. size_t ctstart, cstart, cend;
  2789. int csfrom, csto;
  2790. SCM dummy = SCM_UNDEFINED;
  2791. size_t cdummy;
  2792. MY_VALIDATE_SUBSTRING_SPEC (1, target,
  2793. 2, tstart, ctstart,
  2794. 2, dummy, cdummy);
  2795. MY_VALIDATE_SUBSTRING_SPEC (3, s,
  2796. 6, start, cstart,
  2797. 7, end, cend);
  2798. csfrom = scm_to_int (sfrom);
  2799. if (SCM_UNBNDP (sto))
  2800. csto = csfrom + (cend - cstart);
  2801. else
  2802. csto = scm_to_int (sto);
  2803. if (cstart == cend && csfrom != csto)
  2804. SCM_MISC_ERROR ("start and end indices must not be equal", SCM_EOL);
  2805. SCM_ASSERT_RANGE (1, tstart,
  2806. ctstart + (csto - csfrom) <= scm_i_string_length (target));
  2807. p = scm_i_string_writable_chars (target) + ctstart;
  2808. cs = scm_i_string_chars (s);
  2809. while (csfrom < csto)
  2810. {
  2811. size_t t = ((csfrom < 0) ? -csfrom : csfrom) % (cend - cstart);
  2812. if (csfrom < 0)
  2813. *p = cs[(cend - cstart) - t];
  2814. else
  2815. *p = cs[t];
  2816. csfrom++;
  2817. p++;
  2818. }
  2819. scm_i_string_stop_writing ();
  2820. scm_remember_upto_here_2 (target, s);
  2821. return SCM_UNSPECIFIED;
  2822. }
  2823. #undef FUNC_NAME
  2824. SCM_DEFINE (scm_string_replace, "string-replace", 2, 4, 0,
  2825. (SCM s1, SCM s2, SCM start1, SCM end1, SCM start2, SCM end2),
  2826. "Return the string @var{s1}, but with the characters\n"
  2827. "@var{start1} @dots{} @var{end1} replaced by the characters\n"
  2828. "@var{start2} @dots{} @var{end2} from @var{s2}.")
  2829. #define FUNC_NAME s_scm_string_replace
  2830. {
  2831. const char *cstr1, *cstr2;
  2832. char *p;
  2833. size_t cstart1, cend1, cstart2, cend2;
  2834. SCM result;
  2835. MY_VALIDATE_SUBSTRING_SPEC (1, s1,
  2836. 3, start1, cstart1,
  2837. 4, end1, cend1);
  2838. MY_VALIDATE_SUBSTRING_SPEC (2, s2,
  2839. 5, start2, cstart2,
  2840. 6, end2, cend2);
  2841. result = scm_i_make_string (cstart1 + (cend2 - cstart2) +
  2842. scm_i_string_length (s1) - cend1, &p);
  2843. cstr1 = scm_i_string_chars (s1);
  2844. cstr2 = scm_i_string_chars (s2);
  2845. memmove (p, cstr1, cstart1 * sizeof (char));
  2846. memmove (p + cstart1, cstr2 + cstart2, (cend2 - cstart2) * sizeof (char));
  2847. memmove (p + cstart1 + (cend2 - cstart2),
  2848. cstr1 + cend1,
  2849. (scm_i_string_length (s1) - cend1) * sizeof (char));
  2850. scm_remember_upto_here_2 (s1, s2);
  2851. return result;
  2852. }
  2853. #undef FUNC_NAME
  2854. SCM_DEFINE (scm_string_tokenize, "string-tokenize", 1, 3, 0,
  2855. (SCM s, SCM token_set, SCM start, SCM end),
  2856. "Split the string @var{s} into a list of substrings, where each\n"
  2857. "substring is a maximal non-empty contiguous sequence of\n"
  2858. "characters from the character set @var{token_set}, which\n"
  2859. "defaults to @code{char-set:graphic}.\n"
  2860. "If @var{start} or @var{end} indices are provided, they restrict\n"
  2861. "@code{string-tokenize} to operating on the indicated substring\n"
  2862. "of @var{s}.")
  2863. #define FUNC_NAME s_scm_string_tokenize
  2864. {
  2865. const char *cstr;
  2866. size_t cstart, cend;
  2867. SCM result = SCM_EOL;
  2868. MY_VALIDATE_SUBSTRING_SPEC_COPY (1, s, cstr,
  2869. 3, start, cstart,
  2870. 4, end, cend);
  2871. if (SCM_UNBNDP (token_set))
  2872. token_set = scm_char_set_graphic;
  2873. if (SCM_CHARSETP (token_set))
  2874. {
  2875. size_t idx;
  2876. while (cstart < cend)
  2877. {
  2878. while (cstart < cend)
  2879. {
  2880. if (SCM_CHARSET_GET (token_set, cstr[cend - 1]))
  2881. break;
  2882. cend--;
  2883. }
  2884. if (cstart >= cend)
  2885. break;
  2886. idx = cend;
  2887. while (cstart < cend)
  2888. {
  2889. if (!SCM_CHARSET_GET (token_set, cstr[cend - 1]))
  2890. break;
  2891. cend--;
  2892. }
  2893. result = scm_cons (scm_c_substring (s, cend, idx), result);
  2894. cstr = scm_i_string_chars (s);
  2895. }
  2896. }
  2897. else
  2898. SCM_WRONG_TYPE_ARG (2, token_set);
  2899. scm_remember_upto_here_1 (s);
  2900. return result;
  2901. }
  2902. #undef FUNC_NAME
  2903. SCM_DEFINE (scm_string_split, "string-split", 2, 0, 0,
  2904. (SCM str, SCM chr),
  2905. "Split the string @var{str} into the a list of the substrings delimited\n"
  2906. "by appearances of the character @var{chr}. Note that an empty substring\n"
  2907. "between separator characters will result in an empty string in the\n"
  2908. "result list.\n"
  2909. "\n"
  2910. "@lisp\n"
  2911. "(string-split \"root:x:0:0:root:/root:/bin/bash\" #\\:)\n"
  2912. "@result{}\n"
  2913. "(\"root\" \"x\" \"0\" \"0\" \"root\" \"/root\" \"/bin/bash\")\n"
  2914. "\n"
  2915. "(string-split \"::\" #\\:)\n"
  2916. "@result{}\n"
  2917. "(\"\" \"\" \"\")\n"
  2918. "\n"
  2919. "(string-split \"\" #\\:)\n"
  2920. "@result{}\n"
  2921. "(\"\")\n"
  2922. "@end lisp")
  2923. #define FUNC_NAME s_scm_string_split
  2924. {
  2925. long idx, last_idx;
  2926. const char * p;
  2927. char ch;
  2928. SCM res = SCM_EOL;
  2929. SCM_VALIDATE_STRING (1, str);
  2930. SCM_VALIDATE_CHAR (2, chr);
  2931. idx = scm_i_string_length (str);
  2932. p = scm_i_string_chars (str);
  2933. ch = SCM_CHAR (chr);
  2934. while (idx >= 0)
  2935. {
  2936. last_idx = idx;
  2937. while (idx > 0 && p[idx - 1] != ch)
  2938. idx--;
  2939. if (idx >= 0)
  2940. {
  2941. res = scm_cons (scm_c_substring (str, idx, last_idx), res);
  2942. p = scm_i_string_chars (str);
  2943. idx--;
  2944. }
  2945. }
  2946. scm_remember_upto_here_1 (str);
  2947. return res;
  2948. }
  2949. #undef FUNC_NAME
  2950. SCM_DEFINE (scm_string_filter, "string-filter", 2, 2, 0,
  2951. (SCM s, SCM char_pred, SCM start, SCM end),
  2952. "Filter the string @var{s}, retaining only those characters\n"
  2953. "which satisfy @var{char_pred}.\n"
  2954. "\n"
  2955. "If @var{char_pred} is a procedure, it is applied to each\n"
  2956. "character as a predicate, if it is a character, it is tested\n"
  2957. "for equality and if it is a character set, it is tested for\n"
  2958. "membership.")
  2959. #define FUNC_NAME s_scm_string_filter
  2960. {
  2961. const char *cstr;
  2962. size_t cstart, cend;
  2963. SCM result;
  2964. size_t idx;
  2965. MY_VALIDATE_SUBSTRING_SPEC_COPY (1, s, cstr,
  2966. 3, start, cstart,
  2967. 4, end, cend);
  2968. /* The explicit loops below stripping leading and trailing non-matches
  2969. mean we can return a substring if those are the only deletions, making
  2970. string-filter as efficient as string-trim-both in that case. */
  2971. if (SCM_CHARP (char_pred))
  2972. {
  2973. size_t count;
  2974. char chr;
  2975. chr = SCM_CHAR (char_pred);
  2976. /* strip leading non-matches by incrementing cstart */
  2977. while (cstart < cend && cstr[cstart] != chr)
  2978. cstart++;
  2979. /* strip trailing non-matches by decrementing cend */
  2980. while (cend > cstart && cstr[cend-1] != chr)
  2981. cend--;
  2982. /* count chars to keep */
  2983. count = 0;
  2984. for (idx = cstart; idx < cend; idx++)
  2985. if (cstr[idx] == chr)
  2986. count++;
  2987. if (count == cend - cstart)
  2988. {
  2989. /* whole of cstart to cend is to be kept, return a copy-on-write
  2990. substring */
  2991. result_substring:
  2992. result = scm_i_substring (s, cstart, cend);
  2993. }
  2994. else
  2995. result = scm_c_make_string (count, char_pred);
  2996. }
  2997. else if (SCM_CHARSETP (char_pred))
  2998. {
  2999. size_t count;
  3000. /* strip leading non-matches by incrementing cstart */
  3001. while (cstart < cend && ! SCM_CHARSET_GET (char_pred, cstr[cstart]))
  3002. cstart++;
  3003. /* strip trailing non-matches by decrementing cend */
  3004. while (cend > cstart && ! SCM_CHARSET_GET (char_pred, cstr[cend-1]))
  3005. cend--;
  3006. /* count chars to be kept */
  3007. count = 0;
  3008. for (idx = cstart; idx < cend; idx++)
  3009. if (SCM_CHARSET_GET (char_pred, cstr[idx]))
  3010. count++;
  3011. /* if whole of start to end kept then return substring */
  3012. if (count == cend - cstart)
  3013. goto result_substring;
  3014. else
  3015. {
  3016. char *dst;
  3017. result = scm_i_make_string (count, &dst);
  3018. cstr = scm_i_string_chars (s);
  3019. /* decrement "count" in this loop as well as using idx, so that if
  3020. another thread is simultaneously changing "s" there's no chance
  3021. it'll make us copy more than count characters */
  3022. for (idx = cstart; idx < cend && count != 0; idx++)
  3023. {
  3024. if (SCM_CHARSET_GET (char_pred, cstr[idx]))
  3025. {
  3026. *dst++ = cstr[idx];
  3027. count--;
  3028. }
  3029. }
  3030. }
  3031. }
  3032. else
  3033. {
  3034. SCM ls = SCM_EOL;
  3035. scm_t_trampoline_1 pred_tramp = scm_trampoline_1 (char_pred);
  3036. SCM_ASSERT (pred_tramp, char_pred, SCM_ARG2, FUNC_NAME);
  3037. idx = cstart;
  3038. while (idx < cend)
  3039. {
  3040. SCM res, ch;
  3041. ch = SCM_MAKE_CHAR (cstr[idx]);
  3042. res = pred_tramp (char_pred, ch);
  3043. if (scm_is_true (res))
  3044. ls = scm_cons (ch, ls);
  3045. cstr = scm_i_string_chars (s);
  3046. idx++;
  3047. }
  3048. result = scm_reverse_list_to_string (ls);
  3049. }
  3050. scm_remember_upto_here_1 (s);
  3051. return result;
  3052. }
  3053. #undef FUNC_NAME
  3054. SCM_DEFINE (scm_string_delete, "string-delete", 2, 2, 0,
  3055. (SCM s, SCM char_pred, SCM start, SCM end),
  3056. "Delete characters satisfying @var{char_pred} from @var{s}.\n"
  3057. "\n"
  3058. "If @var{char_pred} is a procedure, it is applied to each\n"
  3059. "character as a predicate, if it is a character, it is tested\n"
  3060. "for equality and if it is a character set, it is tested for\n"
  3061. "membership.")
  3062. #define FUNC_NAME s_scm_string_delete
  3063. {
  3064. const char *cstr;
  3065. size_t cstart, cend;
  3066. SCM result;
  3067. size_t idx;
  3068. MY_VALIDATE_SUBSTRING_SPEC_COPY (1, s, cstr,
  3069. 3, start, cstart,
  3070. 4, end, cend);
  3071. /* The explicit loops below stripping leading and trailing matches mean we
  3072. can return a substring if those are the only deletions, making
  3073. string-delete as efficient as string-trim-both in that case. */
  3074. if (SCM_CHARP (char_pred))
  3075. {
  3076. size_t count;
  3077. char chr;
  3078. chr = SCM_CHAR (char_pred);
  3079. /* strip leading matches by incrementing cstart */
  3080. while (cstart < cend && cstr[cstart] == chr)
  3081. cstart++;
  3082. /* strip trailing matches by decrementing cend */
  3083. while (cend > cstart && cstr[cend-1] == chr)
  3084. cend--;
  3085. /* count chars to be kept */
  3086. count = 0;
  3087. for (idx = cstart; idx < cend; idx++)
  3088. if (cstr[idx] != chr)
  3089. count++;
  3090. if (count == cend - cstart)
  3091. {
  3092. /* whole of cstart to cend is to be kept, return a copy-on-write
  3093. substring */
  3094. result_substring:
  3095. result = scm_i_substring (s, cstart, cend);
  3096. }
  3097. else
  3098. {
  3099. /* new string for retained portion */
  3100. char *dst;
  3101. result = scm_i_make_string (count, &dst);
  3102. cstr = scm_i_string_chars (s);
  3103. /* decrement "count" in this loop as well as using idx, so that if
  3104. another thread is simultaneously changing "s" there's no chance
  3105. it'll make us copy more than count characters */
  3106. for (idx = cstart; idx < cend && count != 0; idx++)
  3107. {
  3108. if (cstr[idx] != chr)
  3109. {
  3110. *dst++ = cstr[idx];
  3111. count--;
  3112. }
  3113. }
  3114. }
  3115. }
  3116. else if (SCM_CHARSETP (char_pred))
  3117. {
  3118. size_t count;
  3119. /* strip leading matches by incrementing cstart */
  3120. while (cstart < cend && SCM_CHARSET_GET (char_pred, cstr[cstart]))
  3121. cstart++;
  3122. /* strip trailing matches by decrementing cend */
  3123. while (cend > cstart && SCM_CHARSET_GET (char_pred, cstr[cend-1]))
  3124. cend--;
  3125. /* count chars to be kept */
  3126. count = 0;
  3127. for (idx = cstart; idx < cend; idx++)
  3128. if (! SCM_CHARSET_GET (char_pred, cstr[idx]))
  3129. count++;
  3130. if (count == cend - cstart)
  3131. goto result_substring;
  3132. else
  3133. {
  3134. /* new string for retained portion */
  3135. char *dst;
  3136. result = scm_i_make_string (count, &dst);
  3137. cstr = scm_i_string_chars (s);
  3138. /* decrement "count" in this loop as well as using idx, so that if
  3139. another thread is simultaneously changing "s" there's no chance
  3140. it'll make us copy more than count characters */
  3141. for (idx = cstart; idx < cend && count != 0; idx++)
  3142. {
  3143. if (! SCM_CHARSET_GET (char_pred, cstr[idx]))
  3144. {
  3145. *dst++ = cstr[idx];
  3146. count--;
  3147. }
  3148. }
  3149. }
  3150. }
  3151. else
  3152. {
  3153. SCM ls = SCM_EOL;
  3154. scm_t_trampoline_1 pred_tramp = scm_trampoline_1 (char_pred);
  3155. SCM_ASSERT (pred_tramp, char_pred, SCM_ARG2, FUNC_NAME);
  3156. idx = cstart;
  3157. while (idx < cend)
  3158. {
  3159. SCM res, ch = SCM_MAKE_CHAR (cstr[idx]);
  3160. res = pred_tramp (char_pred, ch);
  3161. if (scm_is_false (res))
  3162. ls = scm_cons (ch, ls);
  3163. cstr = scm_i_string_chars (s);
  3164. idx++;
  3165. }
  3166. result = scm_reverse_list_to_string (ls);
  3167. }
  3168. scm_remember_upto_here_1 (s);
  3169. return result;
  3170. }
  3171. #undef FUNC_NAME
  3172. void
  3173. scm_init_srfi_13 (void)
  3174. {
  3175. #include "libguile/srfi-13.x"
  3176. }
  3177. /* End of srfi-13.c. */