strings.c 67 KB

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