srfi-13.c 92 KB

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