strings.c 67 KB

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