srfi-13.c 90 KB

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