strings.c 66 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502
  1. /* Copyright 1995-1996,1998,2000-2001,2004,2006,2008-2016,2018-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 <alloca.h>
  19. #include <string.h>
  20. #include <stdio.h>
  21. #include <ctype.h>
  22. #include <errno.h>
  23. #include <uninorm.h>
  24. #include <unistr.h>
  25. #include <uniconv.h>
  26. #include <c-strcase.h>
  27. #include <intprops.h>
  28. #include "chars.h"
  29. #include "deprecation.h"
  30. #include "error.h"
  31. #include "generalized-vectors.h"
  32. #include "gsubr.h"
  33. #include "numbers.h"
  34. #include "pairs.h"
  35. #include "ports-internal.h"
  36. #include "ports.h"
  37. #include "private-options.h"
  38. #include "striconveh.h"
  39. #include "symbols.h"
  40. #include "threads.h"
  41. #include "strings.h"
  42. #ifndef SCM_MAX_ALLOCA
  43. # define SCM_MAX_ALLOCA 4096 /* Max bytes per string to allocate via alloca */
  44. #endif
  45. /* {Strings}
  46. */
  47. SCM_SYMBOL (sym_UTF_8, "UTF-8");
  48. SCM_SYMBOL (sym_ISO_8859_1, "ISO-8859-1");
  49. SCM_SYMBOL (sym_error, "error");
  50. /* A stringbuf is a linear buffer of characters. Every string has a
  51. stringbuf. Strings may reference just a slice of a stringbuf; that's
  52. often the case for strings made by the "substring" function.
  53. Stringbufs may hold either 8-bit characters or 32-bit characters. In
  54. either case the characters are Unicode codepoints. "Narrow"
  55. stringbufs thus have the ISO-8859-1 (Latin-1) encoding, and "wide"
  56. stringbufs have the UTF-32 (UCS-4) encoding.
  57. By default, stringbufs are immutable. This enables an O(1)
  58. "substring" operation with no synchronization. A string-set! will
  59. first ensure that the string's stringbuf is mutable, copying the
  60. stringbuf if necessary. This is therefore a copy-on-write
  61. representation. However, taking a substring of a mutable stringbuf
  62. is an O(n) operation as it has to create a new immutable stringbuf.
  63. There are also mutation-sharing substrings as well. */
  64. /* The size in words of the stringbuf header (type tag + size). */
  65. #define STRINGBUF_HEADER_SIZE 2U
  66. #define STRINGBUF_HEADER_BYTES (STRINGBUF_HEADER_SIZE * sizeof (SCM))
  67. #define STRINGBUF_F_WIDE SCM_I_STRINGBUF_F_WIDE
  68. #define STRINGBUF_F_MUTABLE SCM_I_STRINGBUF_F_MUTABLE
  69. #define STRINGBUF_TAG scm_tc7_stringbuf
  70. #define STRINGBUF_WIDE(buf) (SCM_CELL_WORD_0(buf) & STRINGBUF_F_WIDE)
  71. #define STRINGBUF_MUTABLE(buf) (SCM_CELL_WORD_0(buf) & STRINGBUF_F_MUTABLE)
  72. #define STRINGBUF_SET_MUTABLE(buf) \
  73. SCM_SET_CELL_WORD_0 (buf, SCM_CELL_WORD_0 (buf) | STRINGBUF_F_MUTABLE)
  74. #define STRINGBUF_CONTENTS(buf) ((void *) \
  75. SCM_CELL_OBJECT_LOC (buf, \
  76. STRINGBUF_HEADER_SIZE))
  77. #define STRINGBUF_CHARS(buf) ((unsigned char *) STRINGBUF_CONTENTS (buf))
  78. #define STRINGBUF_WIDE_CHARS(buf) ((scm_t_wchar *) STRINGBUF_CONTENTS (buf))
  79. #define STRINGBUF_LENGTH(buf) (SCM_CELL_WORD_1 (buf))
  80. #ifdef SCM_STRING_LENGTH_HISTOGRAM
  81. static size_t lenhist[1001];
  82. #endif
  83. /* Make a stringbuf with space for LEN 8-bit Latin-1-encoded
  84. characters. */
  85. static SCM
  86. make_stringbuf (size_t len)
  87. {
  88. /* XXX - for the benefit of SCM_STRING_CHARS, SCM_SYMBOL_CHARS and
  89. scm_i_symbol_chars, all stringbufs are null-terminated. Once
  90. SCM_STRING_CHARS and SCM_SYMBOL_CHARS are removed and the code
  91. has been changed for scm_i_symbol_chars, this null-termination
  92. can be dropped.
  93. */
  94. SCM buf;
  95. #ifdef SCM_STRING_LENGTH_HISTOGRAM
  96. if (len < 1000)
  97. lenhist[len]++;
  98. else
  99. lenhist[1000]++;
  100. #endif
  101. /* Make sure that the total allocation size will not overflow size_t,
  102. with ~30 extra bytes to spare to avoid an overflow within the
  103. allocator. */
  104. if (INT_ADD_OVERFLOW (len, STRINGBUF_HEADER_BYTES + 32))
  105. scm_num_overflow ("make_stringbuf");
  106. buf = SCM_PACK_POINTER (scm_gc_malloc_pointerless (STRINGBUF_HEADER_BYTES + len + 1,
  107. "string"));
  108. SCM_SET_CELL_TYPE (buf, STRINGBUF_TAG);
  109. SCM_SET_CELL_WORD_1 (buf, (scm_t_bits) len);
  110. STRINGBUF_CHARS (buf)[len] = 0;
  111. return buf;
  112. }
  113. /* Make a stringbuf with space for LEN 32-bit UCS-4-encoded
  114. characters. */
  115. static SCM
  116. make_wide_stringbuf (size_t len)
  117. {
  118. SCM buf;
  119. size_t raw_len;
  120. #ifdef SCM_STRING_LENGTH_HISTOGRAM
  121. if (len < 1000)
  122. lenhist[len]++;
  123. else
  124. lenhist[1000]++;
  125. #endif
  126. /* Make sure that the total allocation size will not overflow size_t,
  127. with ~30 extra bytes to spare to avoid an overflow within the
  128. allocator. */
  129. if (len > (((size_t) -(STRINGBUF_HEADER_BYTES + 32 + sizeof (scm_t_wchar)))
  130. / sizeof (scm_t_wchar)))
  131. scm_num_overflow ("make_wide_stringbuf");
  132. raw_len = (len + 1) * sizeof (scm_t_wchar);
  133. buf = SCM_PACK_POINTER (scm_gc_malloc_pointerless (STRINGBUF_HEADER_BYTES + raw_len,
  134. "string"));
  135. SCM_SET_CELL_TYPE (buf, STRINGBUF_TAG | STRINGBUF_F_WIDE);
  136. SCM_SET_CELL_WORD_1 (buf, (scm_t_bits) len);
  137. STRINGBUF_WIDE_CHARS (buf)[len] = 0;
  138. return buf;
  139. }
  140. /* Return a UCS-4-encoded stringbuf containing the (possibly Latin-1-encoded)
  141. characters from BUF. */
  142. static SCM
  143. wide_stringbuf (SCM buf)
  144. {
  145. SCM new_buf;
  146. if (STRINGBUF_WIDE (buf))
  147. new_buf = buf;
  148. else
  149. {
  150. size_t i, len;
  151. scm_t_wchar *mem;
  152. len = STRINGBUF_LENGTH (buf);
  153. new_buf = make_wide_stringbuf (len);
  154. mem = STRINGBUF_WIDE_CHARS (new_buf);
  155. for (i = 0; i < len; i++)
  156. mem[i] = (scm_t_wchar) STRINGBUF_CHARS (buf)[i];
  157. mem[len] = 0;
  158. }
  159. return new_buf;
  160. }
  161. /* Return a Latin-1-encoded stringbuf containing the (possibly UCS-4-encoded)
  162. characters from BUF, if possible. */
  163. static SCM
  164. narrow_stringbuf (SCM buf)
  165. {
  166. SCM new_buf;
  167. if (!STRINGBUF_WIDE (buf))
  168. new_buf = buf;
  169. else
  170. {
  171. size_t i, len;
  172. scm_t_wchar *wmem;
  173. unsigned char *mem;
  174. len = STRINGBUF_LENGTH (buf);
  175. wmem = STRINGBUF_WIDE_CHARS (buf);
  176. for (i = 0; i < len; i++)
  177. if (wmem[i] > 0xFF)
  178. /* BUF cannot be narrowed. */
  179. return buf;
  180. new_buf = make_stringbuf (len);
  181. mem = STRINGBUF_CHARS (new_buf);
  182. for (i = 0; i < len; i++)
  183. mem[i] = (unsigned char) wmem[i];
  184. mem[len] = 0;
  185. }
  186. return new_buf;
  187. }
  188. /* Copy-on-write strings.
  189. */
  190. #define STRING_TAG scm_tc7_string
  191. #define STRING_STRINGBUF(str) (SCM_CELL_OBJECT_1(str))
  192. #define STRING_START(str) ((size_t)SCM_CELL_WORD_2(str))
  193. #define STRING_LENGTH(str) ((size_t)SCM_CELL_WORD_3(str))
  194. #define SET_STRING_STRINGBUF(str,buf) (SCM_SET_CELL_OBJECT_1(str,buf))
  195. #define SET_STRING_START(str,start) (SCM_SET_CELL_WORD_2(str,start))
  196. #define IS_STRING(str) (SCM_HAS_TYP7 (str, STRING_TAG))
  197. /* Read-only strings.
  198. */
  199. #define RO_STRING_TAG scm_tc7_ro_string
  200. #define IS_RO_STRING(str) (SCM_CELL_TYPE(str)==RO_STRING_TAG)
  201. /* Mutation-sharing substrings
  202. */
  203. #define SH_STRING_TAG (scm_tc7_string + 0x100)
  204. #define SH_STRING_STRING(sh) (SCM_CELL_OBJECT_1(sh))
  205. /* START and LENGTH as for STRINGs. */
  206. #define IS_SH_STRING(str) (SCM_CELL_TYPE(str)==SH_STRING_TAG)
  207. void
  208. scm_i_print_stringbuf (SCM exp, SCM port, scm_print_state *pstate)
  209. {
  210. SCM str = scm_double_cell (STRING_TAG, SCM_UNPACK(exp), 0,
  211. STRINGBUF_LENGTH (exp));
  212. scm_puts ("#<stringbuf ", port);
  213. scm_iprin1 (str, port, pstate);
  214. scm_puts (">", port);
  215. }
  216. SCM scm_nullstr;
  217. static SCM null_stringbuf;
  218. static void
  219. init_null_stringbuf (void)
  220. {
  221. null_stringbuf = make_stringbuf (0);
  222. }
  223. /* Create a scheme string with space for LEN 8-bit Latin-1-encoded
  224. characters. CHARSP, if not NULL, will be set to location of the
  225. char array. If READ_ONLY_P, the returned string is read-only;
  226. otherwise it is writable. */
  227. SCM
  228. scm_i_make_string (size_t len, char **charsp, int read_only_p)
  229. {
  230. SCM buf;
  231. SCM res;
  232. if (len == 0)
  233. {
  234. static scm_i_pthread_once_t once = SCM_I_PTHREAD_ONCE_INIT;
  235. scm_i_pthread_once (&once, init_null_stringbuf);
  236. buf = null_stringbuf;
  237. }
  238. else
  239. buf = make_stringbuf (len);
  240. if (charsp)
  241. *charsp = (char *) STRINGBUF_CHARS (buf);
  242. res = scm_double_cell (read_only_p ? RO_STRING_TAG : STRING_TAG,
  243. SCM_UNPACK (buf),
  244. (scm_t_bits) 0, (scm_t_bits) len);
  245. return res;
  246. }
  247. /* Create a scheme string with space for LEN 32-bit UCS-4-encoded
  248. characters. CHARSP, if not NULL, will be set to location of the
  249. character array. If READ_ONLY_P, the returned string is read-only;
  250. otherwise it is writable. */
  251. SCM
  252. scm_i_make_wide_string (size_t len, scm_t_wchar **charsp, int read_only_p)
  253. {
  254. SCM buf = make_wide_stringbuf (len);
  255. SCM res;
  256. if (charsp)
  257. *charsp = STRINGBUF_WIDE_CHARS (buf);
  258. res = scm_double_cell (read_only_p ? RO_STRING_TAG : STRING_TAG,
  259. SCM_UNPACK (buf),
  260. (scm_t_bits) 0, (scm_t_bits) len);
  261. return res;
  262. }
  263. static void
  264. validate_substring_args (SCM str, size_t start, size_t end)
  265. {
  266. if (!IS_STRING (str))
  267. scm_wrong_type_arg_msg (NULL, 0, str, "string");
  268. if (start > STRING_LENGTH (str))
  269. scm_out_of_range (NULL, scm_from_size_t (start));
  270. if (end > STRING_LENGTH (str) || end < start)
  271. scm_out_of_range (NULL, scm_from_size_t (end));
  272. }
  273. static inline void
  274. get_str_buf_start (SCM *str, SCM *buf, size_t *start)
  275. {
  276. *start = STRING_START (*str);
  277. if (IS_SH_STRING (*str))
  278. {
  279. *str = SH_STRING_STRING (*str);
  280. *start += STRING_START (*str);
  281. }
  282. *buf = STRING_STRINGBUF (*str);
  283. }
  284. static SCM
  285. substring_with_immutable_stringbuf (SCM str, size_t start, size_t end,
  286. int force_copy_p, int read_only_p)
  287. {
  288. SCM buf;
  289. size_t str_start, len;
  290. scm_t_bits tag = read_only_p ? RO_STRING_TAG : STRING_TAG;
  291. get_str_buf_start (&str, &buf, &str_start);
  292. len = end - start;
  293. start += str_start;
  294. if (len == 0)
  295. return scm_i_make_string (0, NULL, read_only_p);
  296. else if (!force_copy_p && SCM_LIKELY (!STRINGBUF_MUTABLE (buf)))
  297. return scm_double_cell (tag, SCM_UNPACK (buf), start, len);
  298. else
  299. {
  300. SCM new_buf, new_str;
  301. if (STRINGBUF_WIDE (buf))
  302. {
  303. new_buf = make_wide_stringbuf (len);
  304. u32_cpy ((uint32_t *) STRINGBUF_WIDE_CHARS (new_buf),
  305. (uint32_t *) (STRINGBUF_WIDE_CHARS (buf) + start), len);
  306. new_str = scm_double_cell (tag, SCM_UNPACK (new_buf), 0, len);
  307. scm_i_try_narrow_string (new_str);
  308. }
  309. else
  310. {
  311. new_buf = make_stringbuf (len);
  312. memcpy (STRINGBUF_CHARS (new_buf),
  313. STRINGBUF_CHARS (buf) + start, len);
  314. new_str = scm_double_cell (tag, SCM_UNPACK (new_buf), 0, len);
  315. }
  316. return new_str;
  317. }
  318. }
  319. SCM
  320. scm_i_substring (SCM str, size_t start, size_t end)
  321. {
  322. return substring_with_immutable_stringbuf (str, start, end, 0, 0);
  323. }
  324. SCM
  325. scm_i_substring_read_only (SCM str, size_t start, size_t end)
  326. {
  327. return substring_with_immutable_stringbuf (str, start, end, 0, 1);
  328. }
  329. SCM
  330. scm_i_substring_copy (SCM str, size_t start, size_t end)
  331. {
  332. return substring_with_immutable_stringbuf (str, start, end, 1, 0);
  333. }
  334. static void
  335. scm_i_string_ensure_mutable_x (SCM str)
  336. {
  337. SCM buf;
  338. if (IS_SH_STRING (str))
  339. {
  340. /* Shared-mutation strings always have mutable stringbufs. */
  341. buf = STRING_STRINGBUF (SH_STRING_STRING (str));
  342. if (!STRINGBUF_MUTABLE (buf))
  343. abort ();
  344. return;
  345. }
  346. if (IS_RO_STRING (str))
  347. scm_misc_error (NULL, "string is read-only: ~s", scm_list_1 (str));
  348. buf = STRING_STRINGBUF (str);
  349. if (STRINGBUF_MUTABLE (buf))
  350. return;
  351. /* Otherwise copy and mark the fresh stringbuf as mutable. Note that
  352. we copy the whole stringbuf so that the start/len offsets from the
  353. original string keep working, so that concurrent accessors on this
  354. string don't see things in an inconsistent state. */
  355. {
  356. SCM new_buf;
  357. size_t len = STRINGBUF_LENGTH (buf);
  358. if (STRINGBUF_WIDE (buf))
  359. {
  360. new_buf = make_wide_stringbuf (len);
  361. u32_cpy ((uint32_t *) STRINGBUF_WIDE_CHARS (new_buf),
  362. (uint32_t *) STRINGBUF_WIDE_CHARS (buf), len);
  363. }
  364. else
  365. {
  366. new_buf = make_stringbuf (len);
  367. memcpy (STRINGBUF_CHARS (new_buf), STRINGBUF_CHARS (buf), len);
  368. }
  369. STRINGBUF_SET_MUTABLE (new_buf);
  370. SET_STRING_STRINGBUF (str, new_buf);
  371. }
  372. }
  373. SCM
  374. scm_i_substring_shared (SCM str, size_t start, size_t end)
  375. {
  376. if (start == 0 && end == STRING_LENGTH (str))
  377. return str;
  378. else if (start == end)
  379. return scm_i_make_string (0, NULL, 0);
  380. else if (IS_RO_STRING (str))
  381. return scm_i_substring_read_only (str, start, end);
  382. else
  383. {
  384. size_t len = end - start;
  385. if (IS_SH_STRING (str))
  386. {
  387. start += STRING_START (str);
  388. str = SH_STRING_STRING (str);
  389. }
  390. scm_i_string_ensure_mutable_x (str);
  391. return scm_double_cell (SH_STRING_TAG, SCM_UNPACK(str),
  392. (scm_t_bits)start, (scm_t_bits) len);
  393. }
  394. }
  395. SCM
  396. scm_c_substring (SCM str, size_t start, size_t end)
  397. {
  398. validate_substring_args (str, start, end);
  399. return scm_i_substring (str, start, end);
  400. }
  401. SCM
  402. scm_c_substring_read_only (SCM str, size_t start, size_t end)
  403. {
  404. validate_substring_args (str, start, end);
  405. return scm_i_substring_read_only (str, start, end);
  406. }
  407. SCM
  408. scm_c_substring_copy (SCM str, size_t start, size_t end)
  409. {
  410. validate_substring_args (str, start, end);
  411. return scm_i_substring_copy (str, start, end);
  412. }
  413. SCM
  414. scm_c_substring_shared (SCM str, size_t start, size_t end)
  415. {
  416. validate_substring_args (str, start, end);
  417. return scm_i_substring_shared (str, start, end);
  418. }
  419. /* Internal accessors
  420. */
  421. /* Returns the number of characters in STR. This may be different
  422. than the memory size of the string storage. */
  423. size_t
  424. scm_i_string_length (SCM str)
  425. {
  426. return STRING_LENGTH (str);
  427. }
  428. int
  429. scm_i_string_is_mutable (SCM str)
  430. {
  431. return !IS_RO_STRING (str);
  432. }
  433. /* True if the string is 'narrow', meaning it has a 8-bit Latin-1
  434. encoding. False if it is 'wide', having a 32-bit UCS-4
  435. encoding. */
  436. int
  437. scm_i_is_narrow_string (SCM str)
  438. {
  439. if (IS_SH_STRING (str))
  440. str = SH_STRING_STRING (str);
  441. return !STRINGBUF_WIDE (STRING_STRINGBUF (str));
  442. }
  443. /* Try to coerce a string to be narrow. It if is narrow already, do
  444. nothing. If it is wide, shrink it to narrow if none of its
  445. characters are above 0xFF. Return true if the string is narrow or
  446. was made to be narrow. */
  447. int
  448. scm_i_try_narrow_string (SCM str)
  449. {
  450. if (IS_SH_STRING (str))
  451. str = SH_STRING_STRING (str);
  452. SET_STRING_STRINGBUF (str, narrow_stringbuf (STRING_STRINGBUF (str)));
  453. return scm_i_is_narrow_string (str);
  454. }
  455. /* Return a pointer to the raw data of the string, which can be either Latin-1
  456. or UCS-4 encoded data, depending on `scm_i_is_narrow_string (STR)'. */
  457. const void *
  458. scm_i_string_data (SCM str)
  459. {
  460. SCM buf;
  461. size_t start;
  462. const char *data;
  463. get_str_buf_start (&str, &buf, &start);
  464. data = STRINGBUF_CONTENTS (buf);
  465. data += start * (scm_i_is_narrow_string (str) ? 1 : 4);
  466. return data;
  467. }
  468. /* Returns a pointer to the 8-bit Latin-1 encoded character array of
  469. STR. */
  470. const char *
  471. scm_i_string_chars (SCM str)
  472. {
  473. SCM buf;
  474. size_t start;
  475. get_str_buf_start (&str, &buf, &start);
  476. if (scm_i_is_narrow_string (str))
  477. return (const char *) STRINGBUF_CHARS (buf) + start;
  478. else
  479. scm_misc_error (NULL, "Invalid read access of chars of wide string: ~s",
  480. scm_list_1 (str));
  481. return NULL;
  482. }
  483. /* Returns a pointer to the 32-bit UCS-4 encoded character array of
  484. STR. */
  485. const scm_t_wchar *
  486. scm_i_string_wide_chars (SCM str)
  487. {
  488. SCM buf;
  489. size_t start;
  490. get_str_buf_start (&str, &buf, &start);
  491. if (!scm_i_is_narrow_string (str))
  492. return (const scm_t_wchar *) STRINGBUF_WIDE_CHARS (buf) + start;
  493. else
  494. scm_misc_error (NULL, "Invalid read access of chars of narrow string: ~s",
  495. scm_list_1 (str));
  496. }
  497. /* If the buffer in ORIG_STR is immutable, copy ORIG_STR's characters to
  498. a new string buffer, so that it can be modified without modifying
  499. other strings. */
  500. SCM
  501. scm_i_string_start_writing (SCM orig_str)
  502. {
  503. scm_i_string_ensure_mutable_x (orig_str);
  504. return orig_str;
  505. }
  506. /* Return a pointer to the 8-bit Latin-1 chars of a string. */
  507. char *
  508. scm_i_string_writable_chars (SCM str)
  509. {
  510. SCM buf;
  511. size_t start;
  512. get_str_buf_start (&str, &buf, &start);
  513. if (scm_i_is_narrow_string (str))
  514. return (char *) STRINGBUF_CHARS (buf) + start;
  515. else
  516. scm_misc_error (NULL, "Invalid write access of chars of wide string: ~s",
  517. scm_list_1 (str));
  518. return NULL;
  519. }
  520. /* Return a pointer to the UCS-4 codepoints of a string. */
  521. static scm_t_wchar *
  522. scm_i_string_writable_wide_chars (SCM str)
  523. {
  524. SCM buf;
  525. size_t start;
  526. get_str_buf_start (&str, &buf, &start);
  527. if (!scm_i_is_narrow_string (str))
  528. return STRINGBUF_WIDE_CHARS (buf) + start;
  529. else
  530. scm_misc_error (NULL, "Invalid write access of chars of narrow string: ~s",
  531. scm_list_1 (str));
  532. }
  533. /* Unlock the string mutex that was locked when
  534. scm_i_string_start_writing was called. */
  535. void
  536. scm_i_string_stop_writing (void)
  537. {
  538. }
  539. /* Return the Xth character of STR as a UCS-4 codepoint. */
  540. scm_t_wchar
  541. scm_i_string_ref (SCM str, size_t x)
  542. {
  543. if (scm_i_is_narrow_string (str))
  544. return (scm_t_wchar) (unsigned char) (scm_i_string_chars (str)[x]);
  545. else
  546. return scm_i_string_wide_chars (str)[x];
  547. }
  548. /* Returns index+1 of the first char in STR that matches C, or
  549. 0 if the char is not found. */
  550. int
  551. scm_i_string_contains_char (SCM str, char ch)
  552. {
  553. size_t i;
  554. size_t len = scm_i_string_length (str);
  555. i = 0;
  556. if (scm_i_is_narrow_string (str))
  557. {
  558. while (i < len)
  559. {
  560. if (scm_i_string_chars (str)[i] == ch)
  561. return i+1;
  562. i++;
  563. }
  564. }
  565. else
  566. {
  567. while (i < len)
  568. {
  569. if (scm_i_string_wide_chars (str)[i]
  570. == (unsigned char) ch)
  571. return i+1;
  572. i++;
  573. }
  574. }
  575. return 0;
  576. }
  577. int
  578. scm_i_string_strcmp (SCM sstr, size_t start_x, const char *cstr)
  579. {
  580. if (scm_i_is_narrow_string (sstr))
  581. {
  582. const char *a = scm_i_string_chars (sstr) + start_x;
  583. const char *b = cstr;
  584. return strncmp (a, b, strlen(b));
  585. }
  586. else
  587. {
  588. size_t i;
  589. const scm_t_wchar *a = scm_i_string_wide_chars (sstr) + start_x;
  590. const char *b = cstr;
  591. for (i = 0; i < strlen (b); i++)
  592. {
  593. if (a[i] != (unsigned char) b[i])
  594. return 1;
  595. }
  596. }
  597. return 0;
  598. }
  599. /* Set the Pth character of STR to UCS-4 codepoint CHR. */
  600. void
  601. scm_i_string_set_x (SCM str, size_t p, scm_t_wchar chr)
  602. {
  603. if (IS_SH_STRING (str))
  604. {
  605. p += STRING_START (str);
  606. str = SH_STRING_STRING (str);
  607. }
  608. if (chr > 0xFF && scm_i_is_narrow_string (str))
  609. SET_STRING_STRINGBUF (str, wide_stringbuf (STRING_STRINGBUF (str)));
  610. if (scm_i_is_narrow_string (str))
  611. {
  612. char *dst = scm_i_string_writable_chars (str);
  613. dst[p] = chr;
  614. }
  615. else
  616. {
  617. scm_t_wchar *dst = scm_i_string_writable_wide_chars (str);
  618. dst[p] = chr;
  619. }
  620. }
  621. /* Symbols.
  622. Basic symbol creation and accessing is done here, the rest is in
  623. symbols.[hc]. This has been done to keep stringbufs and the
  624. internals of strings and string-like objects confined to this file.
  625. */
  626. #define SYMBOL_STRINGBUF SCM_CELL_OBJECT_1
  627. SCM
  628. scm_i_make_symbol (SCM name, scm_t_bits flags, unsigned long hash)
  629. {
  630. SCM buf, symbol;
  631. size_t start, length = STRING_LENGTH (name);
  632. get_str_buf_start (&name, &buf, &start);
  633. if (SCM_UNLIKELY (STRINGBUF_MUTABLE (buf) || start != 0))
  634. {
  635. name = scm_i_substring_copy (name, 0, length);
  636. buf = STRING_STRINGBUF (name);
  637. }
  638. symbol = scm_words (scm_tc7_symbol | flags, 3);
  639. SCM_SET_CELL_WORD_1 (symbol, SCM_UNPACK (buf));
  640. SCM_SET_CELL_WORD_2 (symbol, hash);
  641. return symbol;
  642. }
  643. /* Returns the number of characters in SYM. This may be different
  644. from the memory size of SYM. */
  645. size_t
  646. scm_i_symbol_length (SCM sym)
  647. {
  648. return STRINGBUF_LENGTH (SYMBOL_STRINGBUF (sym));
  649. }
  650. size_t
  651. scm_c_symbol_length (SCM sym)
  652. #define FUNC_NAME "scm_c_symbol_length"
  653. {
  654. SCM_VALIDATE_SYMBOL (1, sym);
  655. return STRINGBUF_LENGTH (SYMBOL_STRINGBUF (sym));
  656. }
  657. #undef FUNC_NAME
  658. /* True if the name of SYM is stored as a Latin-1 encoded string.
  659. False if it is stored as a 32-bit UCS-4-encoded string. */
  660. int
  661. scm_i_is_narrow_symbol (SCM sym)
  662. {
  663. SCM buf;
  664. buf = SYMBOL_STRINGBUF (sym);
  665. return !STRINGBUF_WIDE (buf);
  666. }
  667. /* Returns a pointer to the 8-bit Latin-1 encoded character array that
  668. contains the name of SYM. */
  669. const char *
  670. scm_i_symbol_chars (SCM sym)
  671. {
  672. SCM buf;
  673. buf = SYMBOL_STRINGBUF (sym);
  674. if (!STRINGBUF_WIDE (buf))
  675. return (const char *) STRINGBUF_CHARS (buf);
  676. else
  677. scm_misc_error (NULL, "Invalid access of chars of a wide symbol ~S",
  678. scm_list_1 (sym));
  679. }
  680. /* Return a pointer to the 32-bit UCS-4-encoded character array of a
  681. symbol's name. */
  682. const scm_t_wchar *
  683. scm_i_symbol_wide_chars (SCM sym)
  684. {
  685. SCM buf;
  686. buf = SYMBOL_STRINGBUF (sym);
  687. if (STRINGBUF_WIDE (buf))
  688. return (const scm_t_wchar *) STRINGBUF_WIDE_CHARS (buf);
  689. else
  690. scm_misc_error (NULL, "Invalid access of chars of a narrow symbol ~S",
  691. scm_list_1 (sym));
  692. }
  693. SCM
  694. scm_i_symbol_substring (SCM sym, size_t start, size_t end)
  695. {
  696. SCM buf = SYMBOL_STRINGBUF (sym);
  697. return scm_double_cell (RO_STRING_TAG, SCM_UNPACK (buf),
  698. (scm_t_bits)start, (scm_t_bits) end - start);
  699. }
  700. /* Returns the Xth character of symbol SYM as a UCS-4 codepoint. */
  701. scm_t_wchar
  702. scm_i_symbol_ref (SCM sym, size_t x)
  703. {
  704. if (scm_i_is_narrow_symbol (sym))
  705. return (scm_t_wchar) (unsigned char) (scm_i_symbol_chars (sym)[x]);
  706. else
  707. return scm_i_symbol_wide_chars (sym)[x];
  708. }
  709. /* Debugging
  710. */
  711. SCM_DEFINE (scm_sys_string_dump, "%string-dump", 1, 0, 0, (SCM str),
  712. "Returns an association list containing debugging information\n"
  713. "for @var{str}. The association list has the following entries."
  714. "@table @code\n"
  715. "@item string\n"
  716. "The string itself.\n"
  717. "@item start\n"
  718. "The start index of the string into its stringbuf\n"
  719. "@item length\n"
  720. "The length of the string\n"
  721. "@item shared\n"
  722. "If this string is a substring, it returns its parent string.\n"
  723. "Otherwise, it returns @code{#f}\n"
  724. "@item read-only\n"
  725. "@code{#t} if the string is read-only\n"
  726. "@item stringbuf-chars\n"
  727. "A new string containing this string's stringbuf's characters\n"
  728. "@item stringbuf-length\n"
  729. "The number of characters in this stringbuf\n"
  730. "@item stringbuf-mutable\n"
  731. "@code{#t} if this stringbuf is mutable\n"
  732. "@item stringbuf-wide\n"
  733. "@code{#t} if this stringbuf's characters are stored in a\n"
  734. "32-bit buffer, or @code{#f} if they are stored in an 8-bit\n"
  735. "buffer\n"
  736. "@end table")
  737. #define FUNC_NAME s_scm_sys_string_dump
  738. {
  739. SCM e1, e2, e3, e4, e5, e6, e7, e8, e9;
  740. SCM buf;
  741. SCM_VALIDATE_STRING (1, str);
  742. /* String info */
  743. e1 = scm_cons (scm_from_latin1_symbol ("string"),
  744. str);
  745. e2 = scm_cons (scm_from_latin1_symbol ("start"),
  746. scm_from_size_t (STRING_START (str)));
  747. e3 = scm_cons (scm_from_latin1_symbol ("length"),
  748. scm_from_size_t (STRING_LENGTH (str)));
  749. if (IS_SH_STRING (str))
  750. {
  751. e4 = scm_cons (scm_from_latin1_symbol ("shared"),
  752. SH_STRING_STRING (str));
  753. buf = STRING_STRINGBUF (SH_STRING_STRING (str));
  754. }
  755. else
  756. {
  757. e4 = scm_cons (scm_from_latin1_symbol ("shared"),
  758. SCM_BOOL_F);
  759. buf = STRING_STRINGBUF (str);
  760. }
  761. if (IS_RO_STRING (str))
  762. e5 = scm_cons (scm_from_latin1_symbol ("read-only"),
  763. SCM_BOOL_T);
  764. else
  765. e5 = scm_cons (scm_from_latin1_symbol ("read-only"),
  766. SCM_BOOL_F);
  767. /* Stringbuf info */
  768. if (!STRINGBUF_WIDE (buf))
  769. {
  770. size_t len = STRINGBUF_LENGTH (buf);
  771. char *cbuf;
  772. SCM sbc = scm_i_make_string (len, &cbuf, 0);
  773. memcpy (cbuf, STRINGBUF_CHARS (buf), len);
  774. e6 = scm_cons (scm_from_latin1_symbol ("stringbuf-chars"),
  775. sbc);
  776. }
  777. else
  778. {
  779. size_t len = STRINGBUF_LENGTH (buf);
  780. scm_t_wchar *cbuf;
  781. SCM sbc = scm_i_make_wide_string (len, &cbuf, 0);
  782. u32_cpy ((uint32_t *) cbuf,
  783. (uint32_t *) STRINGBUF_WIDE_CHARS (buf), len);
  784. e6 = scm_cons (scm_from_latin1_symbol ("stringbuf-chars"),
  785. sbc);
  786. }
  787. e7 = scm_cons (scm_from_latin1_symbol ("stringbuf-length"),
  788. scm_from_size_t (STRINGBUF_LENGTH (buf)));
  789. if (STRINGBUF_MUTABLE (buf))
  790. e8 = scm_cons (scm_from_latin1_symbol ("stringbuf-mutable"),
  791. SCM_BOOL_T);
  792. else
  793. e8 = scm_cons (scm_from_latin1_symbol ("stringbuf-mutable"),
  794. SCM_BOOL_F);
  795. if (STRINGBUF_WIDE (buf))
  796. e9 = scm_cons (scm_from_latin1_symbol ("stringbuf-wide"),
  797. SCM_BOOL_T);
  798. else
  799. e9 = scm_cons (scm_from_latin1_symbol ("stringbuf-wide"),
  800. SCM_BOOL_F);
  801. return scm_list_n (e1, e2, e3, e4, e5, e6, e7, e8, e9, SCM_UNDEFINED);
  802. }
  803. #undef FUNC_NAME
  804. SCM_DEFINE (scm_sys_symbol_dump, "%symbol-dump", 1, 0, 0, (SCM sym),
  805. "Returns an association list containing debugging information\n"
  806. "for @var{sym}. The association list has the following entries."
  807. "@table @code\n"
  808. "@item symbol\n"
  809. "The symbol itself\n"
  810. "@item hash\n"
  811. "Its hash value\n"
  812. "@item interned\n"
  813. "@code{#t} if it is an interned symbol\n"
  814. "@item stringbuf-chars\n"
  815. "A new string containing this symbols's stringbuf's characters\n"
  816. "@item stringbuf-length\n"
  817. "The number of characters in this stringbuf\n"
  818. "@item stringbuf-mutable\n"
  819. "@code{#t} if this stringbuf is mutable\n"
  820. "@item stringbuf-wide\n"
  821. "@code{#t} if this stringbuf's characters are stored in a\n"
  822. "32-bit buffer, or @code{#f} if they are stored in an 8-bit\n"
  823. "buffer\n"
  824. "@end table")
  825. #define FUNC_NAME s_scm_sys_symbol_dump
  826. {
  827. SCM e1, e2, e3, e4, e5, e6, e7;
  828. SCM buf;
  829. SCM_VALIDATE_SYMBOL (1, sym);
  830. e1 = scm_cons (scm_from_latin1_symbol ("symbol"),
  831. sym);
  832. e2 = scm_cons (scm_from_latin1_symbol ("hash"),
  833. scm_from_ulong (scm_i_symbol_hash (sym)));
  834. e3 = scm_cons (scm_from_latin1_symbol ("interned"),
  835. scm_symbol_interned_p (sym));
  836. buf = SYMBOL_STRINGBUF (sym);
  837. /* Stringbuf info */
  838. if (!STRINGBUF_WIDE (buf))
  839. {
  840. size_t len = STRINGBUF_LENGTH (buf);
  841. char *cbuf;
  842. SCM sbc = scm_i_make_string (len, &cbuf, 0);
  843. memcpy (cbuf, STRINGBUF_CHARS (buf), len);
  844. e4 = scm_cons (scm_from_latin1_symbol ("stringbuf-chars"),
  845. sbc);
  846. }
  847. else
  848. {
  849. size_t len = STRINGBUF_LENGTH (buf);
  850. scm_t_wchar *cbuf;
  851. SCM sbc = scm_i_make_wide_string (len, &cbuf, 0);
  852. u32_cpy ((uint32_t *) cbuf,
  853. (uint32_t *) STRINGBUF_WIDE_CHARS (buf), len);
  854. e4 = scm_cons (scm_from_latin1_symbol ("stringbuf-chars"),
  855. sbc);
  856. }
  857. e5 = scm_cons (scm_from_latin1_symbol ("stringbuf-length"),
  858. scm_from_size_t (STRINGBUF_LENGTH (buf)));
  859. if (STRINGBUF_MUTABLE (buf))
  860. e6 = scm_cons (scm_from_latin1_symbol ("stringbuf-mutable"),
  861. SCM_BOOL_T);
  862. else
  863. e6 = scm_cons (scm_from_latin1_symbol ("stringbuf-mutable"),
  864. SCM_BOOL_F);
  865. if (STRINGBUF_WIDE (buf))
  866. e7 = scm_cons (scm_from_latin1_symbol ("stringbuf-wide"),
  867. SCM_BOOL_T);
  868. else
  869. e7 = scm_cons (scm_from_latin1_symbol ("stringbuf-wide"),
  870. SCM_BOOL_F);
  871. return scm_list_n (e1, e2, e3, e4, e5, e6, e7, SCM_UNDEFINED);
  872. }
  873. #undef FUNC_NAME
  874. #ifdef SCM_STRING_LENGTH_HISTOGRAM
  875. SCM_DEFINE (scm_sys_stringbuf_hist, "%stringbuf-hist", 0, 0, 0, (void), "")
  876. #define FUNC_NAME s_scm_sys_stringbuf_hist
  877. {
  878. int i;
  879. for (i = 0; i < 1000; i++)
  880. if (lenhist[i])
  881. fprintf (stderr, " %3d: %u\n", i, lenhist[i]);
  882. fprintf (stderr, ">999: %u\n", lenhist[1000]);
  883. return SCM_UNSPECIFIED;
  884. }
  885. #undef FUNC_NAME
  886. #endif
  887. SCM_DEFINE (scm_string_p, "string?", 1, 0, 0,
  888. (SCM obj),
  889. "Return @code{#t} if @var{obj} is a string, else @code{#f}.")
  890. #define FUNC_NAME s_scm_string_p
  891. {
  892. return scm_from_bool (IS_STRING (obj));
  893. }
  894. #undef FUNC_NAME
  895. SCM_REGISTER_PROC (s_scm_list_to_string, "list->string", 1, 0, 0, scm_string);
  896. SCM_DEFINE (scm_string, "string", 0, 0, 1,
  897. (SCM chrs),
  898. "@deffnx {Scheme Procedure} list->string chrs\n"
  899. "Return a newly allocated string composed of the arguments,\n"
  900. "@var{chrs}.")
  901. #define FUNC_NAME s_scm_string
  902. {
  903. SCM result = SCM_BOOL_F;
  904. SCM rest;
  905. size_t len;
  906. size_t p = 0;
  907. long i;
  908. int wide = 0;
  909. /* Verify that this is a list of chars. */
  910. i = scm_ilength (chrs);
  911. SCM_ASSERT (i >= 0, chrs, SCM_ARG1, FUNC_NAME);
  912. len = (size_t) i;
  913. rest = chrs;
  914. while (len > 0 && scm_is_pair (rest))
  915. {
  916. SCM elt = SCM_CAR (rest);
  917. SCM_VALIDATE_CHAR (SCM_ARGn, elt);
  918. if (SCM_CHAR (elt) > 0xFF)
  919. wide = 1;
  920. rest = SCM_CDR (rest);
  921. len--;
  922. scm_remember_upto_here_1 (elt);
  923. }
  924. /* Construct a string containing this list of chars. */
  925. len = (size_t) i;
  926. rest = chrs;
  927. if (wide == 0)
  928. {
  929. char *buf;
  930. result = scm_i_make_string (len, NULL, 0);
  931. buf = scm_i_string_writable_chars (result);
  932. while (len > 0 && scm_is_pair (rest))
  933. {
  934. SCM elt = SCM_CAR (rest);
  935. buf[p] = (unsigned char) SCM_CHAR (elt);
  936. p++;
  937. rest = SCM_CDR (rest);
  938. len--;
  939. scm_remember_upto_here_1 (elt);
  940. }
  941. }
  942. else
  943. {
  944. scm_t_wchar *buf;
  945. result = scm_i_make_wide_string (len, NULL, 0);
  946. buf = scm_i_string_writable_wide_chars (result);
  947. while (len > 0 && scm_is_pair (rest))
  948. {
  949. SCM elt = SCM_CAR (rest);
  950. buf[p] = SCM_CHAR (elt);
  951. p++;
  952. rest = SCM_CDR (rest);
  953. len--;
  954. scm_remember_upto_here_1 (elt);
  955. }
  956. }
  957. if (len > 0)
  958. scm_misc_error (NULL, "list changed while constructing string", SCM_EOL);
  959. if (!scm_is_null (rest))
  960. scm_wrong_type_arg_msg (NULL, 0, chrs, "proper list");
  961. return result;
  962. }
  963. #undef FUNC_NAME
  964. SCM_DEFINE (scm_make_string, "make-string", 1, 1, 0,
  965. (SCM k, SCM chr),
  966. "Return a newly allocated string of\n"
  967. "length @var{k}. If @var{chr} is given, then all elements of\n"
  968. "the string are initialized to @var{chr}, otherwise the contents\n"
  969. "of the string are all set to @code{#\nul}.")
  970. #define FUNC_NAME s_scm_make_string
  971. {
  972. SCM ret = scm_c_make_string (scm_to_size_t (k), chr);
  973. /* Given that make-string is mostly used by Scheme to prepare a
  974. mutable string buffer, let's go ahead and mark this as mutable to
  975. avoid a copy when this buffer is next written to. */
  976. STRINGBUF_SET_MUTABLE (STRING_STRINGBUF (ret));
  977. return ret;
  978. }
  979. #undef FUNC_NAME
  980. SCM
  981. scm_c_make_string (size_t len, SCM chr)
  982. #define FUNC_NAME NULL
  983. {
  984. size_t p;
  985. char *contents = NULL;
  986. SCM res = scm_i_make_string (len, &contents, 0);
  987. /* If no char is given, initialize string contents to NULL. */
  988. if (SCM_UNBNDP (chr))
  989. memset (contents, 0, len);
  990. else
  991. {
  992. SCM_VALIDATE_CHAR (0, chr);
  993. for (p = 0; p < len; p++)
  994. scm_i_string_set_x (res, p, SCM_CHAR (chr));
  995. }
  996. return res;
  997. }
  998. #undef FUNC_NAME
  999. SCM_DEFINE (scm_string_length, "string-length", 1, 0, 0,
  1000. (SCM string),
  1001. "Return the number of characters in @var{string}.")
  1002. #define FUNC_NAME s_scm_string_length
  1003. {
  1004. SCM_VALIDATE_STRING (1, string);
  1005. return scm_from_size_t (STRING_LENGTH (string));
  1006. }
  1007. #undef FUNC_NAME
  1008. SCM_DEFINE (scm_string_bytes_per_char, "string-bytes-per-char", 1, 0, 0,
  1009. (SCM string),
  1010. "Return the bytes used to represent a character in @var{string}."
  1011. "This will return 1 or 4.")
  1012. #define FUNC_NAME s_scm_string_bytes_per_char
  1013. {
  1014. SCM_VALIDATE_STRING (1, string);
  1015. if (!scm_i_is_narrow_string (string))
  1016. return scm_from_int (4);
  1017. return scm_from_int (1);
  1018. }
  1019. #undef FUNC_NAME
  1020. size_t
  1021. scm_c_string_length (SCM string)
  1022. {
  1023. if (!IS_STRING (string))
  1024. scm_wrong_type_arg_msg (NULL, 0, string, "string");
  1025. return STRING_LENGTH (string);
  1026. }
  1027. SCM_DEFINE (scm_string_ref, "string-ref", 2, 0, 0,
  1028. (SCM str, SCM k),
  1029. "Return character @var{k} of @var{str} using zero-origin\n"
  1030. "indexing. @var{k} must be a valid index of @var{str}.")
  1031. #define FUNC_NAME s_scm_string_ref
  1032. {
  1033. size_t len;
  1034. unsigned long idx;
  1035. SCM_VALIDATE_STRING (1, str);
  1036. len = scm_i_string_length (str);
  1037. if (SCM_LIKELY (len > 0))
  1038. idx = scm_to_unsigned_integer (k, 0, len - 1);
  1039. else
  1040. scm_out_of_range (NULL, k);
  1041. if (scm_i_is_narrow_string (str))
  1042. return scm_c_make_char (scm_i_string_chars (str)[idx]);
  1043. else
  1044. return scm_c_make_char (scm_i_string_wide_chars (str)[idx]);
  1045. }
  1046. #undef FUNC_NAME
  1047. SCM
  1048. scm_c_string_ref (SCM str, size_t p)
  1049. {
  1050. if (p >= scm_i_string_length (str))
  1051. scm_out_of_range (NULL, scm_from_size_t (p));
  1052. if (scm_i_is_narrow_string (str))
  1053. return scm_c_make_char (scm_i_string_chars (str)[p]);
  1054. else
  1055. return scm_c_make_char (scm_i_string_wide_chars (str)[p]);
  1056. }
  1057. SCM_DEFINE (scm_string_set_x, "string-set!", 3, 0, 0,
  1058. (SCM str, SCM k, SCM chr),
  1059. "Store @var{chr} in element @var{k} of @var{str} and return\n"
  1060. "an unspecified value. @var{k} must be a valid index of\n"
  1061. "@var{str}.")
  1062. #define FUNC_NAME s_scm_string_set_x
  1063. {
  1064. size_t len;
  1065. unsigned long idx;
  1066. SCM_VALIDATE_STRING (1, str);
  1067. len = scm_i_string_length (str);
  1068. if (SCM_LIKELY (len > 0))
  1069. idx = scm_to_unsigned_integer (k, 0, len - 1);
  1070. else
  1071. scm_out_of_range (NULL, k);
  1072. SCM_VALIDATE_CHAR (3, chr);
  1073. str = scm_i_string_start_writing (str);
  1074. scm_i_string_set_x (str, idx, SCM_CHAR (chr));
  1075. scm_i_string_stop_writing ();
  1076. return SCM_UNSPECIFIED;
  1077. }
  1078. #undef FUNC_NAME
  1079. void
  1080. scm_c_string_set_x (SCM str, size_t p, SCM chr)
  1081. {
  1082. if (p >= scm_i_string_length (str))
  1083. scm_out_of_range (NULL, scm_from_size_t (p));
  1084. str = scm_i_string_start_writing (str);
  1085. scm_i_string_set_x (str, p, SCM_CHAR (chr));
  1086. scm_i_string_stop_writing ();
  1087. }
  1088. SCM_DEFINE (scm_substring, "substring", 2, 1, 0,
  1089. (SCM str, SCM start, SCM end),
  1090. "Return a newly allocated string formed from the characters\n"
  1091. "of @var{str} beginning with index @var{start} (inclusive) and\n"
  1092. "ending with index @var{end} (exclusive).\n"
  1093. "@var{str} must be a string, @var{start} and @var{end} must be\n"
  1094. "exact integers satisfying:\n\n"
  1095. "0 <= @var{start} <= @var{end} <= (string-length @var{str}).")
  1096. #define FUNC_NAME s_scm_substring
  1097. {
  1098. size_t len, from, to;
  1099. SCM_VALIDATE_STRING (1, str);
  1100. len = scm_i_string_length (str);
  1101. from = scm_to_unsigned_integer (start, 0, len);
  1102. if (SCM_UNBNDP (end))
  1103. to = len;
  1104. else
  1105. to = scm_to_unsigned_integer (end, from, len);
  1106. return scm_i_substring (str, from, to);
  1107. }
  1108. #undef FUNC_NAME
  1109. SCM_DEFINE (scm_substring_read_only, "substring/read-only", 2, 1, 0,
  1110. (SCM str, SCM start, SCM end),
  1111. "Return a newly allocated string formed from the characters\n"
  1112. "of @var{str} beginning with index @var{start} (inclusive) and\n"
  1113. "ending with index @var{end} (exclusive).\n"
  1114. "@var{str} must be a string, @var{start} and @var{end} must be\n"
  1115. "exact integers satisfying:\n"
  1116. "\n"
  1117. "0 <= @var{start} <= @var{end} <= (string-length @var{str}).\n"
  1118. "\n"
  1119. "The returned string is read-only.\n")
  1120. #define FUNC_NAME s_scm_substring_read_only
  1121. {
  1122. size_t len, from, to;
  1123. SCM_VALIDATE_STRING (1, str);
  1124. len = scm_i_string_length (str);
  1125. from = scm_to_unsigned_integer (start, 0, len);
  1126. if (SCM_UNBNDP (end))
  1127. to = len;
  1128. else
  1129. to = scm_to_unsigned_integer (end, from, len);
  1130. return scm_i_substring_read_only (str, from, to);
  1131. }
  1132. #undef FUNC_NAME
  1133. SCM_DEFINE (scm_substring_copy, "substring/copy", 2, 1, 0,
  1134. (SCM str, SCM start, SCM end),
  1135. "Return a newly allocated string formed from the characters\n"
  1136. "of @var{str} beginning with index @var{start} (inclusive) and\n"
  1137. "ending with index @var{end} (exclusive).\n"
  1138. "@var{str} must be a string, @var{start} and @var{end} must be\n"
  1139. "exact integers satisfying:\n\n"
  1140. "0 <= @var{start} <= @var{end} <= (string-length @var{str}).")
  1141. #define FUNC_NAME s_scm_substring_copy
  1142. {
  1143. /* For the Scheme version, START is mandatory, but for the C
  1144. version, it is optional. See scm_string_copy in srfi-13.c for a
  1145. rationale.
  1146. */
  1147. size_t from, to;
  1148. SCM_VALIDATE_STRING (1, str);
  1149. scm_i_get_substring_spec (scm_i_string_length (str),
  1150. start, &from, end, &to);
  1151. return scm_i_substring_copy (str, from, to);
  1152. }
  1153. #undef FUNC_NAME
  1154. SCM_DEFINE (scm_substring_shared, "substring/shared", 2, 1, 0,
  1155. (SCM str, SCM start, SCM end),
  1156. "Return string that indirectly refers to the characters\n"
  1157. "of @var{str} beginning with index @var{start} (inclusive) and\n"
  1158. "ending with index @var{end} (exclusive).\n"
  1159. "@var{str} must be a string, @var{start} and @var{end} must be\n"
  1160. "exact integers satisfying:\n\n"
  1161. "0 <= @var{start} <= @var{end} <= (string-length @var{str}).")
  1162. #define FUNC_NAME s_scm_substring_shared
  1163. {
  1164. size_t len, from, to;
  1165. SCM_VALIDATE_STRING (1, str);
  1166. len = scm_i_string_length (str);
  1167. from = scm_to_unsigned_integer (start, 0, len);
  1168. if (SCM_UNBNDP (end))
  1169. to = len;
  1170. else
  1171. to = scm_to_unsigned_integer (end, from, len);
  1172. return scm_i_substring_shared (str, from, to);
  1173. }
  1174. #undef FUNC_NAME
  1175. SCM_DEFINE (scm_string_append, "string-append", 0, 0, 1,
  1176. (SCM args),
  1177. "Return a newly allocated string whose characters form the\n"
  1178. "concatenation of the given strings, @var{args}.")
  1179. #define FUNC_NAME s_scm_string_append
  1180. {
  1181. SCM res;
  1182. size_t total = 0;
  1183. size_t len;
  1184. int wide = 0;
  1185. SCM l, s;
  1186. size_t i;
  1187. union
  1188. {
  1189. char *narrow;
  1190. scm_t_wchar *wide;
  1191. } data;
  1192. SCM_VALIDATE_REST_ARGUMENT (args);
  1193. for (l = args; !scm_is_null (l); l = SCM_CDR (l))
  1194. {
  1195. s = SCM_CAR (l);
  1196. SCM_VALIDATE_STRING (SCM_ARGn, s);
  1197. len = scm_i_string_length (s);
  1198. if (INT_ADD_OVERFLOW (total, len))
  1199. scm_num_overflow (FUNC_NAME);
  1200. total += len;
  1201. if (!scm_i_is_narrow_string (s))
  1202. wide = 1;
  1203. }
  1204. data.narrow = NULL;
  1205. if (!wide)
  1206. res = scm_i_make_string (total, &data.narrow, 0);
  1207. else
  1208. res = scm_i_make_wide_string (total, &data.wide, 0);
  1209. for (l = args; !scm_is_null (l); l = SCM_CDR (l))
  1210. {
  1211. size_t len;
  1212. s = SCM_CAR (l);
  1213. SCM_VALIDATE_STRING (SCM_ARGn, s);
  1214. len = scm_i_string_length (s);
  1215. if (len > total)
  1216. SCM_MISC_ERROR ("list changed during string-append", SCM_EOL);
  1217. if (!wide)
  1218. {
  1219. memcpy (data.narrow, scm_i_string_chars (s), len);
  1220. data.narrow += len;
  1221. }
  1222. else
  1223. {
  1224. if (scm_i_is_narrow_string (s))
  1225. {
  1226. const char *src = scm_i_string_chars (s);
  1227. for (i = 0; i < len; i++)
  1228. data.wide[i] = (unsigned char) src[i];
  1229. }
  1230. else
  1231. u32_cpy ((uint32_t *) data.wide,
  1232. (uint32_t *) scm_i_string_wide_chars (s), len);
  1233. data.wide += len;
  1234. }
  1235. total -= len;
  1236. scm_remember_upto_here_1 (s);
  1237. }
  1238. if (total != 0)
  1239. SCM_MISC_ERROR ("list changed during string-append", SCM_EOL);
  1240. return res;
  1241. }
  1242. #undef FUNC_NAME
  1243. /* Charset conversion error handling. */
  1244. SCM_SYMBOL (scm_encoding_error_key, "encoding-error");
  1245. SCM_SYMBOL (scm_decoding_error_key, "decoding-error");
  1246. /* Raise an exception informing that character CHR could not be written
  1247. to PORT in its current encoding. */
  1248. void
  1249. scm_encoding_error (const char *subr, int err, const char *message,
  1250. SCM port, SCM chr)
  1251. {
  1252. scm_throw (scm_encoding_error_key,
  1253. scm_list_n (scm_from_latin1_string (subr),
  1254. scm_from_latin1_string (message),
  1255. scm_from_int (err),
  1256. port, chr,
  1257. SCM_UNDEFINED));
  1258. }
  1259. /* Raise an exception informing of an encoding error on PORT. This
  1260. means that a character could not be written in PORT's encoding. */
  1261. void
  1262. scm_decoding_error (const char *subr, int err, const char *message, SCM port)
  1263. {
  1264. scm_throw (scm_decoding_error_key,
  1265. scm_list_n (scm_from_latin1_string (subr),
  1266. scm_from_latin1_string (message),
  1267. scm_from_int (err),
  1268. port,
  1269. SCM_UNDEFINED));
  1270. }
  1271. /* String conversion to/from C. */
  1272. static void
  1273. decoding_error (const char *func_name, int errno_save,
  1274. const char *str, size_t len)
  1275. {
  1276. /* Raise an error and pass the raw C string as a bytevector to the `throw'
  1277. handler. */
  1278. SCM bv;
  1279. signed char *buf;
  1280. buf = scm_gc_malloc_pointerless (len, "bytevector");
  1281. memcpy (buf, str, len);
  1282. bv = scm_c_take_gc_bytevector (buf, len, SCM_BOOL_F);
  1283. scm_decoding_error (func_name, errno_save,
  1284. "input locale conversion error", bv);
  1285. }
  1286. SCM
  1287. scm_from_stringn (const char *str, size_t len, const char *encoding,
  1288. scm_t_string_failed_conversion_handler handler)
  1289. {
  1290. size_t u32len, i;
  1291. scm_t_wchar *u32;
  1292. int wide = 0;
  1293. SCM res;
  1294. /* The order of these checks is important. */
  1295. if (!str && len != 0)
  1296. scm_misc_error ("scm_from_stringn", "NULL string pointer", SCM_EOL);
  1297. if (len == (size_t) -1)
  1298. len = strlen (str);
  1299. if (c_strcasecmp (encoding, "ISO-8859-1") == 0 || len == 0)
  1300. return scm_from_latin1_stringn (str, len);
  1301. else if (c_strcasecmp (encoding, "UTF-8") == 0
  1302. && handler == SCM_FAILED_CONVERSION_ERROR)
  1303. return scm_from_utf8_stringn (str, len);
  1304. u32len = 0;
  1305. u32 = (scm_t_wchar *) u32_conv_from_encoding (encoding,
  1306. (enum iconv_ilseq_handler)
  1307. handler,
  1308. str, len,
  1309. NULL,
  1310. NULL, &u32len);
  1311. if (SCM_UNLIKELY (u32 == NULL))
  1312. decoding_error (__func__, errno, str, len);
  1313. i = 0;
  1314. while (i < u32len)
  1315. if (u32[i++] > 0xFF)
  1316. {
  1317. wide = 1;
  1318. break;
  1319. }
  1320. if (!wide)
  1321. {
  1322. char *dst;
  1323. res = scm_i_make_string (u32len, &dst, 0);
  1324. for (i = 0; i < u32len; i ++)
  1325. dst[i] = (unsigned char) u32[i];
  1326. dst[u32len] = '\0';
  1327. }
  1328. else
  1329. {
  1330. scm_t_wchar *wdst;
  1331. res = scm_i_make_wide_string (u32len, &wdst, 0);
  1332. u32_cpy ((uint32_t *) wdst, (uint32_t *) u32, u32len);
  1333. wdst[u32len] = 0;
  1334. }
  1335. free (u32);
  1336. return res;
  1337. }
  1338. SCM
  1339. scm_from_locale_string (const char *str)
  1340. {
  1341. return scm_from_locale_stringn (str, -1);
  1342. }
  1343. scm_t_string_failed_conversion_handler
  1344. scm_i_default_string_failed_conversion_handler (void)
  1345. {
  1346. return scm_i_string_failed_conversion_handler
  1347. (scm_i_default_port_conversion_strategy ());
  1348. }
  1349. SCM
  1350. scm_from_locale_stringn (const char *str, size_t len)
  1351. {
  1352. return scm_from_stringn (str, len, locale_charset (),
  1353. scm_i_default_string_failed_conversion_handler ());
  1354. }
  1355. SCM
  1356. scm_from_latin1_string (const char *str)
  1357. {
  1358. return scm_from_latin1_stringn (str, -1);
  1359. }
  1360. SCM
  1361. scm_from_latin1_stringn (const char *str, size_t len)
  1362. {
  1363. char *buf;
  1364. SCM result;
  1365. if (len == (size_t) -1)
  1366. len = strlen (str);
  1367. /* Make a narrow string and copy STR as is. */
  1368. result = scm_i_make_string (len, &buf, 0);
  1369. memcpy (buf, str, len);
  1370. return result;
  1371. }
  1372. SCM
  1373. scm_from_utf8_string (const char *str)
  1374. {
  1375. return scm_from_utf8_stringn (str, -1);
  1376. }
  1377. SCM
  1378. scm_from_utf8_stringn (const char *str, size_t len)
  1379. {
  1380. size_t i, char_len;
  1381. const uint8_t *ustr = (const uint8_t *) str;
  1382. int ascii = 1, narrow = 1;
  1383. SCM res;
  1384. if (len == (size_t) -1)
  1385. len = strlen (str);
  1386. i = 0;
  1387. char_len = 0;
  1388. while (i < len)
  1389. {
  1390. if (ustr[i] <= 127)
  1391. {
  1392. char_len++;
  1393. i++;
  1394. }
  1395. else
  1396. {
  1397. ucs4_t c;
  1398. int nbytes;
  1399. ascii = 0;
  1400. nbytes = u8_mbtoucr (&c, ustr + i, len - i);
  1401. if (nbytes < 0)
  1402. /* Bad UTF-8. */
  1403. decoding_error (__func__, errno, str, len);
  1404. if (c > 255)
  1405. narrow = 0;
  1406. char_len++;
  1407. i += nbytes;
  1408. }
  1409. }
  1410. if (ascii)
  1411. {
  1412. char *dst;
  1413. res = scm_i_make_string (char_len, &dst, 0);
  1414. memcpy (dst, str, len);
  1415. }
  1416. else if (narrow)
  1417. {
  1418. char *dst;
  1419. size_t j;
  1420. ucs4_t c;
  1421. res = scm_i_make_string (char_len, &dst, 0);
  1422. for (i = 0, j = 0; i < len; j++)
  1423. {
  1424. i += u8_mbtouc_unsafe (&c, ustr + i, len - i);
  1425. dst[j] = (signed char) c;
  1426. }
  1427. }
  1428. else
  1429. {
  1430. scm_t_wchar *dst;
  1431. size_t j;
  1432. ucs4_t c;
  1433. res = scm_i_make_wide_string (char_len, &dst, 0);
  1434. for (i = 0, j = 0; i < len; j++)
  1435. {
  1436. i += u8_mbtouc_unsafe (&c, ustr + i, len - i);
  1437. dst[j] = c;
  1438. }
  1439. }
  1440. return res;
  1441. }
  1442. SCM
  1443. scm_from_utf32_string (const scm_t_wchar *str)
  1444. {
  1445. return scm_from_utf32_stringn (str, -1);
  1446. }
  1447. SCM
  1448. scm_from_utf32_stringn (const scm_t_wchar *str, size_t len)
  1449. {
  1450. SCM result;
  1451. scm_t_wchar *buf;
  1452. if (len == (size_t) -1)
  1453. len = u32_strlen ((uint32_t *) str);
  1454. result = scm_i_make_wide_string (len, &buf, 0);
  1455. memcpy (buf, str, len * sizeof (scm_t_wchar));
  1456. scm_i_try_narrow_string (result);
  1457. return result;
  1458. }
  1459. SCM
  1460. scm_from_port_string (const char *str, SCM port)
  1461. {
  1462. return scm_from_port_stringn (str, -1, port);
  1463. }
  1464. SCM
  1465. scm_from_port_stringn (const char *str, size_t len, SCM port)
  1466. {
  1467. scm_t_port *pt = SCM_PORT (port);
  1468. if (scm_is_eq (pt->encoding, sym_ISO_8859_1))
  1469. return scm_from_latin1_stringn (str, len);
  1470. else if (scm_is_eq (pt->encoding, sym_UTF_8)
  1471. && (scm_is_eq (pt->conversion_strategy, sym_error)
  1472. || (u8_check ((uint8_t *) str, len) == NULL)))
  1473. return scm_from_utf8_stringn (str, len);
  1474. else
  1475. return scm_from_stringn (str, len, scm_i_symbol_chars (pt->encoding),
  1476. scm_i_string_failed_conversion_handler
  1477. (SCM_PORT (port)->conversion_strategy));
  1478. }
  1479. /* Create a new scheme string from the C string STR. The memory of
  1480. STR may be used directly as storage for the new string. */
  1481. /* FIXME: GC-wise, the only way to use the memory area pointed to by STR
  1482. would be to register a finalizer to eventually free(3) STR, which isn't
  1483. worth it. Should we just deprecate the `scm_take_' functions? */
  1484. SCM
  1485. scm_take_locale_stringn (char *str, size_t len)
  1486. {
  1487. SCM res;
  1488. res = scm_from_locale_stringn (str, len);
  1489. free (str);
  1490. return res;
  1491. }
  1492. SCM
  1493. scm_take_locale_string (char *str)
  1494. {
  1495. return scm_take_locale_stringn (str, -1);
  1496. }
  1497. /* Change libunistring escapes (`\uXXXX' and `\UXXXXXXXX') in BUF, a
  1498. *LENP-byte locale-encoded string, to `\xXX', `\uXXXX', or `\UXXXXXX'.
  1499. Set *LENP to the size of the resulting string.
  1500. FIXME: This is a hack we should get rid of. See
  1501. <http://lists.gnu.org/archive/html/bug-libunistring/2010-09/msg00004.html>
  1502. for details. */
  1503. static void
  1504. unistring_escapes_to_guile_escapes (char *buf, size_t *lenp)
  1505. {
  1506. char *before, *after;
  1507. size_t i, j;
  1508. before = buf;
  1509. after = buf;
  1510. i = 0;
  1511. j = 0;
  1512. while (i < *lenp)
  1513. {
  1514. if ((i <= *lenp - 6)
  1515. && before[i] == '\\'
  1516. && before[i + 1] == 'u'
  1517. && before[i + 2] == '0' && before[i + 3] == '0')
  1518. {
  1519. /* Convert \u00NN to \xNN */
  1520. after[j] = '\\';
  1521. after[j + 1] = 'x';
  1522. after[j + 2] = tolower ((int) before[i + 4]);
  1523. after[j + 3] = tolower ((int) before[i + 5]);
  1524. i += 6;
  1525. j += 4;
  1526. }
  1527. else if ((i <= *lenp - 10)
  1528. && before[i] == '\\'
  1529. && before[i + 1] == 'U'
  1530. && before[i + 2] == '0' && before[i + 3] == '0')
  1531. {
  1532. /* Convert \U00NNNNNN to \UNNNNNN */
  1533. after[j] = '\\';
  1534. after[j + 1] = 'U';
  1535. after[j + 2] = tolower ((int) before[i + 4]);
  1536. after[j + 3] = tolower ((int) before[i + 5]);
  1537. after[j + 4] = tolower ((int) before[i + 6]);
  1538. after[j + 5] = tolower ((int) before[i + 7]);
  1539. after[j + 6] = tolower ((int) before[i + 8]);
  1540. after[j + 7] = tolower ((int) before[i + 9]);
  1541. i += 10;
  1542. j += 8;
  1543. }
  1544. else
  1545. {
  1546. after[j] = before[i];
  1547. i++;
  1548. j++;
  1549. }
  1550. }
  1551. *lenp = j;
  1552. }
  1553. /* Change libunistring escapes (`\uXXXX' and `\UXXXXXXXX') in BUF, a
  1554. *LENP-byte locale-encoded string, to `\xXXXX;'. Set *LEN to the size
  1555. of the resulting string. BUF must be large enough to handle the
  1556. worst case when `\uXXXX' escapes (6 characters) are replaced by
  1557. `\xXXXX;' (7 characters). */
  1558. static void
  1559. unistring_escapes_to_r6rs_escapes (char *buf, size_t *lenp)
  1560. {
  1561. char *before, *after;
  1562. int malloc_p;
  1563. size_t i, j;
  1564. /* The worst case is if the input string contains all 4-digit hex escapes.
  1565. "\uXXXX" (six characters) becomes "\xXXXX;" (seven characters) */
  1566. size_t max_out_len = (*lenp * 7) / 6 + 1;
  1567. size_t nzeros, ndigits;
  1568. before = buf;
  1569. malloc_p = (max_out_len > SCM_MAX_ALLOCA);
  1570. after = malloc_p ? malloc (max_out_len) : alloca (max_out_len);
  1571. i = 0;
  1572. j = 0;
  1573. while (i < *lenp)
  1574. {
  1575. if (((i <= *lenp - 6) && before[i] == '\\' && before[i + 1] == 'u')
  1576. || ((i <= *lenp - 10) && before[i] == '\\' && before[i + 1] == 'U'))
  1577. {
  1578. if (before[i + 1] == 'u')
  1579. ndigits = 4;
  1580. else if (before[i + 1] == 'U')
  1581. ndigits = 8;
  1582. else
  1583. abort ();
  1584. /* Add the R6RS hex escape initial sequence. */
  1585. after[j] = '\\';
  1586. after[j + 1] = 'x';
  1587. /* Move string positions to the start of the hex numbers. */
  1588. i += 2;
  1589. j += 2;
  1590. /* Find the number of initial zeros in this hex number. */
  1591. nzeros = 0;
  1592. while (before[i + nzeros] == '0' && nzeros < ndigits)
  1593. nzeros++;
  1594. /* Copy the number, skipping initial zeros, and then move the string
  1595. positions. */
  1596. if (nzeros == ndigits)
  1597. {
  1598. after[j] = '0';
  1599. i += ndigits;
  1600. j += 1;
  1601. }
  1602. else
  1603. {
  1604. int pos;
  1605. for (pos = 0; pos < ndigits - nzeros; pos++)
  1606. after[j + pos] = tolower ((int) before[i + nzeros + pos]);
  1607. i += ndigits;
  1608. j += (ndigits - nzeros);
  1609. }
  1610. /* Add terminating semicolon. */
  1611. after[j] = ';';
  1612. j++;
  1613. }
  1614. else
  1615. {
  1616. after[j] = before[i];
  1617. i++;
  1618. j++;
  1619. }
  1620. }
  1621. *lenp = j;
  1622. memcpy (before, after, j);
  1623. if (malloc_p)
  1624. free (after);
  1625. }
  1626. char *
  1627. scm_to_locale_string (SCM str)
  1628. {
  1629. return scm_to_locale_stringn (str, NULL);
  1630. }
  1631. char *
  1632. scm_to_locale_stringn (SCM str, size_t *lenp)
  1633. {
  1634. return scm_to_stringn (str, lenp,
  1635. locale_charset (),
  1636. scm_i_default_string_failed_conversion_handler ());
  1637. }
  1638. char *
  1639. scm_to_latin1_string (SCM str)
  1640. {
  1641. return scm_to_latin1_stringn (str, NULL);
  1642. }
  1643. char *
  1644. scm_to_latin1_stringn (SCM str, size_t *lenp)
  1645. #define FUNC_NAME "scm_to_latin1_stringn"
  1646. {
  1647. char *result;
  1648. SCM_VALIDATE_STRING (1, str);
  1649. if (scm_i_is_narrow_string (str))
  1650. {
  1651. size_t len = scm_i_string_length (str);
  1652. if (lenp)
  1653. *lenp = len;
  1654. result = scm_strndup (scm_i_string_data (str), len);
  1655. }
  1656. else
  1657. result = scm_to_stringn (str, lenp, NULL,
  1658. SCM_FAILED_CONVERSION_ERROR);
  1659. return result;
  1660. }
  1661. #undef FUNC_NAME
  1662. char *
  1663. scm_to_utf8_string (SCM str)
  1664. {
  1665. return scm_to_utf8_stringn (str, NULL);
  1666. }
  1667. static size_t
  1668. latin1_u8_strlen (const uint8_t *str, size_t len)
  1669. {
  1670. size_t ret, i;
  1671. for (i = 0, ret = 0; i < len; i++)
  1672. ret += (str[i] < 128) ? 1 : 2;
  1673. return ret;
  1674. }
  1675. static uint8_t*
  1676. latin1_to_u8 (const uint8_t *str, size_t latin_len,
  1677. uint8_t *u8_result, size_t *u8_lenp)
  1678. {
  1679. size_t i, n;
  1680. size_t u8_len = latin1_u8_strlen (str, latin_len);
  1681. if (!(u8_result && u8_lenp && *u8_lenp > u8_len))
  1682. u8_result = scm_malloc (u8_len + 1);
  1683. if (u8_lenp)
  1684. *u8_lenp = u8_len;
  1685. for (i = 0, n = 0; i < latin_len; i++)
  1686. n += u8_uctomb (u8_result + n, str[i], u8_len - n);
  1687. if (n != u8_len)
  1688. abort ();
  1689. u8_result[n] = 0;
  1690. return u8_result;
  1691. }
  1692. /* UTF-8 code table
  1693. (Note that this includes code points that are not allowed by Unicode,
  1694. but since this function has no way to report an error, and its
  1695. purpose is to determine the size of destination buffers for
  1696. libunicode conversion functions, we err on the safe side and handle
  1697. everything that libunicode might conceivably handle, now or in the
  1698. future.)
  1699. Char. number range | UTF-8 octet sequence
  1700. (hexadecimal) | (binary)
  1701. --------------------+------------------------------------------------------
  1702. 0000 0000-0000 007F | 0xxxxxxx
  1703. 0000 0080-0000 07FF | 110xxxxx 10xxxxxx
  1704. 0000 0800-0000 FFFF | 1110xxxx 10xxxxxx 10xxxxxx
  1705. 0001 0000-001F FFFF | 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
  1706. 0020 0000-03FF FFFF | 111110xx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx
  1707. 0400 0000-7FFF FFFF | 1111110x 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx
  1708. */
  1709. static size_t
  1710. u32_u8_length_in_bytes (const uint32_t *str, size_t len)
  1711. {
  1712. size_t ret, i;
  1713. for (i = 0, ret = 0; i < len; i++)
  1714. {
  1715. uint32_t c = str[i];
  1716. if (c <= 0x7f)
  1717. ret += 1;
  1718. else if (c <= 0x7ff)
  1719. ret += 2;
  1720. else if (c <= 0xffff)
  1721. ret += 3;
  1722. else if (c <= 0x1fffff)
  1723. ret += 4;
  1724. else if (c <= 0x3ffffff)
  1725. ret += 5;
  1726. else
  1727. ret += 6;
  1728. }
  1729. return ret;
  1730. }
  1731. static size_t
  1732. utf8_length (SCM str)
  1733. {
  1734. if (scm_i_is_narrow_string (str))
  1735. return latin1_u8_strlen ((uint8_t *) scm_i_string_chars (str),
  1736. scm_i_string_length (str));
  1737. else
  1738. return u32_u8_length_in_bytes
  1739. ((uint32_t *) scm_i_string_wide_chars (str),
  1740. scm_i_string_length (str));
  1741. }
  1742. size_t
  1743. scm_c_string_utf8_length (SCM string)
  1744. #define FUNC_NAME "scm_c_string_utf8_length"
  1745. {
  1746. SCM_VALIDATE_STRING (1, string);
  1747. return utf8_length (string);
  1748. }
  1749. #undef FUNC_NAME
  1750. SCM_DEFINE (scm_string_utf8_length, "string-utf8-length", 1, 0, 0,
  1751. (SCM string),
  1752. "Returns the number of bytes in the UTF-8 representation of "
  1753. "@var{string}.")
  1754. #define FUNC_NAME s_scm_string_utf8_length
  1755. {
  1756. SCM_VALIDATE_STRING (1, string);
  1757. return scm_from_size_t (utf8_length (string));
  1758. }
  1759. #undef FUNC_NAME
  1760. char *
  1761. scm_to_utf8_stringn (SCM str, size_t *lenp)
  1762. #define FUNC_NAME "scm_to_utf8_stringn"
  1763. {
  1764. SCM_VALIDATE_STRING (1, str);
  1765. if (scm_i_is_narrow_string (str))
  1766. return (char *) latin1_to_u8 ((uint8_t *) scm_i_string_chars (str),
  1767. scm_i_string_length (str),
  1768. NULL, lenp);
  1769. else
  1770. {
  1771. uint32_t *chars = (uint32_t *) scm_i_string_wide_chars (str);
  1772. uint8_t *buf, *ret;
  1773. size_t num_chars = scm_i_string_length (str);
  1774. size_t num_bytes_predicted, num_bytes_actual;
  1775. num_bytes_predicted = u32_u8_length_in_bytes (chars, num_chars);
  1776. if (lenp)
  1777. {
  1778. *lenp = num_bytes_predicted;
  1779. buf = scm_malloc (num_bytes_predicted);
  1780. }
  1781. else
  1782. {
  1783. buf = scm_malloc (num_bytes_predicted + 1);
  1784. buf[num_bytes_predicted] = 0;
  1785. }
  1786. num_bytes_actual = num_bytes_predicted;
  1787. ret = u32_to_u8 (chars, num_chars, buf, &num_bytes_actual);
  1788. if (SCM_LIKELY (ret == buf && num_bytes_actual == num_bytes_predicted))
  1789. return (char *) ret;
  1790. /* An error: a bad codepoint. */
  1791. {
  1792. int saved_errno = errno;
  1793. free (buf);
  1794. if (!saved_errno)
  1795. abort ();
  1796. scm_decoding_error ("scm_to_utf8_stringn", errno,
  1797. "invalid codepoint in string", str);
  1798. /* Not reached. */
  1799. return NULL;
  1800. }
  1801. }
  1802. }
  1803. #undef FUNC_NAME
  1804. scm_t_wchar *
  1805. scm_to_utf32_string (SCM str)
  1806. {
  1807. return scm_to_utf32_stringn (str, NULL);
  1808. }
  1809. scm_t_wchar *
  1810. scm_to_utf32_stringn (SCM str, size_t *lenp)
  1811. #define FUNC_NAME "scm_to_utf32_stringn"
  1812. {
  1813. scm_t_wchar *result;
  1814. SCM_VALIDATE_STRING (1, str);
  1815. if (scm_i_is_narrow_string (str))
  1816. {
  1817. uint8_t *codepoints;
  1818. size_t i, len;
  1819. codepoints = (uint8_t*) scm_i_string_chars (str);
  1820. len = scm_i_string_length (str);
  1821. if (lenp)
  1822. *lenp = len;
  1823. result = scm_malloc ((len + 1) * sizeof (scm_t_wchar));
  1824. for (i = 0; i < len; i++)
  1825. result[i] = codepoints[i];
  1826. result[len] = 0;
  1827. }
  1828. else
  1829. {
  1830. size_t len;
  1831. len = scm_i_string_length (str);
  1832. if (lenp)
  1833. *lenp = len;
  1834. result = scm_malloc ((len + 1) * sizeof (scm_t_wchar));
  1835. memcpy (result, scm_i_string_wide_chars (str),
  1836. len * sizeof (scm_t_wchar));
  1837. result[len] = 0;
  1838. }
  1839. return result;
  1840. }
  1841. #undef FUNC_NAME
  1842. char *
  1843. scm_to_port_string (SCM str, SCM port)
  1844. {
  1845. return scm_to_port_stringn (str, NULL, port);
  1846. }
  1847. char *
  1848. scm_to_port_stringn (SCM str, size_t *lenp, SCM port)
  1849. {
  1850. scm_t_port *pt = SCM_PORT (port);
  1851. if (scm_is_eq (pt->encoding, sym_ISO_8859_1)
  1852. && scm_is_eq (pt->conversion_strategy, sym_error))
  1853. return scm_to_latin1_stringn (str, lenp);
  1854. else if (scm_is_eq (pt->encoding, sym_UTF_8))
  1855. return scm_to_utf8_stringn (str, lenp);
  1856. else
  1857. return scm_to_stringn (str, lenp, scm_i_symbol_chars (pt->encoding),
  1858. scm_i_string_failed_conversion_handler
  1859. (SCM_PORT (port)->conversion_strategy));
  1860. }
  1861. /* Return a malloc(3)-allocated buffer containing the contents of STR encoded
  1862. according to ENCODING. If LENP is non-NULL, set it to the size in bytes of
  1863. the returned buffer. If the conversion to ENCODING fails, apply the strategy
  1864. defined by HANDLER. */
  1865. char *
  1866. scm_to_stringn (SCM str, size_t *lenp, const char *encoding,
  1867. scm_t_string_failed_conversion_handler handler)
  1868. {
  1869. char *buf;
  1870. size_t ilen, len, i;
  1871. int ret;
  1872. if (!scm_is_string (str))
  1873. scm_wrong_type_arg_msg (NULL, 0, str, "string");
  1874. if (encoding == NULL)
  1875. encoding = "ISO-8859-1";
  1876. if (c_strcasecmp (encoding, "UTF-8") == 0)
  1877. /* This is the most common case--e.g., when calling libc bindings
  1878. while using a UTF-8 locale. */
  1879. return scm_to_utf8_stringn (str, lenp);
  1880. ilen = scm_i_string_length (str);
  1881. if (ilen == 0)
  1882. {
  1883. buf = scm_malloc (1);
  1884. buf[0] = '\0';
  1885. if (lenp)
  1886. *lenp = 0;
  1887. return buf;
  1888. }
  1889. if (lenp == NULL)
  1890. for (i = 0; i < ilen; i++)
  1891. if (scm_i_string_ref (str, i) == '\0')
  1892. scm_misc_error (NULL,
  1893. "string contains #\\nul character: ~S",
  1894. scm_list_1 (str));
  1895. if (scm_i_is_narrow_string (str)
  1896. && c_strcasecmp (encoding, "ISO-8859-1") == 0)
  1897. {
  1898. /* If using native Latin-1 encoding, just copy the string
  1899. contents. */
  1900. if (lenp)
  1901. {
  1902. buf = scm_malloc (ilen);
  1903. memcpy (buf, scm_i_string_chars (str), ilen);
  1904. *lenp = ilen;
  1905. return buf;
  1906. }
  1907. else
  1908. {
  1909. buf = scm_malloc (ilen + 1);
  1910. memcpy (buf, scm_i_string_chars (str), ilen);
  1911. buf[ilen] = '\0';
  1912. return buf;
  1913. }
  1914. }
  1915. buf = NULL;
  1916. len = 0;
  1917. if (scm_i_is_narrow_string (str))
  1918. {
  1919. ret = mem_iconveh (scm_i_string_chars (str), ilen,
  1920. "ISO-8859-1", encoding,
  1921. (enum iconv_ilseq_handler) handler, NULL,
  1922. &buf, &len);
  1923. if (ret != 0)
  1924. scm_encoding_error (__func__, errno,
  1925. "cannot convert narrow string to output locale",
  1926. SCM_BOOL_F,
  1927. /* FIXME: Faulty character unknown. */
  1928. SCM_BOOL_F);
  1929. }
  1930. else
  1931. {
  1932. buf = u32_conv_to_encoding (encoding,
  1933. (enum iconv_ilseq_handler) handler,
  1934. (uint32_t *) scm_i_string_wide_chars (str),
  1935. ilen,
  1936. NULL,
  1937. NULL, &len);
  1938. if (buf == NULL)
  1939. scm_encoding_error (__func__, errno,
  1940. "cannot convert wide string to output locale",
  1941. SCM_BOOL_F,
  1942. /* FIXME: Faulty character unknown. */
  1943. SCM_BOOL_F);
  1944. }
  1945. if (handler == SCM_FAILED_CONVERSION_ESCAPE_SEQUENCE)
  1946. {
  1947. if (SCM_R6RS_ESCAPES_P)
  1948. {
  1949. /* The worst case is if the input string contains all 4-digit
  1950. hex escapes. "\uXXXX" (six characters) becomes "\xXXXX;"
  1951. (seven characters). Make BUF large enough to hold
  1952. that. */
  1953. buf = scm_realloc (buf, (len * 7) / 6 + 1);
  1954. unistring_escapes_to_r6rs_escapes (buf, &len);
  1955. }
  1956. else
  1957. unistring_escapes_to_guile_escapes (buf, &len);
  1958. buf = scm_realloc (buf, len);
  1959. }
  1960. if (lenp)
  1961. *lenp = len;
  1962. else
  1963. {
  1964. buf = scm_realloc (buf, len + 1);
  1965. buf[len] = '\0';
  1966. }
  1967. scm_remember_upto_here_1 (str);
  1968. return buf;
  1969. }
  1970. size_t
  1971. scm_to_locale_stringbuf (SCM str, char *buf, size_t max_len)
  1972. {
  1973. size_t len, copy_len;
  1974. char *result = NULL;
  1975. if (!scm_is_string (str))
  1976. scm_wrong_type_arg_msg (NULL, 0, str, "string");
  1977. result = scm_to_locale_stringn (str, &len);
  1978. copy_len = (len > max_len) ? max_len : len;
  1979. if (copy_len != 0)
  1980. /* Some users of 'scm_to_locale_stringbuf' may pass NULL for buf
  1981. when max_len is zero, and yet we must avoid passing NULL to
  1982. memcpy to avoid undefined behavior. */
  1983. memcpy (buf, result, copy_len);
  1984. free (result);
  1985. scm_remember_upto_here_1 (str);
  1986. return len;
  1987. }
  1988. /* Unicode string normalization. */
  1989. /* This function is a partial clone of SCM_STRING_TO_U32_BUF from
  1990. libguile/i18n.c. It would be useful to have this factored out into a more
  1991. convenient location, but its use of alloca makes that tricky to do. */
  1992. static SCM
  1993. normalize_str (SCM string, uninorm_t form)
  1994. {
  1995. SCM ret;
  1996. uint32_t *w_str;
  1997. uint32_t *w_norm_str;
  1998. scm_t_wchar *cbuf;
  1999. int malloc_p;
  2000. size_t norm_len, len = scm_i_string_length (string);
  2001. if (scm_i_is_narrow_string (string))
  2002. {
  2003. size_t i, bytes;
  2004. const char *buf = scm_i_string_chars (string);
  2005. bytes = (len + 1) * sizeof (scm_t_wchar);
  2006. malloc_p = (bytes > SCM_MAX_ALLOCA);
  2007. w_str = malloc_p ? malloc (bytes) : alloca (bytes);
  2008. for (i = 0; i < len; i ++)
  2009. w_str[i] = (unsigned char) buf[i];
  2010. w_str[len] = 0;
  2011. }
  2012. else
  2013. {
  2014. malloc_p = 0;
  2015. w_str = (uint32_t *) scm_i_string_wide_chars (string);
  2016. }
  2017. w_norm_str = u32_normalize (form, w_str, len, NULL, &norm_len);
  2018. ret = scm_i_make_wide_string (norm_len, &cbuf, 0);
  2019. u32_cpy ((uint32_t *) cbuf, w_norm_str, norm_len);
  2020. free (w_norm_str);
  2021. if (malloc_p)
  2022. free (w_str);
  2023. scm_i_try_narrow_string (ret);
  2024. return ret;
  2025. }
  2026. SCM_DEFINE (scm_string_normalize_nfc, "string-normalize-nfc", 1, 0, 0,
  2027. (SCM string),
  2028. "Returns the NFC normalized form of @var{string}.")
  2029. #define FUNC_NAME s_scm_string_normalize_nfc
  2030. {
  2031. SCM_VALIDATE_STRING (1, string);
  2032. return normalize_str (string, UNINORM_NFC);
  2033. }
  2034. #undef FUNC_NAME
  2035. SCM_DEFINE (scm_string_normalize_nfd, "string-normalize-nfd", 1, 0, 0,
  2036. (SCM string),
  2037. "Returns the NFD normalized form of @var{string}.")
  2038. #define FUNC_NAME s_scm_string_normalize_nfd
  2039. {
  2040. SCM_VALIDATE_STRING (1, string);
  2041. return normalize_str (string, UNINORM_NFD);
  2042. }
  2043. #undef FUNC_NAME
  2044. SCM_DEFINE (scm_string_normalize_nfkc, "string-normalize-nfkc", 1, 0, 0,
  2045. (SCM string),
  2046. "Returns the NFKC normalized form of @var{string}.")
  2047. #define FUNC_NAME s_scm_string_normalize_nfkc
  2048. {
  2049. SCM_VALIDATE_STRING (1, string);
  2050. return normalize_str (string, UNINORM_NFKC);
  2051. }
  2052. #undef FUNC_NAME
  2053. SCM_DEFINE (scm_string_normalize_nfkd, "string-normalize-nfkd", 1, 0, 0,
  2054. (SCM string),
  2055. "Returns the NFKD normalized form of @var{string}.")
  2056. #define FUNC_NAME s_scm_string_normalize_nfkd
  2057. {
  2058. SCM_VALIDATE_STRING (1, string);
  2059. return normalize_str (string, UNINORM_NFKD);
  2060. }
  2061. #undef FUNC_NAME
  2062. /* converts C scm_array of strings to SCM scm_list of strings.
  2063. If argc < 0, a null terminated scm_array is assumed.
  2064. The current locale encoding is assumed */
  2065. SCM
  2066. scm_makfromstrs (int argc, char **argv)
  2067. {
  2068. int i = argc;
  2069. SCM lst = SCM_EOL;
  2070. if (0 > i)
  2071. for (i = 0; argv[i]; i++);
  2072. while (i--)
  2073. lst = scm_cons (scm_from_locale_string (argv[i]), lst);
  2074. return lst;
  2075. }
  2076. /* Return a newly allocated array of char pointers to each of the strings
  2077. in args, with a terminating NULL pointer. The strings are encoded using
  2078. the current locale. */
  2079. char **
  2080. scm_i_allocate_string_pointers (SCM list)
  2081. #define FUNC_NAME "scm_i_allocate_string_pointers"
  2082. {
  2083. char **result;
  2084. int list_len = scm_ilength (list);
  2085. int i;
  2086. if (list_len < 0)
  2087. scm_wrong_type_arg_msg (NULL, 0, list, "proper list");
  2088. result = scm_gc_malloc ((list_len + 1) * sizeof (char *),
  2089. "string pointers");
  2090. result[list_len] = NULL;
  2091. /* The list might have been modified in another thread, so
  2092. we check LIST before each access.
  2093. */
  2094. for (i = 0; i < list_len && scm_is_pair (list); i++)
  2095. {
  2096. SCM str = SCM_CAR (list);
  2097. size_t len; /* String length in bytes */
  2098. char *c_str = scm_to_locale_stringn (str, &len);
  2099. /* OPTIMIZE-ME: Right now, scm_to_locale_stringn always uses
  2100. scm_malloc to allocate the returned string, which must be
  2101. explicitly deallocated. This forces us to copy the string a
  2102. second time into a new buffer. Ideally there would be variants
  2103. of scm_to_*_stringn that can return garbage-collected buffers. */
  2104. result[i] = scm_gc_malloc_pointerless (len + 1, "string");
  2105. memcpy (result[i], c_str, len);
  2106. result[i][len] = '\0';
  2107. free (c_str);
  2108. list = SCM_CDR (list);
  2109. }
  2110. return result;
  2111. }
  2112. #undef FUNC_NAME
  2113. void
  2114. scm_i_get_substring_spec (size_t len,
  2115. SCM start, size_t *cstart,
  2116. SCM end, size_t *cend)
  2117. {
  2118. if (SCM_UNBNDP (start))
  2119. *cstart = 0;
  2120. else
  2121. *cstart = scm_to_unsigned_integer (start, 0, len);
  2122. if (SCM_UNBNDP (end))
  2123. *cend = len;
  2124. else
  2125. *cend = scm_to_unsigned_integer (end, *cstart, len);
  2126. }
  2127. SCM_VECTOR_IMPLEMENTATION (SCM_ARRAY_ELEMENT_TYPE_CHAR, scm_make_string)
  2128. void
  2129. scm_init_strings ()
  2130. {
  2131. scm_nullstr = scm_i_make_string (0, NULL, 0);
  2132. #include "strings.x"
  2133. }