strings.c 65 KB

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