srfi-13.c 93 KB

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