srfi-13.c 93 KB

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