strings.c 65 KB

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