strings.c 66 KB

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