strings.c 66 KB

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