srfi-13.c 93 KB

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