srfi-13.c 91 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352
  1. /* srfi-13.c --- SRFI-13 procedures for Guile
  2. *
  3. * Copyright (C) 2001, 2004, 2005, 2006, 2008, 2009, 2010, 2011 Free Software Foundation, Inc.
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public License
  7. * as published by the Free Software Foundation; either version 3 of
  8. * the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  18. * 02110-1301 USA
  19. */
  20. #ifdef HAVE_CONFIG_H
  21. # include <config.h>
  22. #endif
  23. #include <string.h>
  24. #include <unicase.h>
  25. #include <unictype.h>
  26. #include "libguile.h"
  27. #include <libguile/deprecation.h>
  28. #include "libguile/srfi-13.h"
  29. #include "libguile/srfi-14.h"
  30. #define MY_VALIDATE_SUBSTRING_SPEC(pos_str, str, \
  31. pos_start, start, c_start, \
  32. pos_end, end, c_end) \
  33. do { \
  34. SCM_VALIDATE_STRING (pos_str, str); \
  35. scm_i_get_substring_spec (scm_i_string_length (str), \
  36. start, &c_start, end, &c_end); \
  37. } while (0)
  38. #define MY_SUBF_VALIDATE_SUBSTRING_SPEC(fname, pos_str, str, \
  39. pos_start, start, c_start, \
  40. pos_end, end, c_end) \
  41. do { \
  42. SCM_ASSERT_TYPE (scm_is_string (str), str, pos_str, fname, "string"); \
  43. scm_i_get_substring_spec (scm_i_string_length (str), \
  44. start, &c_start, end, &c_end); \
  45. } while (0)
  46. #define REF_IN_CHARSET(s, i, cs) \
  47. (scm_is_true (scm_char_set_contains_p ((cs), SCM_MAKE_CHAR (scm_i_string_ref (s, i)))))
  48. SCM_DEFINE (scm_string_null_p, "string-null?", 1, 0, 0,
  49. (SCM str),
  50. "Return @code{#t} if @var{str}'s length is zero, and\n"
  51. "@code{#f} otherwise.\n"
  52. "@lisp\n"
  53. "(string-null? \"\") @result{} #t\n"
  54. "y @result{} \"foo\"\n"
  55. "(string-null? y) @result{} #f\n"
  56. "@end lisp")
  57. #define FUNC_NAME s_scm_string_null_p
  58. {
  59. SCM_VALIDATE_STRING (1, str);
  60. return scm_from_bool (scm_i_string_length (str) == 0);
  61. }
  62. #undef FUNC_NAME
  63. #if 0
  64. static void
  65. race_error ()
  66. {
  67. scm_misc_error (NULL, "race condition detected", SCM_EOL);
  68. }
  69. #endif
  70. SCM_DEFINE (scm_string_any, "string-any-c-code", 2, 2, 0,
  71. (SCM char_pred, SCM s, SCM start, SCM end),
  72. "Check if @var{char_pred} is true for any character in string @var{s}.\n"
  73. "\n"
  74. "@var{char_pred} can be a character to check for any equal to that, or\n"
  75. "a character set (@pxref{Character Sets}) to check for any in that set,\n"
  76. "or a predicate procedure to call.\n"
  77. "\n"
  78. "For a procedure, calls @code{(@var{char_pred} c)} are made\n"
  79. "successively on the characters from @var{start} to @var{end}. If\n"
  80. "@var{char_pred} returns true (ie.@: non-@code{#f}), @code{string-any}\n"
  81. "stops and that return value is the return from @code{string-any}. The\n"
  82. "call on the last character (ie.@: at @math{@var{end}-1}), if that\n"
  83. "point is reached, is a tail call.\n"
  84. "\n"
  85. "If there are no characters in @var{s} (ie.@: @var{start} equals\n"
  86. "@var{end}) then the return is @code{#f}.\n")
  87. #define FUNC_NAME s_scm_string_any
  88. {
  89. size_t cstart, cend;
  90. SCM res = SCM_BOOL_F;
  91. MY_VALIDATE_SUBSTRING_SPEC (2, s,
  92. 3, start, cstart,
  93. 4, end, cend);
  94. if (SCM_CHARP (char_pred))
  95. {
  96. size_t i;
  97. for (i = cstart; i < cend; i ++)
  98. if (scm_i_string_ref (s, i) == SCM_CHAR (char_pred))
  99. {
  100. res = SCM_BOOL_T;
  101. break;
  102. }
  103. }
  104. else if (SCM_CHARSETP (char_pred))
  105. {
  106. size_t i;
  107. for (i = cstart; i < cend; i++)
  108. if (REF_IN_CHARSET (s, i, char_pred))
  109. {
  110. res = SCM_BOOL_T;
  111. break;
  112. }
  113. }
  114. else
  115. {
  116. SCM_ASSERT (scm_is_true (scm_procedure_p (char_pred)),
  117. char_pred, SCM_ARG1, FUNC_NAME);
  118. while (cstart < cend)
  119. {
  120. res = scm_call_1 (char_pred,
  121. SCM_MAKE_CHAR (scm_i_string_ref (s, cstart)));
  122. if (scm_is_true (res))
  123. break;
  124. cstart++;
  125. }
  126. }
  127. scm_remember_upto_here_1 (s);
  128. return res;
  129. }
  130. #undef FUNC_NAME
  131. SCM_DEFINE (scm_string_every, "string-every-c-code", 2, 2, 0,
  132. (SCM char_pred, SCM s, SCM start, SCM end),
  133. "Check if @var{char_pred} is true for every character in string\n"
  134. "@var{s}.\n"
  135. "\n"
  136. "@var{char_pred} can be a character to check for every character equal\n"
  137. "to that, or a character set (@pxref{Character Sets}) to check for\n"
  138. "every character being in that set, or a predicate procedure to call.\n"
  139. "\n"
  140. "For a procedure, calls @code{(@var{char_pred} c)} are made\n"
  141. "successively on the characters from @var{start} to @var{end}. If\n"
  142. "@var{char_pred} returns @code{#f}, @code{string-every} stops and\n"
  143. "returns @code{#f}. The call on the last character (ie.@: at\n"
  144. "@math{@var{end}-1}), if that point is reached, is a tail call and the\n"
  145. "return from that call is the return from @code{string-every}.\n"
  146. "\n"
  147. "If there are no characters in @var{s} (ie.@: @var{start} equals\n"
  148. "@var{end}) then the return is @code{#t}.\n")
  149. #define FUNC_NAME s_scm_string_every
  150. {
  151. size_t cstart, cend;
  152. SCM res = SCM_BOOL_T;
  153. MY_VALIDATE_SUBSTRING_SPEC (2, s,
  154. 3, start, cstart,
  155. 4, end, cend);
  156. if (SCM_CHARP (char_pred))
  157. {
  158. size_t i;
  159. for (i = cstart; i < cend; i++)
  160. if (scm_i_string_ref (s, i) != SCM_CHAR (char_pred))
  161. {
  162. res = SCM_BOOL_F;
  163. break;
  164. }
  165. }
  166. else if (SCM_CHARSETP (char_pred))
  167. {
  168. size_t i;
  169. for (i = cstart; i < cend; i++)
  170. if (!REF_IN_CHARSET (s, i, char_pred))
  171. {
  172. res = SCM_BOOL_F;
  173. break;
  174. }
  175. }
  176. else
  177. {
  178. SCM_ASSERT (scm_is_true (scm_procedure_p (char_pred)),
  179. char_pred, SCM_ARG1, FUNC_NAME);
  180. while (cstart < cend)
  181. {
  182. res = scm_call_1 (char_pred,
  183. SCM_MAKE_CHAR (scm_i_string_ref (s, cstart)));
  184. if (scm_is_false (res))
  185. break;
  186. cstart++;
  187. }
  188. }
  189. scm_remember_upto_here_1 (s);
  190. return res;
  191. }
  192. #undef FUNC_NAME
  193. SCM_DEFINE (scm_string_tabulate, "string-tabulate", 2, 0, 0,
  194. (SCM proc, SCM len),
  195. "@var{proc} is an integer->char procedure. Construct a string\n"
  196. "of size @var{len} by applying @var{proc} to each index to\n"
  197. "produce the corresponding string element. The order in which\n"
  198. "@var{proc} is applied to the indices is not specified.")
  199. #define FUNC_NAME s_scm_string_tabulate
  200. {
  201. size_t clen, i;
  202. SCM res;
  203. SCM ch;
  204. SCM_ASSERT (scm_is_true (scm_procedure_p (proc)),
  205. proc, SCM_ARG1, FUNC_NAME);
  206. SCM_ASSERT_RANGE (2, len, scm_to_int (len) >= 0);
  207. clen = scm_to_size_t (len);
  208. {
  209. /* This function is more complicated than necessary for the sake
  210. of speed. */
  211. scm_t_wchar *buf = scm_malloc (clen * sizeof (scm_t_wchar));
  212. int wide = 0;
  213. i = 0;
  214. while (i < clen)
  215. {
  216. ch = scm_call_1 (proc, scm_from_size_t (i));
  217. if (!SCM_CHARP (ch))
  218. {
  219. SCM_MISC_ERROR ("procedure ~S returned non-char", scm_list_1 (proc));
  220. }
  221. if (SCM_CHAR (ch) > 255)
  222. wide = 1;
  223. buf[i] = SCM_CHAR (ch);
  224. i++;
  225. }
  226. if (wide)
  227. {
  228. scm_t_wchar *wbuf = NULL;
  229. res = scm_i_make_wide_string (clen, &wbuf, 0);
  230. memcpy (wbuf, buf, clen * sizeof (scm_t_wchar));
  231. free (buf);
  232. }
  233. else
  234. {
  235. char *nbuf = NULL;
  236. res = scm_i_make_string (clen, &nbuf, 0);
  237. for (i = 0; i < clen; i ++)
  238. nbuf[i] = (unsigned char) buf[i];
  239. free (buf);
  240. }
  241. }
  242. return res;
  243. }
  244. #undef FUNC_NAME
  245. SCM_DEFINE (scm_substring_to_list, "string->list", 1, 2, 0,
  246. (SCM str, SCM start, SCM end),
  247. "Convert the string @var{str} into a list of characters.")
  248. #define FUNC_NAME s_scm_substring_to_list
  249. {
  250. size_t cstart, cend;
  251. int narrow;
  252. SCM result = SCM_EOL;
  253. MY_VALIDATE_SUBSTRING_SPEC (1, str,
  254. 2, start, cstart,
  255. 3, end, cend);
  256. /* This explicit narrow/wide logic (instead of just using
  257. scm_i_string_ref) is for speed optimizaion. */
  258. narrow = scm_i_is_narrow_string (str);
  259. if (narrow)
  260. {
  261. const char *buf = scm_i_string_chars (str);
  262. while (cstart < cend)
  263. {
  264. cend--;
  265. result = scm_cons (SCM_MAKE_CHAR (buf[cend]), result);
  266. }
  267. }
  268. else
  269. {
  270. const scm_t_wchar *buf = scm_i_string_wide_chars (str);
  271. while (cstart < cend)
  272. {
  273. cend--;
  274. result = scm_cons (SCM_MAKE_CHAR (buf[cend]), result);
  275. }
  276. }
  277. scm_remember_upto_here_1 (str);
  278. return result;
  279. }
  280. #undef FUNC_NAME
  281. /* We export scm_substring_to_list as "string->list" since it is
  282. compatible and more general. This function remains for the benefit
  283. of C code that used it.
  284. */
  285. SCM
  286. scm_string_to_list (SCM str)
  287. {
  288. return scm_substring_to_list (str, SCM_UNDEFINED, SCM_UNDEFINED);
  289. }
  290. SCM_DEFINE (scm_reverse_list_to_string, "reverse-list->string", 1, 0, 0,
  291. (SCM chrs),
  292. "An efficient implementation of @code{(compose string->list\n"
  293. "reverse)}:\n"
  294. "\n"
  295. "@smalllisp\n"
  296. "(reverse-list->string '(#\\a #\\B #\\c)) @result{} \"cBa\"\n"
  297. "@end smalllisp")
  298. #define FUNC_NAME s_scm_reverse_list_to_string
  299. {
  300. SCM result;
  301. long i = scm_ilength (chrs), j;
  302. char *data;
  303. if (i < 0)
  304. SCM_WRONG_TYPE_ARG (1, chrs);
  305. result = scm_i_make_string (i, &data, 0);
  306. {
  307. SCM rest;
  308. rest = chrs;
  309. j = 0;
  310. while (j < i && scm_is_pair (rest))
  311. {
  312. SCM elt = SCM_CAR (rest);
  313. SCM_VALIDATE_CHAR (SCM_ARGn, elt);
  314. j++;
  315. rest = SCM_CDR (rest);
  316. }
  317. rest = chrs;
  318. j = i;
  319. result = scm_i_string_start_writing (result);
  320. while (j > 0 && scm_is_pair (rest))
  321. {
  322. SCM elt = SCM_CAR (rest);
  323. scm_i_string_set_x (result, j-1, SCM_CHAR (elt));
  324. rest = SCM_CDR (rest);
  325. j--;
  326. }
  327. scm_i_string_stop_writing ();
  328. }
  329. return result;
  330. }
  331. #undef FUNC_NAME
  332. SCM_SYMBOL (scm_sym_infix, "infix");
  333. SCM_SYMBOL (scm_sym_strict_infix, "strict-infix");
  334. SCM_SYMBOL (scm_sym_suffix, "suffix");
  335. SCM_SYMBOL (scm_sym_prefix, "prefix");
  336. SCM_DEFINE (scm_string_join, "string-join", 1, 2, 0,
  337. (SCM ls, SCM delimiter, SCM grammar),
  338. "Append the string in the string list @var{ls}, using the string\n"
  339. "@var{delimiter} as a delimiter between the elements of @var{ls}.\n"
  340. "@var{grammar} is a symbol which specifies how the delimiter is\n"
  341. "placed between the strings, and defaults to the symbol\n"
  342. "@code{infix}.\n"
  343. "\n"
  344. "@table @code\n"
  345. "@item infix\n"
  346. "Insert the separator between list elements. An empty string\n"
  347. "will produce an empty list.\n"
  348. "@item string-infix\n"
  349. "Like @code{infix}, but will raise an error if given the empty\n"
  350. "list.\n"
  351. "@item suffix\n"
  352. "Insert the separator after every list element.\n"
  353. "@item prefix\n"
  354. "Insert the separator before each list element.\n"
  355. "@end table")
  356. #define FUNC_NAME s_scm_string_join
  357. {
  358. #define GRAM_INFIX 0
  359. #define GRAM_STRICT_INFIX 1
  360. #define GRAM_SUFFIX 2
  361. #define GRAM_PREFIX 3
  362. SCM tmp;
  363. SCM result;
  364. int gram = GRAM_INFIX;
  365. size_t del_len = 0;
  366. long strings = scm_ilength (ls);
  367. /* Validate the string list. */
  368. if (strings < 0)
  369. SCM_WRONG_TYPE_ARG (1, ls);
  370. /* Validate the delimiter and record its length. */
  371. if (SCM_UNBNDP (delimiter))
  372. {
  373. delimiter = scm_from_locale_string (" ");
  374. del_len = 1;
  375. }
  376. else
  377. {
  378. SCM_VALIDATE_STRING (2, delimiter);
  379. del_len = scm_i_string_length (delimiter);
  380. }
  381. /* Validate the grammar symbol and remember the grammar. */
  382. if (SCM_UNBNDP (grammar))
  383. gram = GRAM_INFIX;
  384. else if (scm_is_eq (grammar, scm_sym_infix))
  385. gram = GRAM_INFIX;
  386. else if (scm_is_eq (grammar, scm_sym_strict_infix))
  387. gram = GRAM_STRICT_INFIX;
  388. else if (scm_is_eq (grammar, scm_sym_suffix))
  389. gram = GRAM_SUFFIX;
  390. else if (scm_is_eq (grammar, scm_sym_prefix))
  391. gram = GRAM_PREFIX;
  392. else
  393. SCM_WRONG_TYPE_ARG (3, grammar);
  394. /* Check grammar constraints. */
  395. if (strings == 0 && gram == GRAM_STRICT_INFIX)
  396. SCM_MISC_ERROR ("strict-infix grammar requires non-empty list",
  397. SCM_EOL);
  398. result = scm_i_make_string (0, NULL, 0);
  399. tmp = ls;
  400. switch (gram)
  401. {
  402. case GRAM_INFIX:
  403. case GRAM_STRICT_INFIX:
  404. while (scm_is_pair (tmp))
  405. {
  406. result = scm_string_append (scm_list_2 (result, SCM_CAR (tmp)));
  407. if (!scm_is_null (SCM_CDR (tmp)) && del_len > 0)
  408. result = scm_string_append (scm_list_2 (result, delimiter));
  409. tmp = SCM_CDR (tmp);
  410. }
  411. break;
  412. case GRAM_SUFFIX:
  413. while (scm_is_pair (tmp))
  414. {
  415. result = scm_string_append (scm_list_2 (result, SCM_CAR (tmp)));
  416. if (del_len > 0)
  417. result = scm_string_append (scm_list_2 (result, delimiter));
  418. tmp = SCM_CDR (tmp);
  419. }
  420. break;
  421. case GRAM_PREFIX:
  422. while (scm_is_pair (tmp))
  423. {
  424. if (del_len > 0)
  425. result = scm_string_append (scm_list_2 (result, delimiter));
  426. result = scm_string_append (scm_list_2 (result, SCM_CAR (tmp)));
  427. tmp = SCM_CDR (tmp);
  428. }
  429. break;
  430. }
  431. return result;
  432. #undef GRAM_INFIX
  433. #undef GRAM_STRICT_INFIX
  434. #undef GRAM_SUFFIX
  435. #undef GRAM_PREFIX
  436. }
  437. #undef FUNC_NAME
  438. /* There are a number of functions to consider here for Scheme and C:
  439. string-copy STR [start [end]] ;; SRFI-13 variant of R5RS string-copy
  440. substring/copy STR start [end] ;; Guile variant of R5RS substring
  441. scm_string_copy (str) ;; Old function from Guile
  442. scm_substring_copy (str, [start, [end]])
  443. ;; C version of SRFI-13 string-copy
  444. ;; and C version of substring/copy
  445. The C function underlying string-copy is not exported to C
  446. programs. scm_substring_copy is defined in strings.c as the
  447. underlying function of substring/copy and allows an optional START
  448. argument.
  449. */
  450. SCM scm_srfi13_substring_copy (SCM str, SCM start, SCM end);
  451. SCM_DEFINE (scm_srfi13_substring_copy, "string-copy", 1, 2, 0,
  452. (SCM str, SCM start, SCM end),
  453. "Return a freshly allocated copy of the string @var{str}. If\n"
  454. "given, @var{start} and @var{end} delimit the portion of\n"
  455. "@var{str} which is copied.")
  456. #define FUNC_NAME s_scm_srfi13_substring_copy
  457. {
  458. size_t cstart, cend;
  459. MY_VALIDATE_SUBSTRING_SPEC (1, str,
  460. 2, start, cstart,
  461. 3, end, cend);
  462. return scm_i_substring_copy (str, cstart, cend);
  463. }
  464. #undef FUNC_NAME
  465. SCM
  466. scm_string_copy (SCM str)
  467. {
  468. if (!scm_is_string (str))
  469. scm_wrong_type_arg ("scm_string_copy", 0, str);
  470. return scm_i_substring (str, 0, scm_i_string_length (str));
  471. }
  472. SCM_DEFINE (scm_string_copy_x, "string-copy!", 3, 2, 0,
  473. (SCM target, SCM tstart, SCM s, SCM start, SCM end),
  474. "Copy the sequence of characters from index range [@var{start},\n"
  475. "@var{end}) in string @var{s} to string @var{target}, beginning\n"
  476. "at index @var{tstart}. The characters are copied left-to-right\n"
  477. "or right-to-left as needed -- the copy is guaranteed to work,\n"
  478. "even if @var{target} and @var{s} are the same string. It is an\n"
  479. "error if the copy operation runs off the end of the target\n"
  480. "string.")
  481. #define FUNC_NAME s_scm_string_copy_x
  482. {
  483. size_t cstart, cend, ctstart, dummy, len, i;
  484. SCM sdummy = SCM_UNDEFINED;
  485. MY_VALIDATE_SUBSTRING_SPEC (1, target,
  486. 2, tstart, ctstart,
  487. 2, sdummy, dummy);
  488. MY_VALIDATE_SUBSTRING_SPEC (3, s,
  489. 4, start, cstart,
  490. 5, end, cend);
  491. if (cstart < cend)
  492. {
  493. len = cend - cstart;
  494. SCM_ASSERT_RANGE (3, s, len <= scm_i_string_length (target) - ctstart);
  495. target = scm_i_string_start_writing (target);
  496. for (i = 0; i < cend - cstart; i++)
  497. {
  498. scm_i_string_set_x (target, ctstart + i,
  499. scm_i_string_ref (s, cstart + i));
  500. }
  501. scm_i_string_stop_writing ();
  502. scm_remember_upto_here_1 (target);
  503. }
  504. return SCM_UNSPECIFIED;
  505. }
  506. #undef FUNC_NAME
  507. SCM_DEFINE (scm_substring_move_x, "substring-move!", 5, 0, 0,
  508. (SCM str1, SCM start1, SCM end1, SCM str2, SCM start2),
  509. "Copy the substring of @var{str1} bounded by @var{start1} and @var{end1}\n"
  510. "into @var{str2} beginning at position @var{start2}.\n"
  511. "@var{str1} and @var{str2} can be the same string.")
  512. #define FUNC_NAME s_scm_substring_move_x
  513. {
  514. return scm_string_copy_x (str2, start2, str1, start1, end1);
  515. }
  516. #undef FUNC_NAME
  517. SCM_DEFINE (scm_string_take, "string-take", 2, 0, 0,
  518. (SCM s, SCM n),
  519. "Return the @var{n} first characters of @var{s}.")
  520. #define FUNC_NAME s_scm_string_take
  521. {
  522. return scm_substring (s, SCM_INUM0, n);
  523. }
  524. #undef FUNC_NAME
  525. SCM_DEFINE (scm_string_drop, "string-drop", 2, 0, 0,
  526. (SCM s, SCM n),
  527. "Return all but the first @var{n} characters of @var{s}.")
  528. #define FUNC_NAME s_scm_string_drop
  529. {
  530. return scm_substring (s, n, SCM_UNDEFINED);
  531. }
  532. #undef FUNC_NAME
  533. SCM_DEFINE (scm_string_take_right, "string-take-right", 2, 0, 0,
  534. (SCM s, SCM n),
  535. "Return the @var{n} last characters of @var{s}.")
  536. #define FUNC_NAME s_scm_string_take_right
  537. {
  538. return scm_substring (s,
  539. scm_difference (scm_string_length (s), n),
  540. SCM_UNDEFINED);
  541. }
  542. #undef FUNC_NAME
  543. SCM_DEFINE (scm_string_drop_right, "string-drop-right", 2, 0, 0,
  544. (SCM s, SCM n),
  545. "Return all but the last @var{n} characters of @var{s}.")
  546. #define FUNC_NAME s_scm_string_drop_right
  547. {
  548. return scm_substring (s,
  549. SCM_INUM0,
  550. scm_difference (scm_string_length (s), n));
  551. }
  552. #undef FUNC_NAME
  553. SCM_DEFINE (scm_string_pad, "string-pad", 2, 3, 0,
  554. (SCM s, SCM len, SCM chr, SCM start, SCM end),
  555. "Take that characters from @var{start} to @var{end} from the\n"
  556. "string @var{s} and return a new string, right-padded by the\n"
  557. "character @var{chr} to length @var{len}. If the resulting\n"
  558. "string is longer than @var{len}, it is truncated on the right.")
  559. #define FUNC_NAME s_scm_string_pad
  560. {
  561. size_t cstart, cend, clen;
  562. MY_VALIDATE_SUBSTRING_SPEC (1, s,
  563. 4, start, cstart,
  564. 5, end, cend);
  565. clen = scm_to_size_t (len);
  566. if (SCM_UNBNDP (chr))
  567. chr = SCM_MAKE_CHAR (' ');
  568. else
  569. {
  570. SCM_VALIDATE_CHAR (3, chr);
  571. }
  572. if (clen < (cend - cstart))
  573. return scm_i_substring (s, cend - clen, cend);
  574. else
  575. {
  576. SCM result;
  577. result = (scm_string_append
  578. (scm_list_2 (scm_c_make_string (clen - (cend - cstart), chr),
  579. scm_i_substring (s, cstart, cend))));
  580. return result;
  581. }
  582. }
  583. #undef FUNC_NAME
  584. SCM_DEFINE (scm_string_pad_right, "string-pad-right", 2, 3, 0,
  585. (SCM s, SCM len, SCM chr, SCM start, SCM end),
  586. "Take that characters from @var{start} to @var{end} from the\n"
  587. "string @var{s} and return a new string, left-padded by the\n"
  588. "character @var{chr} to length @var{len}. If the resulting\n"
  589. "string is longer than @var{len}, it is truncated on the left.")
  590. #define FUNC_NAME s_scm_string_pad_right
  591. {
  592. size_t cstart, cend, clen;
  593. MY_VALIDATE_SUBSTRING_SPEC (1, s,
  594. 4, start, cstart,
  595. 5, end, cend);
  596. clen = scm_to_size_t (len);
  597. if (SCM_UNBNDP (chr))
  598. chr = SCM_MAKE_CHAR (' ');
  599. else
  600. {
  601. SCM_VALIDATE_CHAR (3, chr);
  602. }
  603. if (clen < (cend - cstart))
  604. return scm_i_substring (s, cstart, cstart + clen);
  605. else
  606. {
  607. SCM result;
  608. result = (scm_string_append
  609. (scm_list_2 (scm_i_substring (s, cstart, cend),
  610. scm_c_make_string (clen - (cend - cstart), chr))));
  611. return result;
  612. }
  613. }
  614. #undef FUNC_NAME
  615. SCM_DEFINE (scm_string_trim, "string-trim", 1, 3, 0,
  616. (SCM s, SCM char_pred, SCM start, SCM end),
  617. "Trim @var{s} by skipping over all characters on the left\n"
  618. "that satisfy the parameter @var{char_pred}:\n"
  619. "\n"
  620. "@itemize @bullet\n"
  621. "@item\n"
  622. "if it is the character @var{ch}, characters equal to\n"
  623. "@var{ch} are trimmed,\n"
  624. "\n"
  625. "@item\n"
  626. "if it is a procedure @var{pred} characters that\n"
  627. "satisfy @var{pred} are trimmed,\n"
  628. "\n"
  629. "@item\n"
  630. "if it is a character set, characters in that set are trimmed.\n"
  631. "@end itemize\n"
  632. "\n"
  633. "If called without a @var{char_pred} argument, all whitespace is\n"
  634. "trimmed.")
  635. #define FUNC_NAME s_scm_string_trim
  636. {
  637. size_t cstart, cend;
  638. MY_VALIDATE_SUBSTRING_SPEC (1, s,
  639. 3, start, cstart,
  640. 4, end, cend);
  641. if (SCM_UNBNDP (char_pred))
  642. {
  643. while (cstart < cend)
  644. {
  645. if (!uc_is_c_whitespace (scm_i_string_ref (s, cstart)))
  646. break;
  647. cstart++;
  648. }
  649. }
  650. else if (SCM_CHARP (char_pred))
  651. {
  652. while (cstart < cend)
  653. {
  654. if (scm_i_string_ref (s, cstart) != SCM_CHAR (char_pred))
  655. break;
  656. cstart++;
  657. }
  658. }
  659. else if (SCM_CHARSETP (char_pred))
  660. {
  661. while (cstart < cend)
  662. {
  663. if (!REF_IN_CHARSET (s, cstart, char_pred))
  664. break;
  665. cstart++;
  666. }
  667. }
  668. else
  669. {
  670. SCM_ASSERT (scm_is_true (scm_procedure_p (char_pred)),
  671. char_pred, SCM_ARG2, FUNC_NAME);
  672. while (cstart < cend)
  673. {
  674. SCM res;
  675. res = scm_call_1 (char_pred, SCM_MAKE_CHAR (scm_i_string_ref (s, cstart)));
  676. if (scm_is_false (res))
  677. break;
  678. cstart++;
  679. }
  680. }
  681. return scm_i_substring (s, cstart, cend);
  682. }
  683. #undef FUNC_NAME
  684. SCM_DEFINE (scm_string_trim_right, "string-trim-right", 1, 3, 0,
  685. (SCM s, SCM char_pred, SCM start, SCM end),
  686. "Trim @var{s} by skipping over all characters on the right\n"
  687. "that satisfy the parameter @var{char_pred}:\n"
  688. "\n"
  689. "@itemize @bullet\n"
  690. "@item\n"
  691. "if it is the character @var{ch}, characters equal to @var{ch}\n"
  692. "are trimmed,\n"
  693. "\n"
  694. "@item\n"
  695. "if it is a procedure @var{pred} characters that satisfy\n"
  696. "@var{pred} are trimmed,\n"
  697. "\n"
  698. "@item\n"
  699. "if it is a character sets, all characters in that set are\n"
  700. "trimmed.\n"
  701. "@end itemize\n"
  702. "\n"
  703. "If called without a @var{char_pred} argument, all whitespace is\n"
  704. "trimmed.")
  705. #define FUNC_NAME s_scm_string_trim_right
  706. {
  707. size_t cstart, cend;
  708. MY_VALIDATE_SUBSTRING_SPEC (1, s,
  709. 3, start, cstart,
  710. 4, end, cend);
  711. if (SCM_UNBNDP (char_pred))
  712. {
  713. while (cstart < cend)
  714. {
  715. if (!uc_is_c_whitespace (scm_i_string_ref (s, cend - 1)))
  716. break;
  717. cend--;
  718. }
  719. }
  720. else if (SCM_CHARP (char_pred))
  721. {
  722. while (cstart < cend)
  723. {
  724. if (scm_i_string_ref (s, cend - 1) != SCM_CHAR (char_pred))
  725. break;
  726. cend--;
  727. }
  728. }
  729. else if (SCM_CHARSETP (char_pred))
  730. {
  731. while (cstart < cend)
  732. {
  733. if (!REF_IN_CHARSET (s, cend-1, char_pred))
  734. break;
  735. cend--;
  736. }
  737. }
  738. else
  739. {
  740. SCM_ASSERT (scm_is_true (scm_procedure_p (char_pred)),
  741. char_pred, SCM_ARG2, FUNC_NAME);
  742. while (cstart < cend)
  743. {
  744. SCM res;
  745. res = scm_call_1 (char_pred, SCM_MAKE_CHAR (scm_i_string_ref (s, cend - 1)));
  746. if (scm_is_false (res))
  747. break;
  748. cend--;
  749. }
  750. }
  751. return scm_i_substring (s, cstart, cend);
  752. }
  753. #undef FUNC_NAME
  754. SCM_DEFINE (scm_string_trim_both, "string-trim-both", 1, 3, 0,
  755. (SCM s, SCM char_pred, SCM start, SCM end),
  756. "Trim @var{s} by skipping over all characters on both sides of\n"
  757. "the string that satisfy the parameter @var{char_pred}:\n"
  758. "\n"
  759. "@itemize @bullet\n"
  760. "@item\n"
  761. "if it is the character @var{ch}, characters equal to @var{ch}\n"
  762. "are trimmed,\n"
  763. "\n"
  764. "@item\n"
  765. "if it is a procedure @var{pred} characters that satisfy\n"
  766. "@var{pred} are trimmed,\n"
  767. "\n"
  768. "@item\n"
  769. "if it is a character set, the characters in the set are\n"
  770. "trimmed.\n"
  771. "@end itemize\n"
  772. "\n"
  773. "If called without a @var{char_pred} argument, all whitespace is\n"
  774. "trimmed.")
  775. #define FUNC_NAME s_scm_string_trim_both
  776. {
  777. size_t cstart, cend;
  778. MY_VALIDATE_SUBSTRING_SPEC (1, s,
  779. 3, start, cstart,
  780. 4, end, cend);
  781. if (SCM_UNBNDP (char_pred))
  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_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_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_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_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_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_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_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_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_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. result = scm_i_string_start_writing (result);
  2239. scm_i_string_set_x (result, p, SCM_CHAR (ch));
  2240. scm_i_string_stop_writing ();
  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_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_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. str = scm_i_string_start_writing (str);
  2366. scm_i_string_set_x (str, i, SCM_CHAR (ch));
  2367. scm_i_string_stop_writing ();
  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. str = scm_i_string_start_writing (str);
  2427. scm_i_string_set_x (str, i, SCM_CHAR (ch));
  2428. scm_i_string_stop_writing ();
  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_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. result = scm_i_string_start_writing (result);
  2529. p = 0;
  2530. while (cfrom < cto)
  2531. {
  2532. size_t t = ((cfrom < 0) ? -cfrom : cfrom) % (cend - cstart);
  2533. if (cfrom < 0)
  2534. scm_i_string_set_x (result, p,
  2535. scm_i_string_ref (s, (cend - cstart) - t));
  2536. else
  2537. scm_i_string_set_x (result, p, scm_i_string_ref (s, t));
  2538. cfrom++;
  2539. p++;
  2540. }
  2541. scm_i_string_stop_writing ();
  2542. scm_remember_upto_here_1 (s);
  2543. return result;
  2544. }
  2545. #undef FUNC_NAME
  2546. SCM_DEFINE (scm_string_xcopy_x, "string-xcopy!", 4, 3, 0,
  2547. (SCM target, SCM tstart, SCM s, SCM sfrom, SCM sto, SCM start, SCM end),
  2548. "Exactly the same as @code{xsubstring}, but the extracted text\n"
  2549. "is written into the string @var{target} starting at index\n"
  2550. "@var{tstart}. The operation is not defined if @code{(eq?\n"
  2551. "@var{target} @var{s})} or these arguments share storage -- you\n"
  2552. "cannot copy a string on top of itself.")
  2553. #define FUNC_NAME s_scm_string_xcopy_x
  2554. {
  2555. size_t p;
  2556. size_t ctstart, cstart, cend;
  2557. int csfrom, csto;
  2558. SCM dummy = SCM_UNDEFINED;
  2559. size_t cdummy;
  2560. MY_VALIDATE_SUBSTRING_SPEC (1, target,
  2561. 2, tstart, ctstart,
  2562. 2, dummy, cdummy);
  2563. MY_VALIDATE_SUBSTRING_SPEC (3, s,
  2564. 6, start, cstart,
  2565. 7, end, cend);
  2566. csfrom = scm_to_int (sfrom);
  2567. if (SCM_UNBNDP (sto))
  2568. csto = csfrom + (cend - cstart);
  2569. else
  2570. csto = scm_to_int (sto);
  2571. if (csfrom < csto)
  2572. {
  2573. if (cstart == cend)
  2574. SCM_MISC_ERROR ("start and end indices must not be equal", SCM_EOL);
  2575. SCM_ASSERT_RANGE (1, tstart,
  2576. ctstart + (csto - csfrom) <= scm_i_string_length (target));
  2577. p = 0;
  2578. target = scm_i_string_start_writing (target);
  2579. while (csfrom < csto)
  2580. {
  2581. size_t t = ((csfrom < 0) ? -csfrom : csfrom) % (cend - cstart);
  2582. if (csfrom < 0)
  2583. scm_i_string_set_x (target, p + cstart, scm_i_string_ref (s, (cend - cstart) - t));
  2584. else
  2585. scm_i_string_set_x (target, p + cstart, scm_i_string_ref (s, t));
  2586. csfrom++;
  2587. p++;
  2588. }
  2589. scm_i_string_stop_writing ();
  2590. scm_remember_upto_here_2 (target, s);
  2591. }
  2592. return SCM_UNSPECIFIED;
  2593. }
  2594. #undef FUNC_NAME
  2595. SCM_DEFINE (scm_string_replace, "string-replace", 2, 4, 0,
  2596. (SCM s1, SCM s2, SCM start1, SCM end1, SCM start2, SCM end2),
  2597. "Return the string @var{s1}, but with the characters\n"
  2598. "@var{start1} @dots{} @var{end1} replaced by the characters\n"
  2599. "@var{start2} @dots{} @var{end2} from @var{s2}.")
  2600. #define FUNC_NAME s_scm_string_replace
  2601. {
  2602. size_t cstart1, cend1, cstart2, cend2;
  2603. SCM result;
  2604. MY_VALIDATE_SUBSTRING_SPEC (1, s1,
  2605. 3, start1, cstart1,
  2606. 4, end1, cend1);
  2607. MY_VALIDATE_SUBSTRING_SPEC (2, s2,
  2608. 5, start2, cstart2,
  2609. 6, end2, cend2);
  2610. return (scm_string_append
  2611. (scm_list_3 (scm_i_substring (s1, 0, cstart1),
  2612. scm_i_substring (s2, cstart2, cend2),
  2613. scm_i_substring (s1, cend1, scm_i_string_length (s1)))));
  2614. return result;
  2615. }
  2616. #undef FUNC_NAME
  2617. SCM_DEFINE (scm_string_tokenize, "string-tokenize", 1, 3, 0,
  2618. (SCM s, SCM token_set, SCM start, SCM end),
  2619. "Split the string @var{s} into a list of substrings, where each\n"
  2620. "substring is a maximal non-empty contiguous sequence of\n"
  2621. "characters from the character set @var{token_set}, which\n"
  2622. "defaults to @code{char-set:graphic}.\n"
  2623. "If @var{start} or @var{end} indices are provided, they restrict\n"
  2624. "@code{string-tokenize} to operating on the indicated substring\n"
  2625. "of @var{s}.")
  2626. #define FUNC_NAME s_scm_string_tokenize
  2627. {
  2628. size_t cstart, cend;
  2629. SCM result = SCM_EOL;
  2630. MY_VALIDATE_SUBSTRING_SPEC (1, s,
  2631. 3, start, cstart,
  2632. 4, end, cend);
  2633. if (SCM_UNBNDP (token_set))
  2634. token_set = scm_char_set_graphic;
  2635. if (SCM_CHARSETP (token_set))
  2636. {
  2637. size_t idx;
  2638. while (cstart < cend)
  2639. {
  2640. while (cstart < cend)
  2641. {
  2642. if (REF_IN_CHARSET (s, cend-1, token_set))
  2643. break;
  2644. cend--;
  2645. }
  2646. if (cstart >= cend)
  2647. break;
  2648. idx = cend;
  2649. while (cstart < cend)
  2650. {
  2651. if (!REF_IN_CHARSET (s, cend-1, token_set))
  2652. break;
  2653. cend--;
  2654. }
  2655. result = scm_cons (scm_i_substring (s, cend, idx), result);
  2656. }
  2657. }
  2658. else
  2659. SCM_WRONG_TYPE_ARG (2, token_set);
  2660. scm_remember_upto_here_1 (s);
  2661. return result;
  2662. }
  2663. #undef FUNC_NAME
  2664. SCM_DEFINE (scm_string_split, "string-split", 2, 0, 0,
  2665. (SCM str, SCM chr),
  2666. "Split the string @var{str} into a list of the substrings delimited\n"
  2667. "by appearances of the character @var{chr}. Note that an empty substring\n"
  2668. "between separator characters will result in an empty string in the\n"
  2669. "result list.\n"
  2670. "\n"
  2671. "@lisp\n"
  2672. "(string-split \"root:x:0:0:root:/root:/bin/bash\" #\\:)\n"
  2673. "@result{}\n"
  2674. "(\"root\" \"x\" \"0\" \"0\" \"root\" \"/root\" \"/bin/bash\")\n"
  2675. "\n"
  2676. "(string-split \"::\" #\\:)\n"
  2677. "@result{}\n"
  2678. "(\"\" \"\" \"\")\n"
  2679. "\n"
  2680. "(string-split \"\" #\\:)\n"
  2681. "@result{}\n"
  2682. "(\"\")\n"
  2683. "@end lisp")
  2684. #define FUNC_NAME s_scm_string_split
  2685. {
  2686. long idx, last_idx;
  2687. int narrow;
  2688. SCM res = SCM_EOL;
  2689. SCM_VALIDATE_STRING (1, str);
  2690. SCM_VALIDATE_CHAR (2, chr);
  2691. /* This is explicit wide/narrow logic (instead of using
  2692. scm_i_string_ref) is a speed optimization. */
  2693. idx = scm_i_string_length (str);
  2694. narrow = scm_i_is_narrow_string (str);
  2695. if (narrow)
  2696. {
  2697. const char *buf = scm_i_string_chars (str);
  2698. while (idx >= 0)
  2699. {
  2700. last_idx = idx;
  2701. while (idx > 0 && buf[idx-1] != (char) SCM_CHAR(chr))
  2702. idx--;
  2703. if (idx >= 0)
  2704. {
  2705. res = scm_cons (scm_i_substring (str, idx, last_idx), res);
  2706. idx--;
  2707. }
  2708. }
  2709. }
  2710. else
  2711. {
  2712. const scm_t_wchar *buf = scm_i_string_wide_chars (str);
  2713. while (idx >= 0)
  2714. {
  2715. last_idx = idx;
  2716. while (idx > 0 && buf[idx-1] != SCM_CHAR(chr))
  2717. idx--;
  2718. if (idx >= 0)
  2719. {
  2720. res = scm_cons (scm_i_substring (str, idx, last_idx), res);
  2721. idx--;
  2722. }
  2723. }
  2724. }
  2725. scm_remember_upto_here_1 (str);
  2726. return res;
  2727. }
  2728. #undef FUNC_NAME
  2729. SCM_DEFINE (scm_string_filter, "string-filter", 2, 2, 0,
  2730. (SCM char_pred, SCM s, SCM start, SCM end),
  2731. "Filter the string @var{s}, retaining only those characters\n"
  2732. "which satisfy @var{char_pred}.\n"
  2733. "\n"
  2734. "If @var{char_pred} is a procedure, it is applied to each\n"
  2735. "character as a predicate, if it is a character, it is tested\n"
  2736. "for equality and if it is a character set, it is tested for\n"
  2737. "membership.")
  2738. #define FUNC_NAME s_scm_string_filter
  2739. {
  2740. size_t cstart, cend;
  2741. SCM result;
  2742. size_t idx;
  2743. #if SCM_ENABLE_DEPRECATED == 1
  2744. if (scm_is_string (char_pred))
  2745. {
  2746. SCM tmp;
  2747. scm_c_issue_deprecation_warning
  2748. ("Guile used to use the wrong argument order for string-filter.\n"
  2749. "This call to string-filter had the arguments in the wrong order.\n"
  2750. "See SRFI-13 for more details. At some point we will remove this hack.");
  2751. tmp = char_pred;
  2752. char_pred = s;
  2753. s = tmp;
  2754. }
  2755. #endif
  2756. MY_VALIDATE_SUBSTRING_SPEC (2, s,
  2757. 3, start, cstart,
  2758. 4, end, cend);
  2759. /* The explicit loops below stripping leading and trailing non-matches
  2760. mean we can return a substring if those are the only deletions, making
  2761. string-filter as efficient as string-trim-both in that case. */
  2762. if (SCM_CHARP (char_pred))
  2763. {
  2764. size_t count;
  2765. /* strip leading non-matches by incrementing cstart */
  2766. while (cstart < cend && scm_i_string_ref (s, cstart) != SCM_CHAR (char_pred))
  2767. cstart++;
  2768. /* strip trailing non-matches by decrementing cend */
  2769. while (cend > cstart && scm_i_string_ref (s, cend-1) != SCM_CHAR (char_pred))
  2770. cend--;
  2771. /* count chars to keep */
  2772. count = 0;
  2773. for (idx = cstart; idx < cend; idx++)
  2774. if (scm_i_string_ref (s, idx) == SCM_CHAR (char_pred))
  2775. count++;
  2776. if (count == cend - cstart)
  2777. {
  2778. /* whole of cstart to cend is to be kept, return a copy-on-write
  2779. substring */
  2780. result_substring:
  2781. result = scm_i_substring (s, cstart, cend);
  2782. }
  2783. else
  2784. result = scm_c_make_string (count, char_pred);
  2785. }
  2786. else if (SCM_CHARSETP (char_pred))
  2787. {
  2788. size_t count;
  2789. /* strip leading non-matches by incrementing cstart */
  2790. while (cstart < cend && ! REF_IN_CHARSET (s, cstart, char_pred))
  2791. cstart++;
  2792. /* strip trailing non-matches by decrementing cend */
  2793. while (cend > cstart && ! REF_IN_CHARSET (s, cend-1, char_pred))
  2794. cend--;
  2795. /* count chars to be kept */
  2796. count = 0;
  2797. for (idx = cstart; idx < cend; idx++)
  2798. if (REF_IN_CHARSET (s, idx, char_pred))
  2799. count++;
  2800. /* if whole of start to end kept then return substring */
  2801. if (count == cend - cstart)
  2802. goto result_substring;
  2803. else
  2804. {
  2805. size_t dst = 0;
  2806. result = scm_i_make_string (count, NULL, 0);
  2807. result = scm_i_string_start_writing (result);
  2808. /* decrement "count" in this loop as well as using idx, so that if
  2809. another thread is simultaneously changing "s" there's no chance
  2810. it'll make us copy more than count characters */
  2811. for (idx = cstart; idx < cend && count != 0; idx++)
  2812. {
  2813. if (REF_IN_CHARSET (s, idx, char_pred))
  2814. {
  2815. scm_i_string_set_x (result, dst, scm_i_string_ref (s, idx));
  2816. dst ++;
  2817. count--;
  2818. }
  2819. }
  2820. scm_i_string_stop_writing ();
  2821. }
  2822. }
  2823. else
  2824. {
  2825. SCM ls = SCM_EOL;
  2826. SCM_ASSERT (scm_is_true (scm_procedure_p (char_pred)),
  2827. char_pred, SCM_ARG1, FUNC_NAME);
  2828. idx = cstart;
  2829. while (idx < cend)
  2830. {
  2831. SCM res, ch;
  2832. ch = SCM_MAKE_CHAR (scm_i_string_ref (s, idx));
  2833. res = scm_call_1 (char_pred, ch);
  2834. if (scm_is_true (res))
  2835. ls = scm_cons (ch, ls);
  2836. idx++;
  2837. }
  2838. result = scm_reverse_list_to_string (ls);
  2839. }
  2840. scm_remember_upto_here_1 (s);
  2841. return result;
  2842. }
  2843. #undef FUNC_NAME
  2844. SCM_DEFINE (scm_string_delete, "string-delete", 2, 2, 0,
  2845. (SCM char_pred, SCM s, SCM start, SCM end),
  2846. "Delete characters satisfying @var{char_pred} from @var{s}.\n"
  2847. "\n"
  2848. "If @var{char_pred} is a procedure, it is applied to each\n"
  2849. "character as a predicate, if it is a character, it is tested\n"
  2850. "for equality and if it is a character set, it is tested for\n"
  2851. "membership.")
  2852. #define FUNC_NAME s_scm_string_delete
  2853. {
  2854. size_t cstart, cend;
  2855. SCM result;
  2856. size_t idx;
  2857. #if SCM_ENABLE_DEPRECATED == 1
  2858. if (scm_is_string (char_pred))
  2859. {
  2860. SCM tmp;
  2861. scm_c_issue_deprecation_warning
  2862. ("Guile used to use the wrong argument order for string-delete.\n"
  2863. "This call to string-filter had the arguments in the wrong order.\n"
  2864. "See SRFI-13 for more details. At some point we will remove this hack.");
  2865. tmp = char_pred;
  2866. char_pred = s;
  2867. s = tmp;
  2868. }
  2869. #endif
  2870. MY_VALIDATE_SUBSTRING_SPEC (2, s,
  2871. 3, start, cstart,
  2872. 4, end, cend);
  2873. /* The explicit loops below stripping leading and trailing matches mean we
  2874. can return a substring if those are the only deletions, making
  2875. string-delete as efficient as string-trim-both in that case. */
  2876. if (SCM_CHARP (char_pred))
  2877. {
  2878. size_t count;
  2879. /* strip leading matches by incrementing cstart */
  2880. while (cstart < cend && scm_i_string_ref (s, cstart) == SCM_CHAR(char_pred))
  2881. cstart++;
  2882. /* strip trailing matches by decrementing cend */
  2883. while (cend > cstart && scm_i_string_ref (s, cend-1) == SCM_CHAR (char_pred))
  2884. cend--;
  2885. /* count chars to be kept */
  2886. count = 0;
  2887. for (idx = cstart; idx < cend; idx++)
  2888. if (scm_i_string_ref (s, idx) != SCM_CHAR (char_pred))
  2889. count++;
  2890. if (count == cend - cstart)
  2891. {
  2892. /* whole of cstart to cend is to be kept, return a copy-on-write
  2893. substring */
  2894. result_substring:
  2895. result = scm_i_substring (s, cstart, cend);
  2896. }
  2897. else
  2898. {
  2899. int i = 0;
  2900. /* new string for retained portion */
  2901. result = scm_i_make_string (count, NULL, 0);
  2902. result = scm_i_string_start_writing (result);
  2903. /* decrement "count" in this loop as well as using idx, so that if
  2904. another thread is simultaneously changing "s" there's no chance
  2905. it'll make us copy more than count characters */
  2906. for (idx = cstart; idx < cend && count != 0; idx++)
  2907. {
  2908. scm_t_wchar c = scm_i_string_ref (s, idx);
  2909. if (c != SCM_CHAR (char_pred))
  2910. {
  2911. scm_i_string_set_x (result, i, c);
  2912. i++;
  2913. count--;
  2914. }
  2915. }
  2916. scm_i_string_stop_writing ();
  2917. }
  2918. }
  2919. else if (SCM_CHARSETP (char_pred))
  2920. {
  2921. size_t count;
  2922. /* strip leading matches by incrementing cstart */
  2923. while (cstart < cend && REF_IN_CHARSET (s, cstart, char_pred))
  2924. cstart++;
  2925. /* strip trailing matches by decrementing cend */
  2926. while (cend > cstart && REF_IN_CHARSET (s, cend-1, char_pred))
  2927. cend--;
  2928. /* count chars to be kept */
  2929. count = 0;
  2930. for (idx = cstart; idx < cend; idx++)
  2931. if (!REF_IN_CHARSET (s, idx, char_pred))
  2932. count++;
  2933. if (count == cend - cstart)
  2934. goto result_substring;
  2935. else
  2936. {
  2937. size_t i = 0;
  2938. /* new string for retained portion */
  2939. result = scm_i_make_string (count, NULL, 0);
  2940. result = scm_i_string_start_writing (result);
  2941. /* decrement "count" in this loop as well as using idx, so that if
  2942. another thread is simultaneously changing "s" there's no chance
  2943. it'll make us copy more than count characters */
  2944. for (idx = cstart; idx < cend && count != 0; idx++)
  2945. {
  2946. if (!REF_IN_CHARSET (s, idx, char_pred))
  2947. {
  2948. scm_i_string_set_x (result, i, scm_i_string_ref (s, idx));
  2949. i++;
  2950. count--;
  2951. }
  2952. }
  2953. scm_i_string_stop_writing ();
  2954. }
  2955. }
  2956. else
  2957. {
  2958. SCM ls = SCM_EOL;
  2959. SCM_ASSERT (scm_is_true (scm_procedure_p (char_pred)),
  2960. char_pred, SCM_ARG1, FUNC_NAME);
  2961. idx = cstart;
  2962. while (idx < cend)
  2963. {
  2964. SCM res, ch = SCM_MAKE_CHAR (scm_i_string_ref (s, idx));
  2965. res = scm_call_1 (char_pred, ch);
  2966. if (scm_is_false (res))
  2967. ls = scm_cons (ch, ls);
  2968. idx++;
  2969. }
  2970. result = scm_reverse_list_to_string (ls);
  2971. }
  2972. scm_remember_upto_here_1 (s);
  2973. return result;
  2974. }
  2975. #undef FUNC_NAME
  2976. void
  2977. scm_init_srfi_13 (void)
  2978. {
  2979. #include "libguile/srfi-13.x"
  2980. }
  2981. /* End of srfi-13.c. */