123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169 |
- #ifdef HAVE_CONFIG_H
- # include <config.h>
- #endif
- #include <alloca.h>
- #include <string.h>
- #include <stdio.h>
- #include <ctype.h>
- #include <uninorm.h>
- #include <unistr.h>
- #include <uniconv.h>
- #include "striconveh.h"
- #include "libguile/_scm.h"
- #include "libguile/chars.h"
- #include "libguile/root.h"
- #include "libguile/strings.h"
- #include "libguile/error.h"
- #include "libguile/generalized-vectors.h"
- #include "libguile/deprecation.h"
- #include "libguile/validate.h"
- #include "libguile/private-options.h"
- #define STRINGBUF_HEADER_SIZE 2U
- #define STRINGBUF_HEADER_BYTES (STRINGBUF_HEADER_SIZE * sizeof (SCM))
- #define STRINGBUF_F_SHARED SCM_I_STRINGBUF_F_SHARED
- #define STRINGBUF_F_WIDE SCM_I_STRINGBUF_F_WIDE
- #define STRINGBUF_TAG scm_tc7_stringbuf
- #define STRINGBUF_SHARED(buf) (SCM_CELL_WORD_0(buf) & STRINGBUF_F_SHARED)
- #define STRINGBUF_WIDE(buf) (SCM_CELL_WORD_0(buf) & STRINGBUF_F_WIDE)
- #define STRINGBUF_CONTENTS(buf) ((void *) \
- SCM_CELL_OBJECT_LOC (buf, \
- STRINGBUF_HEADER_SIZE))
- #define STRINGBUF_CHARS(buf) ((unsigned char *) STRINGBUF_CONTENTS (buf))
- #define STRINGBUF_WIDE_CHARS(buf) ((scm_t_wchar *) STRINGBUF_CONTENTS (buf))
- #define STRINGBUF_LENGTH(buf) (SCM_CELL_WORD_1 (buf))
- #define SET_STRINGBUF_SHARED(buf) \
- do \
- { \
-
- \
- if (SCM_LIKELY (!STRINGBUF_SHARED (buf))) \
- SCM_SET_CELL_WORD_0 ((buf), SCM_CELL_WORD_0 (buf) | STRINGBUF_F_SHARED); \
- } \
- while (0)
- #ifdef SCM_STRING_LENGTH_HISTOGRAM
- static size_t lenhist[1001];
- #endif
- static SCM
- make_stringbuf (size_t len)
- {
-
- SCM buf;
- #ifdef SCM_STRING_LENGTH_HISTOGRAM
- if (len < 1000)
- lenhist[len]++;
- else
- lenhist[1000]++;
- #endif
- buf = PTR2SCM (scm_gc_malloc_pointerless (STRINGBUF_HEADER_BYTES + len + 1,
- "string"));
- SCM_SET_CELL_TYPE (buf, STRINGBUF_TAG);
- SCM_SET_CELL_WORD_1 (buf, (scm_t_bits) len);
- STRINGBUF_CHARS (buf)[len] = 0;
- return buf;
- }
- static SCM
- make_wide_stringbuf (size_t len)
- {
- SCM buf;
- size_t raw_len;
- #ifdef SCM_STRING_LENGTH_HISTOGRAM
- if (len < 1000)
- lenhist[len]++;
- else
- lenhist[1000]++;
- #endif
- raw_len = (len + 1) * sizeof (scm_t_wchar);
- buf = PTR2SCM (scm_gc_malloc_pointerless (STRINGBUF_HEADER_BYTES + raw_len,
- "string"));
- SCM_SET_CELL_TYPE (buf, STRINGBUF_TAG | STRINGBUF_F_WIDE);
- SCM_SET_CELL_WORD_1 (buf, (scm_t_bits) len);
- STRINGBUF_WIDE_CHARS (buf)[len] = 0;
- return buf;
- }
- static SCM
- wide_stringbuf (SCM buf)
- {
- SCM new_buf;
- if (STRINGBUF_WIDE (buf))
- new_buf = buf;
- else
- {
- size_t i, len;
- scm_t_wchar *mem;
- len = STRINGBUF_LENGTH (buf);
- new_buf = make_wide_stringbuf (len);
- mem = STRINGBUF_WIDE_CHARS (new_buf);
- for (i = 0; i < len; i++)
- mem[i] = (scm_t_wchar) STRINGBUF_CHARS (buf)[i];
- mem[len] = 0;
- }
- return new_buf;
- }
- static SCM
- narrow_stringbuf (SCM buf)
- {
- SCM new_buf;
- if (!STRINGBUF_WIDE (buf))
- new_buf = buf;
- else
- {
- size_t i, len;
- scm_t_wchar *wmem;
- unsigned char *mem;
- len = STRINGBUF_LENGTH (buf);
- wmem = STRINGBUF_WIDE_CHARS (buf);
- for (i = 0; i < len; i++)
- if (wmem[i] > 0xFF)
-
- return buf;
- new_buf = make_stringbuf (len);
- mem = STRINGBUF_CHARS (new_buf);
- for (i = 0; i < len; i++)
- mem[i] = (unsigned char) wmem[i];
- mem[len] = 0;
- }
- return new_buf;
- }
- scm_i_pthread_mutex_t stringbuf_write_mutex = SCM_I_PTHREAD_MUTEX_INITIALIZER;
- #define STRING_TAG scm_tc7_string
- #define STRING_STRINGBUF(str) (SCM_CELL_OBJECT_1(str))
- #define STRING_START(str) ((size_t)SCM_CELL_WORD_2(str))
- #define STRING_LENGTH(str) ((size_t)SCM_CELL_WORD_3(str))
- #define SET_STRING_STRINGBUF(str,buf) (SCM_SET_CELL_OBJECT_1(str,buf))
- #define SET_STRING_START(str,start) (SCM_SET_CELL_WORD_2(str,start))
- #define IS_STRING(str) (SCM_NIMP(str) && SCM_TYP7(str) == STRING_TAG)
- #define RO_STRING_TAG scm_tc7_ro_string
- #define IS_RO_STRING(str) (SCM_CELL_TYPE(str)==RO_STRING_TAG)
- #define SH_STRING_TAG (scm_tc7_string + 0x100)
- #define SH_STRING_STRING(sh) (SCM_CELL_OBJECT_1(sh))
- #define IS_SH_STRING(str) (SCM_CELL_TYPE(str)==SH_STRING_TAG)
- SCM scm_nullstr;
- SCM
- scm_i_make_string (size_t len, char **charsp, int read_only_p)
- {
- SCM buf = make_stringbuf (len);
- SCM res;
- if (charsp)
- *charsp = (char *) STRINGBUF_CHARS (buf);
- res = scm_double_cell (read_only_p ? RO_STRING_TAG : STRING_TAG,
- SCM_UNPACK (buf),
- (scm_t_bits) 0, (scm_t_bits) len);
- return res;
- }
- SCM
- scm_i_make_wide_string (size_t len, scm_t_wchar **charsp, int read_only_p)
- {
- SCM buf = make_wide_stringbuf (len);
- SCM res;
- if (charsp)
- *charsp = STRINGBUF_WIDE_CHARS (buf);
- res = scm_double_cell (read_only_p ? RO_STRING_TAG : STRING_TAG,
- SCM_UNPACK (buf),
- (scm_t_bits) 0, (scm_t_bits) len);
- return res;
- }
- static void
- validate_substring_args (SCM str, size_t start, size_t end)
- {
- if (!IS_STRING (str))
- scm_wrong_type_arg_msg (NULL, 0, str, "string");
- if (start > STRING_LENGTH (str))
- scm_out_of_range (NULL, scm_from_size_t (start));
- if (end > STRING_LENGTH (str) || end < start)
- scm_out_of_range (NULL, scm_from_size_t (end));
- }
- static inline void
- get_str_buf_start (SCM *str, SCM *buf, size_t *start)
- {
- *start = STRING_START (*str);
- if (IS_SH_STRING (*str))
- {
- *str = SH_STRING_STRING (*str);
- *start += STRING_START (*str);
- }
- *buf = STRING_STRINGBUF (*str);
- }
- SCM
- scm_i_substring (SCM str, size_t start, size_t end)
- {
- SCM buf;
- size_t str_start;
- get_str_buf_start (&str, &buf, &str_start);
- scm_i_pthread_mutex_lock (&stringbuf_write_mutex);
- SET_STRINGBUF_SHARED (buf);
- scm_i_pthread_mutex_unlock (&stringbuf_write_mutex);
- return scm_double_cell (STRING_TAG, SCM_UNPACK(buf),
- (scm_t_bits)str_start + start,
- (scm_t_bits) end - start);
- }
- SCM
- scm_i_substring_read_only (SCM str, size_t start, size_t end)
- {
- SCM buf;
- size_t str_start;
- get_str_buf_start (&str, &buf, &str_start);
- scm_i_pthread_mutex_lock (&stringbuf_write_mutex);
- SET_STRINGBUF_SHARED (buf);
- scm_i_pthread_mutex_unlock (&stringbuf_write_mutex);
- return scm_double_cell (RO_STRING_TAG, SCM_UNPACK(buf),
- (scm_t_bits)str_start + start,
- (scm_t_bits) end - start);
- }
- SCM
- scm_i_substring_copy (SCM str, size_t start, size_t end)
- {
- size_t len = end - start;
- SCM buf, my_buf;
- size_t str_start;
- get_str_buf_start (&str, &buf, &str_start);
- if (scm_i_is_narrow_string (str))
- {
- my_buf = make_stringbuf (len);
- memcpy (STRINGBUF_CHARS (my_buf),
- STRINGBUF_CHARS (buf) + str_start + start, len);
- }
- else
- {
- my_buf = make_wide_stringbuf (len);
- u32_cpy ((scm_t_uint32 *) STRINGBUF_WIDE_CHARS (my_buf),
- (scm_t_uint32 *) (STRINGBUF_WIDE_CHARS (buf) + str_start
- + start), len);
-
- }
- scm_remember_upto_here_1 (buf);
- return scm_double_cell (STRING_TAG, SCM_UNPACK (my_buf),
- (scm_t_bits) 0, (scm_t_bits) len);
- }
- SCM
- scm_i_substring_shared (SCM str, size_t start, size_t end)
- {
- if (start == 0 && end == STRING_LENGTH (str))
- return str;
- else
- {
- size_t len = end - start;
- if (IS_SH_STRING (str))
- {
- start += STRING_START (str);
- str = SH_STRING_STRING (str);
- }
- return scm_double_cell (SH_STRING_TAG, SCM_UNPACK(str),
- (scm_t_bits)start, (scm_t_bits) len);
- }
- }
- SCM
- scm_c_substring (SCM str, size_t start, size_t end)
- {
- validate_substring_args (str, start, end);
- return scm_i_substring (str, start, end);
- }
- SCM
- scm_c_substring_read_only (SCM str, size_t start, size_t end)
- {
- validate_substring_args (str, start, end);
- return scm_i_substring_read_only (str, start, end);
- }
- SCM
- scm_c_substring_copy (SCM str, size_t start, size_t end)
- {
- validate_substring_args (str, start, end);
- return scm_i_substring_copy (str, start, end);
- }
- SCM
- scm_c_substring_shared (SCM str, size_t start, size_t end)
- {
- validate_substring_args (str, start, end);
- return scm_i_substring_shared (str, start, end);
- }
- size_t
- scm_i_string_length (SCM str)
- {
- return STRING_LENGTH (str);
- }
- int
- scm_i_is_narrow_string (SCM str)
- {
- return !STRINGBUF_WIDE (STRING_STRINGBUF (str));
- }
- int
- scm_i_try_narrow_string (SCM str)
- {
- SET_STRING_STRINGBUF (str, narrow_stringbuf (STRING_STRINGBUF (str)));
- return scm_i_is_narrow_string (str);
- }
- const void *
- scm_i_string_data (SCM str)
- {
- SCM buf;
- size_t start;
- const char *data;
- get_str_buf_start (&str, &buf, &start);
- data = STRINGBUF_CONTENTS (buf);
- data += start * (scm_i_is_narrow_string (str) ? 1 : 4);
- return data;
- }
- const char *
- scm_i_string_chars (SCM str)
- {
- SCM buf;
- size_t start;
- get_str_buf_start (&str, &buf, &start);
- if (scm_i_is_narrow_string (str))
- return (const char *) STRINGBUF_CHARS (buf) + start;
- else
- scm_misc_error (NULL, "Invalid read access of chars of wide string: ~s",
- scm_list_1 (str));
- return NULL;
- }
- const scm_t_wchar *
- scm_i_string_wide_chars (SCM str)
- {
- SCM buf;
- size_t start;
- get_str_buf_start (&str, &buf, &start);
- if (!scm_i_is_narrow_string (str))
- return (const scm_t_wchar *) STRINGBUF_WIDE_CHARS (buf) + start;
- else
- scm_misc_error (NULL, "Invalid read access of chars of narrow string: ~s",
- scm_list_1 (str));
- }
- SCM
- scm_i_string_start_writing (SCM orig_str)
- {
- SCM buf, str = orig_str;
- size_t start;
- get_str_buf_start (&str, &buf, &start);
- if (IS_RO_STRING (str))
- scm_misc_error (NULL, "string is read-only: ~s", scm_list_1 (orig_str));
- scm_i_pthread_mutex_lock (&stringbuf_write_mutex);
- if (STRINGBUF_SHARED (buf))
- {
-
- size_t len = STRING_LENGTH (str);
- SCM new_buf;
- scm_i_pthread_mutex_unlock (&stringbuf_write_mutex);
- if (scm_i_is_narrow_string (str))
- {
- new_buf = make_stringbuf (len);
- memcpy (STRINGBUF_CHARS (new_buf),
- STRINGBUF_CHARS (buf) + STRING_START (str), len);
- }
- else
- {
- new_buf = make_wide_stringbuf (len);
- u32_cpy ((scm_t_uint32 *) STRINGBUF_WIDE_CHARS (new_buf),
- (scm_t_uint32 *) (STRINGBUF_WIDE_CHARS (buf)
- + STRING_START (str)), len);
- }
- SET_STRING_STRINGBUF (str, new_buf);
- start -= STRING_START (str);
-
- SET_STRING_START (str, 0);
- SET_STRING_STRINGBUF (str, new_buf);
- buf = new_buf;
- scm_i_pthread_mutex_lock (&stringbuf_write_mutex);
- }
- return orig_str;
- }
- char *
- scm_i_string_writable_chars (SCM str)
- {
- SCM buf;
- size_t start;
- get_str_buf_start (&str, &buf, &start);
- if (scm_i_is_narrow_string (str))
- return (char *) STRINGBUF_CHARS (buf) + start;
- else
- scm_misc_error (NULL, "Invalid write access of chars of wide string: ~s",
- scm_list_1 (str));
- return NULL;
- }
- static scm_t_wchar *
- scm_i_string_writable_wide_chars (SCM str)
- {
- SCM buf;
- size_t start;
- get_str_buf_start (&str, &buf, &start);
- if (!scm_i_is_narrow_string (str))
- return STRINGBUF_WIDE_CHARS (buf) + start;
- else
- scm_misc_error (NULL, "Invalid write access of chars of narrow string: ~s",
- scm_list_1 (str));
- }
- void
- scm_i_string_stop_writing (void)
- {
- scm_i_pthread_mutex_unlock (&stringbuf_write_mutex);
- }
- scm_t_wchar
- scm_i_string_ref (SCM str, size_t x)
- {
- if (scm_i_is_narrow_string (str))
- return (scm_t_wchar) (unsigned char) (scm_i_string_chars (str)[x]);
- else
- return scm_i_string_wide_chars (str)[x];
- }
- int
- scm_i_string_contains_char (SCM str, char ch)
- {
- size_t i;
- size_t len = scm_i_string_length (str);
- i = 0;
- if (scm_i_is_narrow_string (str))
- {
- while (i < len)
- {
- if (scm_i_string_chars (str)[i] == ch)
- return i+1;
- i++;
- }
- }
- else
- {
- while (i < len)
- {
- if (scm_i_string_wide_chars (str)[i]
- == (unsigned char) ch)
- return i+1;
- i++;
- }
- }
- return 0;
- }
- int
- scm_i_string_strcmp (SCM sstr, size_t start_x, const char *cstr)
- {
- if (scm_i_is_narrow_string (sstr))
- {
- const char *a = scm_i_string_chars (sstr) + start_x;
- const char *b = cstr;
- return strncmp (a, b, strlen(b));
- }
- else
- {
- size_t i;
- const scm_t_wchar *a = scm_i_string_wide_chars (sstr) + start_x;
- const char *b = cstr;
- for (i = 0; i < strlen (b); i++)
- {
- if (a[i] != (unsigned char) b[i])
- return 1;
- }
- }
- return 0;
- }
- void
- scm_i_string_set_x (SCM str, size_t p, scm_t_wchar chr)
- {
- if (chr > 0xFF && scm_i_is_narrow_string (str))
- SET_STRING_STRINGBUF (str, wide_stringbuf (STRING_STRINGBUF (str)));
- if (scm_i_is_narrow_string (str))
- {
- char *dst = scm_i_string_writable_chars (str);
- dst[p] = chr;
- }
- else
- {
- scm_t_wchar *dst = scm_i_string_writable_wide_chars (str);
- dst[p] = chr;
- }
- }
- #define SYMBOL_STRINGBUF SCM_CELL_OBJECT_1
- SCM
- scm_i_make_symbol (SCM name, scm_t_bits flags,
- unsigned long hash, SCM props)
- {
- SCM buf;
- size_t start = STRING_START (name);
- size_t length = STRING_LENGTH (name);
- if (IS_SH_STRING (name))
- {
- name = SH_STRING_STRING (name);
- start += STRING_START (name);
- }
- buf = SYMBOL_STRINGBUF (name);
- if (start == 0 && length == STRINGBUF_LENGTH (buf))
- {
-
- scm_i_pthread_mutex_lock (&stringbuf_write_mutex);
- SET_STRINGBUF_SHARED (buf);
- scm_i_pthread_mutex_unlock (&stringbuf_write_mutex);
- }
- else
- {
-
- if (scm_i_is_narrow_string (name))
- {
- SCM new_buf = make_stringbuf (length);
- memcpy (STRINGBUF_CHARS (new_buf),
- STRINGBUF_CHARS (buf) + start, length);
- buf = new_buf;
- }
- else
- {
- SCM new_buf = make_wide_stringbuf (length);
- u32_cpy ((scm_t_uint32 *) STRINGBUF_WIDE_CHARS (new_buf),
- (scm_t_uint32 *) STRINGBUF_WIDE_CHARS (buf) + start,
- length);
- buf = new_buf;
- }
- }
- return scm_double_cell (scm_tc7_symbol | flags, SCM_UNPACK (buf),
- (scm_t_bits) hash, SCM_UNPACK (props));
- }
- SCM
- scm_i_c_make_symbol (const char *name, size_t len,
- scm_t_bits flags, unsigned long hash, SCM props)
- {
- SCM buf = make_stringbuf (len);
- memcpy (STRINGBUF_CHARS (buf), name, len);
- return scm_immutable_double_cell (scm_tc7_symbol | flags, SCM_UNPACK (buf),
- (scm_t_bits) hash, SCM_UNPACK (props));
- }
- size_t
- scm_i_symbol_length (SCM sym)
- {
- return STRINGBUF_LENGTH (SYMBOL_STRINGBUF (sym));
- }
- size_t
- scm_c_symbol_length (SCM sym)
- #define FUNC_NAME "scm_c_symbol_length"
- {
- SCM_VALIDATE_SYMBOL (1, sym);
- return STRINGBUF_LENGTH (SYMBOL_STRINGBUF (sym));
- }
- #undef FUNC_NAME
- int
- scm_i_is_narrow_symbol (SCM sym)
- {
- SCM buf;
- buf = SYMBOL_STRINGBUF (sym);
- return !STRINGBUF_WIDE (buf);
- }
- const char *
- scm_i_symbol_chars (SCM sym)
- {
- SCM buf;
- buf = SYMBOL_STRINGBUF (sym);
- if (!STRINGBUF_WIDE (buf))
- return (const char *) STRINGBUF_CHARS (buf);
- else
- scm_misc_error (NULL, "Invalid access of chars of a wide symbol ~S",
- scm_list_1 (sym));
- }
- const scm_t_wchar *
- scm_i_symbol_wide_chars (SCM sym)
- {
- SCM buf;
- buf = SYMBOL_STRINGBUF (sym);
- if (STRINGBUF_WIDE (buf))
- return (const scm_t_wchar *) STRINGBUF_WIDE_CHARS (buf);
- else
- scm_misc_error (NULL, "Invalid access of chars of a narrow symbol ~S",
- scm_list_1 (sym));
- }
- SCM
- scm_i_symbol_substring (SCM sym, size_t start, size_t end)
- {
- SCM buf = SYMBOL_STRINGBUF (sym);
- scm_i_pthread_mutex_lock (&stringbuf_write_mutex);
- SET_STRINGBUF_SHARED (buf);
- scm_i_pthread_mutex_unlock (&stringbuf_write_mutex);
- return scm_double_cell (RO_STRING_TAG, SCM_UNPACK (buf),
- (scm_t_bits)start, (scm_t_bits) end - start);
- }
- scm_t_wchar
- scm_i_symbol_ref (SCM sym, size_t x)
- {
- if (scm_i_is_narrow_symbol (sym))
- return (scm_t_wchar) (unsigned char) (scm_i_symbol_chars (sym)[x]);
- else
- return scm_i_symbol_wide_chars (sym)[x];
- }
- SCM_DEFINE (scm_sys_string_dump, "%string-dump", 1, 0, 0, (SCM str),
- "Returns an association list containing debugging information\n"
- "for @var{str}. The association list has the following entries."
- "@table @code\n"
- "@item string\n"
- "The string itself.\n"
- "@item start\n"
- "The start index of the string into its stringbuf\n"
- "@item length\n"
- "The length of the string\n"
- "@item shared\n"
- "If this string is a substring, it returns its parent string.\n"
- "Otherwise, it returns @code{#f}\n"
- "@item read-only\n"
- "@code{#t} if the string is read-only\n"
- "@item stringbuf-chars\n"
- "A new string containing this string's stringbuf's characters\n"
- "@item stringbuf-length\n"
- "The number of characters in this stringbuf\n"
- "@item stringbuf-shared\n"
- "@code{#t} if this stringbuf is shared\n"
- "@item stringbuf-wide\n"
- "@code{#t} if this stringbuf's characters are stored in a\n"
- "32-bit buffer, or @code{#f} if they are stored in an 8-bit\n"
- "buffer\n"
- "@end table")
- #define FUNC_NAME s_scm_sys_string_dump
- {
- SCM e1, e2, e3, e4, e5, e6, e7, e8, e9;
- SCM buf;
- SCM_VALIDATE_STRING (1, str);
-
- e1 = scm_cons (scm_from_latin1_symbol ("string"),
- str);
- e2 = scm_cons (scm_from_latin1_symbol ("start"),
- scm_from_size_t (STRING_START (str)));
- e3 = scm_cons (scm_from_latin1_symbol ("length"),
- scm_from_size_t (STRING_LENGTH (str)));
- if (IS_SH_STRING (str))
- {
- e4 = scm_cons (scm_from_latin1_symbol ("shared"),
- SH_STRING_STRING (str));
- buf = STRING_STRINGBUF (SH_STRING_STRING (str));
- }
- else
- {
- e4 = scm_cons (scm_from_latin1_symbol ("shared"),
- SCM_BOOL_F);
- buf = STRING_STRINGBUF (str);
- }
- if (IS_RO_STRING (str))
- e5 = scm_cons (scm_from_latin1_symbol ("read-only"),
- SCM_BOOL_T);
- else
- e5 = scm_cons (scm_from_latin1_symbol ("read-only"),
- SCM_BOOL_F);
-
- if (!STRINGBUF_WIDE (buf))
- {
- size_t len = STRINGBUF_LENGTH (buf);
- char *cbuf;
- SCM sbc = scm_i_make_string (len, &cbuf, 0);
- memcpy (cbuf, STRINGBUF_CHARS (buf), len);
- e6 = scm_cons (scm_from_latin1_symbol ("stringbuf-chars"),
- sbc);
- }
- else
- {
- size_t len = STRINGBUF_LENGTH (buf);
- scm_t_wchar *cbuf;
- SCM sbc = scm_i_make_wide_string (len, &cbuf, 0);
- u32_cpy ((scm_t_uint32 *) cbuf,
- (scm_t_uint32 *) STRINGBUF_WIDE_CHARS (buf), len);
- e6 = scm_cons (scm_from_latin1_symbol ("stringbuf-chars"),
- sbc);
- }
- e7 = scm_cons (scm_from_latin1_symbol ("stringbuf-length"),
- scm_from_size_t (STRINGBUF_LENGTH (buf)));
- if (STRINGBUF_SHARED (buf))
- e8 = scm_cons (scm_from_latin1_symbol ("stringbuf-shared"),
- SCM_BOOL_T);
- else
- e8 = scm_cons (scm_from_latin1_symbol ("stringbuf-shared"),
- SCM_BOOL_F);
- if (STRINGBUF_WIDE (buf))
- e9 = scm_cons (scm_from_latin1_symbol ("stringbuf-wide"),
- SCM_BOOL_T);
- else
- e9 = scm_cons (scm_from_latin1_symbol ("stringbuf-wide"),
- SCM_BOOL_F);
- return scm_list_n (e1, e2, e3, e4, e5, e6, e7, e8, e9, SCM_UNDEFINED);
- }
- #undef FUNC_NAME
- SCM_DEFINE (scm_sys_symbol_dump, "%symbol-dump", 1, 0, 0, (SCM sym),
- "Returns an association list containing debugging information\n"
- "for @var{sym}. The association list has the following entries."
- "@table @code\n"
- "@item symbol\n"
- "The symbol itself\n"
- "@item hash\n"
- "Its hash value\n"
- "@item interned\n"
- "@code{#t} if it is an interned symbol\n"
- "@item stringbuf-chars\n"
- "A new string containing this symbols's stringbuf's characters\n"
- "@item stringbuf-length\n"
- "The number of characters in this stringbuf\n"
- "@item stringbuf-shared\n"
- "@code{#t} if this stringbuf is shared\n"
- "@item stringbuf-wide\n"
- "@code{#t} if this stringbuf's characters are stored in a\n"
- "32-bit buffer, or @code{#f} if they are stored in an 8-bit\n"
- "buffer\n"
- "@end table")
- #define FUNC_NAME s_scm_sys_symbol_dump
- {
- SCM e1, e2, e3, e4, e5, e6, e7;
- SCM buf;
- SCM_VALIDATE_SYMBOL (1, sym);
- e1 = scm_cons (scm_from_latin1_symbol ("symbol"),
- sym);
- e2 = scm_cons (scm_from_latin1_symbol ("hash"),
- scm_from_ulong (scm_i_symbol_hash (sym)));
- e3 = scm_cons (scm_from_latin1_symbol ("interned"),
- scm_symbol_interned_p (sym));
- buf = SYMBOL_STRINGBUF (sym);
-
- if (!STRINGBUF_WIDE (buf))
- {
- size_t len = STRINGBUF_LENGTH (buf);
- char *cbuf;
- SCM sbc = scm_i_make_string (len, &cbuf, 0);
- memcpy (cbuf, STRINGBUF_CHARS (buf), len);
- e4 = scm_cons (scm_from_latin1_symbol ("stringbuf-chars"),
- sbc);
- }
- else
- {
- size_t len = STRINGBUF_LENGTH (buf);
- scm_t_wchar *cbuf;
- SCM sbc = scm_i_make_wide_string (len, &cbuf, 0);
- u32_cpy ((scm_t_uint32 *) cbuf,
- (scm_t_uint32 *) STRINGBUF_WIDE_CHARS (buf), len);
- e4 = scm_cons (scm_from_latin1_symbol ("stringbuf-chars"),
- sbc);
- }
- e5 = scm_cons (scm_from_latin1_symbol ("stringbuf-length"),
- scm_from_size_t (STRINGBUF_LENGTH (buf)));
- if (STRINGBUF_SHARED (buf))
- e6 = scm_cons (scm_from_latin1_symbol ("stringbuf-shared"),
- SCM_BOOL_T);
- else
- e6 = scm_cons (scm_from_latin1_symbol ("stringbuf-shared"),
- SCM_BOOL_F);
- if (STRINGBUF_WIDE (buf))
- e7 = scm_cons (scm_from_latin1_symbol ("stringbuf-wide"),
- SCM_BOOL_T);
- else
- e7 = scm_cons (scm_from_latin1_symbol ("stringbuf-wide"),
- SCM_BOOL_F);
- return scm_list_n (e1, e2, e3, e4, e5, e6, e7, SCM_UNDEFINED);
- }
- #undef FUNC_NAME
- #ifdef SCM_STRING_LENGTH_HISTOGRAM
- SCM_DEFINE (scm_sys_stringbuf_hist, "%stringbuf-hist", 0, 0, 0, (void), "")
- #define FUNC_NAME s_scm_sys_stringbuf_hist
- {
- int i;
- for (i = 0; i < 1000; i++)
- if (lenhist[i])
- fprintf (stderr, " %3d: %u\n", i, lenhist[i]);
- fprintf (stderr, ">999: %u\n", lenhist[1000]);
- return SCM_UNSPECIFIED;
- }
- #undef FUNC_NAME
- #endif
- SCM_DEFINE (scm_string_p, "string?", 1, 0, 0,
- (SCM obj),
- "Return @code{#t} if @var{obj} is a string, else @code{#f}.")
- #define FUNC_NAME s_scm_string_p
- {
- return scm_from_bool (IS_STRING (obj));
- }
- #undef FUNC_NAME
- SCM_REGISTER_PROC (s_scm_list_to_string, "list->string", 1, 0, 0, scm_string);
- SCM_DEFINE (scm_string, "string", 0, 0, 1,
- (SCM chrs),
- "@deffnx {Scheme Procedure} list->string chrs\n"
- "Return a newly allocated string composed of the arguments,\n"
- "@var{chrs}.")
- #define FUNC_NAME s_scm_string
- {
- SCM result = SCM_BOOL_F;
- SCM rest;
- size_t len;
- size_t p = 0;
- long i;
- int wide = 0;
-
- i = scm_ilength (chrs);
- SCM_ASSERT (i >= 0, chrs, SCM_ARG1, FUNC_NAME);
- len = (size_t) i;
- rest = chrs;
- while (len > 0 && scm_is_pair (rest))
- {
- SCM elt = SCM_CAR (rest);
- SCM_VALIDATE_CHAR (SCM_ARGn, elt);
- if (SCM_CHAR (elt) > 0xFF)
- wide = 1;
- rest = SCM_CDR (rest);
- len--;
- scm_remember_upto_here_1 (elt);
- }
-
- len = (size_t) i;
- rest = chrs;
- if (wide == 0)
- {
- char *buf;
- result = scm_i_make_string (len, NULL, 0);
- result = scm_i_string_start_writing (result);
- buf = scm_i_string_writable_chars (result);
- while (len > 0 && scm_is_pair (rest))
- {
- SCM elt = SCM_CAR (rest);
- buf[p] = (unsigned char) SCM_CHAR (elt);
- p++;
- rest = SCM_CDR (rest);
- len--;
- scm_remember_upto_here_1 (elt);
- }
- }
- else
- {
- scm_t_wchar *buf;
- result = scm_i_make_wide_string (len, NULL, 0);
- result = scm_i_string_start_writing (result);
- buf = scm_i_string_writable_wide_chars (result);
- while (len > 0 && scm_is_pair (rest))
- {
- SCM elt = SCM_CAR (rest);
- buf[p] = SCM_CHAR (elt);
- p++;
- rest = SCM_CDR (rest);
- len--;
- scm_remember_upto_here_1 (elt);
- }
- }
- scm_i_string_stop_writing ();
- if (len > 0)
- scm_misc_error (NULL, "list changed while constructing string", SCM_EOL);
- if (!scm_is_null (rest))
- scm_wrong_type_arg_msg (NULL, 0, chrs, "proper list");
- return result;
- }
- #undef FUNC_NAME
- SCM_DEFINE (scm_make_string, "make-string", 1, 1, 0,
- (SCM k, SCM chr),
- "Return a newly allocated string of\n"
- "length @var{k}. If @var{chr} is given, then all elements of\n"
- "the string are initialized to @var{chr}, otherwise the contents\n"
- "of the @var{string} are all set to @var{#\nul}.")
- #define FUNC_NAME s_scm_make_string
- {
- return scm_c_make_string (scm_to_size_t (k), chr);
- }
- #undef FUNC_NAME
- SCM
- scm_c_make_string (size_t len, SCM chr)
- #define FUNC_NAME NULL
- {
- size_t p;
- char *contents = NULL;
- SCM res = scm_i_make_string (len, &contents, 0);
-
- if (SCM_UNBNDP (chr))
- memset (contents, 0, len);
- else
- {
- SCM_VALIDATE_CHAR (0, chr);
- res = scm_i_string_start_writing (res);
- for (p = 0; p < len; p++)
- scm_i_string_set_x (res, p, SCM_CHAR (chr));
- scm_i_string_stop_writing ();
- }
- return res;
- }
- #undef FUNC_NAME
- SCM_DEFINE (scm_string_length, "string-length", 1, 0, 0,
- (SCM string),
- "Return the number of characters in @var{string}.")
- #define FUNC_NAME s_scm_string_length
- {
- SCM_VALIDATE_STRING (1, string);
- return scm_from_size_t (STRING_LENGTH (string));
- }
- #undef FUNC_NAME
- SCM_DEFINE (scm_string_bytes_per_char, "string-bytes-per-char", 1, 0, 0,
- (SCM string),
- "Return the bytes used to represent a character in @var{string}."
- "This will return 1 or 4.")
- #define FUNC_NAME s_scm_string_bytes_per_char
- {
- SCM_VALIDATE_STRING (1, string);
- if (!scm_i_is_narrow_string (string))
- return scm_from_int (4);
- return scm_from_int (1);
- }
- #undef FUNC_NAME
- size_t
- scm_c_string_length (SCM string)
- {
- if (!IS_STRING (string))
- scm_wrong_type_arg_msg (NULL, 0, string, "string");
- return STRING_LENGTH (string);
- }
- SCM_DEFINE (scm_string_ref, "string-ref", 2, 0, 0,
- (SCM str, SCM k),
- "Return character @var{k} of @var{str} using zero-origin\n"
- "indexing. @var{k} must be a valid index of @var{str}.")
- #define FUNC_NAME s_scm_string_ref
- {
- size_t len;
- unsigned long idx;
- SCM_VALIDATE_STRING (1, str);
- len = scm_i_string_length (str);
- if (SCM_LIKELY (len > 0))
- idx = scm_to_unsigned_integer (k, 0, len - 1);
- else
- scm_out_of_range (NULL, k);
- if (scm_i_is_narrow_string (str))
- return SCM_MAKE_CHAR (scm_i_string_chars (str)[idx]);
- else
- return SCM_MAKE_CHAR (scm_i_string_wide_chars (str)[idx]);
- }
- #undef FUNC_NAME
- SCM
- scm_c_string_ref (SCM str, size_t p)
- {
- if (p >= scm_i_string_length (str))
- scm_out_of_range (NULL, scm_from_size_t (p));
- if (scm_i_is_narrow_string (str))
- return SCM_MAKE_CHAR (scm_i_string_chars (str)[p]);
- else
- return SCM_MAKE_CHAR (scm_i_string_wide_chars (str)[p]);
- }
- SCM_DEFINE (scm_string_set_x, "string-set!", 3, 0, 0,
- (SCM str, SCM k, SCM chr),
- "Store @var{chr} in element @var{k} of @var{str} and return\n"
- "an unspecified value. @var{k} must be a valid index of\n"
- "@var{str}.")
- #define FUNC_NAME s_scm_string_set_x
- {
- size_t len;
- unsigned long idx;
- SCM_VALIDATE_STRING (1, str);
- len = scm_i_string_length (str);
- if (SCM_LIKELY (len > 0))
- idx = scm_to_unsigned_integer (k, 0, len - 1);
- else
- scm_out_of_range (NULL, k);
- SCM_VALIDATE_CHAR (3, chr);
- str = scm_i_string_start_writing (str);
- scm_i_string_set_x (str, idx, SCM_CHAR (chr));
- scm_i_string_stop_writing ();
- return SCM_UNSPECIFIED;
- }
- #undef FUNC_NAME
- void
- scm_c_string_set_x (SCM str, size_t p, SCM chr)
- {
- if (p >= scm_i_string_length (str))
- scm_out_of_range (NULL, scm_from_size_t (p));
- str = scm_i_string_start_writing (str);
- scm_i_string_set_x (str, p, SCM_CHAR (chr));
- scm_i_string_stop_writing ();
- }
- SCM_DEFINE (scm_substring, "substring", 2, 1, 0,
- (SCM str, SCM start, SCM end),
- "Return a newly allocated string formed from the characters\n"
- "of @var{str} beginning with index @var{start} (inclusive) and\n"
- "ending with index @var{end} (exclusive).\n"
- "@var{str} must be a string, @var{start} and @var{end} must be\n"
- "exact integers satisfying:\n\n"
- "0 <= @var{start} <= @var{end} <= (string-length @var{str}).")
- #define FUNC_NAME s_scm_substring
- {
- size_t len, from, to;
- SCM_VALIDATE_STRING (1, str);
- len = scm_i_string_length (str);
- from = scm_to_unsigned_integer (start, 0, len);
- if (SCM_UNBNDP (end))
- to = len;
- else
- to = scm_to_unsigned_integer (end, from, len);
- return scm_i_substring (str, from, to);
- }
- #undef FUNC_NAME
- SCM_DEFINE (scm_substring_read_only, "substring/read-only", 2, 1, 0,
- (SCM str, SCM start, SCM end),
- "Return a newly allocated string formed from the characters\n"
- "of @var{str} beginning with index @var{start} (inclusive) and\n"
- "ending with index @var{end} (exclusive).\n"
- "@var{str} must be a string, @var{start} and @var{end} must be\n"
- "exact integers satisfying:\n"
- "\n"
- "0 <= @var{start} <= @var{end} <= (string-length @var{str}).\n"
- "\n"
- "The returned string is read-only.\n")
- #define FUNC_NAME s_scm_substring_read_only
- {
- size_t len, from, to;
- SCM_VALIDATE_STRING (1, str);
- len = scm_i_string_length (str);
- from = scm_to_unsigned_integer (start, 0, len);
- if (SCM_UNBNDP (end))
- to = len;
- else
- to = scm_to_unsigned_integer (end, from, len);
- return scm_i_substring_read_only (str, from, to);
- }
- #undef FUNC_NAME
- SCM_DEFINE (scm_substring_copy, "substring/copy", 2, 1, 0,
- (SCM str, SCM start, SCM end),
- "Return a newly allocated string formed from the characters\n"
- "of @var{str} beginning with index @var{start} (inclusive) and\n"
- "ending with index @var{end} (exclusive).\n"
- "@var{str} must be a string, @var{start} and @var{end} must be\n"
- "exact integers satisfying:\n\n"
- "0 <= @var{start} <= @var{end} <= (string-length @var{str}).")
- #define FUNC_NAME s_scm_substring_copy
- {
-
- size_t from, to;
- SCM_VALIDATE_STRING (1, str);
- scm_i_get_substring_spec (scm_i_string_length (str),
- start, &from, end, &to);
- return scm_i_substring_copy (str, from, to);
- }
- #undef FUNC_NAME
- SCM_DEFINE (scm_substring_shared, "substring/shared", 2, 1, 0,
- (SCM str, SCM start, SCM end),
- "Return string that indirectly refers to the characters\n"
- "of @var{str} beginning with index @var{start} (inclusive) and\n"
- "ending with index @var{end} (exclusive).\n"
- "@var{str} must be a string, @var{start} and @var{end} must be\n"
- "exact integers satisfying:\n\n"
- "0 <= @var{start} <= @var{end} <= (string-length @var{str}).")
- #define FUNC_NAME s_scm_substring_shared
- {
- size_t len, from, to;
- SCM_VALIDATE_STRING (1, str);
- len = scm_i_string_length (str);
- from = scm_to_unsigned_integer (start, 0, len);
- if (SCM_UNBNDP (end))
- to = len;
- else
- to = scm_to_unsigned_integer (end, from, len);
- return scm_i_substring_shared (str, from, to);
- }
- #undef FUNC_NAME
- SCM_DEFINE (scm_string_append, "string-append", 0, 0, 1,
- (SCM args),
- "Return a newly allocated string whose characters form the\n"
- "concatenation of the given strings, @var{args}.")
- #define FUNC_NAME s_scm_string_append
- {
- SCM res;
- size_t len = 0;
- int wide = 0;
- SCM l, s;
- size_t i;
- union
- {
- char *narrow;
- scm_t_wchar *wide;
- } data;
- SCM_VALIDATE_REST_ARGUMENT (args);
- for (l = args; !scm_is_null (l); l = SCM_CDR (l))
- {
- s = SCM_CAR (l);
- SCM_VALIDATE_STRING (SCM_ARGn, s);
- len += scm_i_string_length (s);
- if (!scm_i_is_narrow_string (s))
- wide = 1;
- }
- data.narrow = NULL;
- if (!wide)
- res = scm_i_make_string (len, &data.narrow, 0);
- else
- res = scm_i_make_wide_string (len, &data.wide, 0);
- for (l = args; !scm_is_null (l); l = SCM_CDR (l))
- {
- size_t len;
- s = SCM_CAR (l);
- SCM_VALIDATE_STRING (SCM_ARGn, s);
- len = scm_i_string_length (s);
- if (!wide)
- {
- memcpy (data.narrow, scm_i_string_chars (s), len);
- data.narrow += len;
- }
- else
- {
- if (scm_i_is_narrow_string (s))
- {
- for (i = 0; i < scm_i_string_length (s); i++)
- data.wide[i] = (unsigned char) scm_i_string_chars (s)[i];
- }
- else
- u32_cpy ((scm_t_uint32 *) data.wide,
- (scm_t_uint32 *) scm_i_string_wide_chars (s), len);
- data.wide += len;
- }
- scm_remember_upto_here_1 (s);
- }
- return res;
- }
- #undef FUNC_NAME
- SCM_SYMBOL (scm_encoding_error_key, "encoding-error");
- SCM_SYMBOL (scm_decoding_error_key, "decoding-error");
- void
- scm_encoding_error (const char *subr, int err, const char *message,
- SCM port, SCM chr)
- {
- scm_throw (scm_encoding_error_key,
- scm_list_n (scm_from_latin1_string (subr),
- scm_from_latin1_string (message),
- scm_from_int (err),
- port, chr,
- SCM_UNDEFINED));
- }
- void
- scm_decoding_error (const char *subr, int err, const char *message, SCM port)
- {
- scm_throw (scm_decoding_error_key,
- scm_list_n (scm_from_latin1_string (subr),
- scm_from_latin1_string (message),
- scm_from_int (err),
- port,
- SCM_UNDEFINED));
- }
- SCM
- scm_from_stringn (const char *str, size_t len, const char *encoding,
- scm_t_string_failed_conversion_handler handler)
- {
- size_t u32len, i;
- scm_t_wchar *u32;
- int wide = 0;
- SCM res;
-
- if (!str && len != 0)
- scm_misc_error ("scm_from_stringn", "NULL string pointer", SCM_EOL);
- if (len == (size_t) -1)
- len = strlen (str);
- if (len == 0)
- return scm_nullstr;
- if (encoding == NULL)
- {
-
- char *buf;
- res = scm_i_make_string (len, &buf, 0);
- memcpy (buf, str, len);
- return res;
- }
- u32len = 0;
- u32 = (scm_t_wchar *) u32_conv_from_encoding (encoding,
- (enum iconv_ilseq_handler)
- handler,
- str, len,
- NULL,
- NULL, &u32len);
- if (SCM_UNLIKELY (u32 == NULL))
- {
-
- SCM bv;
- signed char *buf;
- buf = scm_gc_malloc_pointerless (len, "bytevector");
- memcpy (buf, str, len);
- bv = scm_c_take_bytevector (buf, len, SCM_BOOL_F);
- scm_decoding_error (__func__, errno,
- "input locale conversion error", bv);
- }
- i = 0;
- while (i < u32len)
- if (u32[i++] > 0xFF)
- {
- wide = 1;
- break;
- }
- if (!wide)
- {
- char *dst;
- res = scm_i_make_string (u32len, &dst, 0);
- for (i = 0; i < u32len; i ++)
- dst[i] = (unsigned char) u32[i];
- dst[u32len] = '\0';
- }
- else
- {
- scm_t_wchar *wdst;
- res = scm_i_make_wide_string (u32len, &wdst, 0);
- u32_cpy ((scm_t_uint32 *) wdst, (scm_t_uint32 *) u32, u32len);
- wdst[u32len] = 0;
- }
- free (u32);
- return res;
- }
- SCM
- scm_from_locale_string (const char *str)
- {
- return scm_from_locale_stringn (str, -1);
- }
- SCM
- scm_from_locale_stringn (const char *str, size_t len)
- {
- return scm_from_stringn (str, len, locale_charset (),
- scm_i_get_conversion_strategy (SCM_BOOL_F));
- }
- SCM
- scm_from_latin1_string (const char *str)
- {
- return scm_from_latin1_stringn (str, -1);
- }
- SCM
- scm_from_latin1_stringn (const char *str, size_t len)
- {
- char *buf;
- SCM result;
- if (len == (size_t) -1)
- len = strlen (str);
-
- result = scm_i_make_string (len, &buf, 0);
- memcpy (buf, str, len);
- return result;
- }
- SCM
- scm_from_utf8_string (const char *str)
- {
- return scm_from_utf8_stringn (str, -1);
- }
- SCM
- scm_from_utf8_stringn (const char *str, size_t len)
- {
- return scm_from_stringn (str, len, "UTF-8", SCM_FAILED_CONVERSION_ERROR);
- }
- SCM
- scm_from_utf32_string (const scm_t_wchar *str)
- {
- return scm_from_utf32_stringn (str, -1);
- }
- SCM
- scm_from_utf32_stringn (const scm_t_wchar *str, size_t len)
- {
- SCM result;
- scm_t_wchar *buf;
- if (len == (size_t) -1)
- len = u32_strlen ((uint32_t *) str);
- result = scm_i_make_wide_string (len, &buf, 0);
- memcpy (buf, str, len * sizeof (scm_t_wchar));
- scm_i_try_narrow_string (result);
- return result;
- }
- SCM
- scm_take_locale_stringn (char *str, size_t len)
- {
- SCM res;
- res = scm_from_locale_stringn (str, len);
- free (str);
- return res;
- }
- SCM
- scm_take_locale_string (char *str)
- {
- return scm_take_locale_stringn (str, -1);
- }
- static void
- unistring_escapes_to_guile_escapes (char *buf, size_t *lenp)
- {
- char *before, *after;
- size_t i, j;
- before = buf;
- after = buf;
- i = 0;
- j = 0;
- while (i < *lenp)
- {
- if ((i <= *lenp - 6)
- && before[i] == '\\'
- && before[i + 1] == 'u'
- && before[i + 2] == '0' && before[i + 3] == '0')
- {
-
- after[j] = '\\';
- after[j + 1] = 'x';
- after[j + 2] = tolower ((int) before[i + 4]);
- after[j + 3] = tolower ((int) before[i + 5]);
- i += 6;
- j += 4;
- }
- else if ((i <= *lenp - 10)
- && before[i] == '\\'
- && before[i + 1] == 'U'
- && before[i + 2] == '0' && before[i + 3] == '0')
- {
-
- after[j] = '\\';
- after[j + 1] = 'U';
- after[j + 2] = tolower ((int) before[i + 4]);
- after[j + 3] = tolower ((int) before[i + 5]);
- after[j + 4] = tolower ((int) before[i + 6]);
- after[j + 5] = tolower ((int) before[i + 7]);
- after[j + 6] = tolower ((int) before[i + 8]);
- after[j + 7] = tolower ((int) before[i + 9]);
- i += 10;
- j += 8;
- }
- else
- {
- after[j] = before[i];
- i++;
- j++;
- }
- }
- *lenp = j;
- }
- static void
- unistring_escapes_to_r6rs_escapes (char *buf, size_t *lenp)
- {
- char *before, *after;
- size_t i, j;
-
- size_t max_out_len = (*lenp * 7) / 6 + 1;
- size_t nzeros, ndigits;
- before = buf;
- after = alloca (max_out_len);
- i = 0;
- j = 0;
- while (i < *lenp)
- {
- if (((i <= *lenp - 6) && before[i] == '\\' && before[i + 1] == 'u')
- || ((i <= *lenp - 10) && before[i] == '\\' && before[i + 1] == 'U'))
- {
- if (before[i + 1] == 'u')
- ndigits = 4;
- else if (before[i + 1] == 'U')
- ndigits = 8;
- else
- abort ();
-
- after[j] = '\\';
- after[j + 1] = 'x';
-
- i += 2;
- j += 2;
-
- nzeros = 0;
- while (before[i + nzeros] == '0' && nzeros < ndigits)
- nzeros++;
-
- if (nzeros == ndigits)
- {
- after[j] = '0';
- i += ndigits;
- j += 1;
- }
- else
- {
- int pos;
- for (pos = 0; pos < ndigits - nzeros; pos++)
- after[j + pos] = tolower ((int) before[i + nzeros + pos]);
- i += ndigits;
- j += (ndigits - nzeros);
- }
-
- after[j] = ';';
- j++;
- }
- else
- {
- after[j] = before[i];
- i++;
- j++;
- }
- }
- *lenp = j;
- memcpy (before, after, j);
- }
- char *
- scm_to_locale_string (SCM str)
- {
- return scm_to_locale_stringn (str, NULL);
- }
- char *
- scm_to_locale_stringn (SCM str, size_t *lenp)
- {
- return scm_to_stringn (str, lenp,
- locale_charset (),
- scm_i_get_conversion_strategy (SCM_BOOL_F));
- }
- char *
- scm_to_latin1_string (SCM str)
- {
- return scm_to_latin1_stringn (str, NULL);
- }
- char *
- scm_to_latin1_stringn (SCM str, size_t *lenp)
- #define FUNC_NAME "scm_to_latin1_stringn"
- {
- char *result;
- SCM_VALIDATE_STRING (1, str);
- if (scm_i_is_narrow_string (str))
- {
- if (lenp)
- *lenp = scm_i_string_length (str);
- result = scm_strdup (scm_i_string_data (str));
- }
- else
- result = scm_to_stringn (str, lenp, NULL,
- SCM_FAILED_CONVERSION_ERROR);
- return result;
- }
- #undef FUNC_NAME
- char *
- scm_to_utf8_string (SCM str)
- {
- return scm_to_utf8_stringn (str, NULL);
- }
- char *
- scm_to_utf8_stringn (SCM str, size_t *lenp)
- {
- return scm_to_stringn (str, lenp, "UTF-8", SCM_FAILED_CONVERSION_ERROR);
- }
- scm_t_wchar *
- scm_to_utf32_string (SCM str)
- {
- return scm_to_utf32_stringn (str, NULL);
- }
- scm_t_wchar *
- scm_to_utf32_stringn (SCM str, size_t *lenp)
- #define FUNC_NAME "scm_to_utf32_stringn"
- {
- scm_t_wchar *result;
- SCM_VALIDATE_STRING (1, str);
- if (scm_i_is_narrow_string (str))
- result = (scm_t_wchar *)
- scm_to_stringn (str, lenp, "UTF-32",
- SCM_FAILED_CONVERSION_ERROR);
- else
- {
- size_t len;
- len = scm_i_string_length (str);
- if (lenp)
- *lenp = len;
- result = scm_malloc ((len + 1) * sizeof (scm_t_wchar));
- memcpy (result, scm_i_string_wide_chars (str),
- len * sizeof (scm_t_wchar));
- result[len] = 0;
- }
- return result;
- }
- #undef FUNC_NAME
- char *
- scm_to_stringn (SCM str, size_t *lenp, const char *encoding,
- scm_t_string_failed_conversion_handler handler)
- {
- char *buf;
- size_t ilen, len, i;
- int ret;
- const char *enc;
- if (!scm_is_string (str))
- scm_wrong_type_arg_msg (NULL, 0, str, "string");
- ilen = scm_i_string_length (str);
- if (ilen == 0)
- {
- buf = scm_malloc (1);
- buf[0] = '\0';
- if (lenp)
- *lenp = 0;
- return buf;
- }
- if (lenp == NULL)
- for (i = 0; i < ilen; i++)
- if (scm_i_string_ref (str, i) == '\0')
- scm_misc_error (NULL,
- "string contains #\\nul character: ~S",
- scm_list_1 (str));
- if (scm_i_is_narrow_string (str) && (encoding == NULL))
- {
-
- if (lenp)
- {
- buf = scm_malloc (ilen);
- memcpy (buf, scm_i_string_chars (str), ilen);
- *lenp = ilen;
- return buf;
- }
- else
- {
- buf = scm_malloc (ilen + 1);
- memcpy (buf, scm_i_string_chars (str), ilen);
- buf[ilen] = '\0';
- return buf;
- }
- }
- buf = NULL;
- len = 0;
- enc = encoding;
- if (enc == NULL)
- enc = "ISO-8859-1";
- if (scm_i_is_narrow_string (str))
- {
- ret = mem_iconveh (scm_i_string_chars (str), ilen,
- "ISO-8859-1", enc,
- (enum iconv_ilseq_handler) handler, NULL,
- &buf, &len);
- if (ret != 0)
- scm_encoding_error (__func__, errno,
- "cannot convert narrow string to output locale",
- SCM_BOOL_F,
-
- SCM_BOOL_F);
- }
- else
- {
- buf = u32_conv_to_encoding (enc,
- (enum iconv_ilseq_handler) handler,
- (scm_t_uint32 *) scm_i_string_wide_chars (str),
- ilen,
- NULL,
- NULL, &len);
- if (buf == NULL)
- scm_encoding_error (__func__, errno,
- "cannot convert wide string to output locale",
- SCM_BOOL_F,
-
- SCM_BOOL_F);
- }
- if (handler == SCM_FAILED_CONVERSION_ESCAPE_SEQUENCE)
- {
- if (SCM_R6RS_ESCAPES_P)
- {
-
- buf = scm_realloc (buf, (len * 7) / 6 + 1);
- unistring_escapes_to_r6rs_escapes (buf, &len);
- }
- else
- unistring_escapes_to_guile_escapes (buf, &len);
- buf = scm_realloc (buf, len);
- }
- if (lenp)
- *lenp = len;
- else
- {
- buf = scm_realloc (buf, len + 1);
- buf[len] = '\0';
- }
- scm_remember_upto_here_1 (str);
- return buf;
- }
- size_t
- scm_to_locale_stringbuf (SCM str, char *buf, size_t max_len)
- {
- size_t len;
- char *result = NULL;
- if (!scm_is_string (str))
- scm_wrong_type_arg_msg (NULL, 0, str, "string");
- result = scm_to_locale_stringn (str, &len);
- memcpy (buf, result, (len > max_len) ? max_len : len);
- free (result);
- scm_remember_upto_here_1 (str);
- return len;
- }
- static SCM
- normalize_str (SCM string, uninorm_t form)
- {
- SCM ret;
- scm_t_uint32 *w_str;
- scm_t_wchar *cbuf;
- size_t rlen, len = scm_i_string_length (string);
-
- if (scm_i_is_narrow_string (string))
- {
- size_t i;
- const char *buf = scm_i_string_chars (string);
-
- w_str = alloca (sizeof (scm_t_wchar) * (len + 1));
-
- for (i = 0; i < len; i ++)
- w_str[i] = (unsigned char) buf[i];
- w_str[len] = 0;
- }
- else
- w_str = (scm_t_uint32 *) scm_i_string_wide_chars (string);
- w_str = u32_normalize (form, w_str, len, NULL, &rlen);
-
- ret = scm_i_make_wide_string (rlen, &cbuf, 0);
- u32_cpy ((scm_t_uint32 *) cbuf, w_str, rlen);
- free (w_str);
- scm_i_try_narrow_string (ret);
- return ret;
- }
- SCM_DEFINE (scm_string_normalize_nfc, "string-normalize-nfc", 1, 0, 0,
- (SCM string),
- "Returns the NFC normalized form of @var{string}.")
- #define FUNC_NAME s_scm_string_normalize_nfc
- {
- SCM_VALIDATE_STRING (1, string);
- return normalize_str (string, UNINORM_NFC);
- }
- #undef FUNC_NAME
- SCM_DEFINE (scm_string_normalize_nfd, "string-normalize-nfd", 1, 0, 0,
- (SCM string),
- "Returns the NFD normalized form of @var{string}.")
- #define FUNC_NAME s_scm_string_normalize_nfd
- {
- SCM_VALIDATE_STRING (1, string);
- return normalize_str (string, UNINORM_NFD);
- }
- #undef FUNC_NAME
- SCM_DEFINE (scm_string_normalize_nfkc, "string-normalize-nfkc", 1, 0, 0,
- (SCM string),
- "Returns the NFKC normalized form of @var{string}.")
- #define FUNC_NAME s_scm_string_normalize_nfkc
- {
- SCM_VALIDATE_STRING (1, string);
- return normalize_str (string, UNINORM_NFKC);
- }
- #undef FUNC_NAME
- SCM_DEFINE (scm_string_normalize_nfkd, "string-normalize-nfkd", 1, 0, 0,
- (SCM string),
- "Returns the NFKD normalized form of @var{string}.")
- #define FUNC_NAME s_scm_string_normalize_nfkd
- {
- SCM_VALIDATE_STRING (1, string);
- return normalize_str (string, UNINORM_NFKD);
- }
- #undef FUNC_NAME
- SCM
- scm_makfromstrs (int argc, char **argv)
- {
- int i = argc;
- SCM lst = SCM_EOL;
- if (0 > i)
- for (i = 0; argv[i]; i++);
- while (i--)
- lst = scm_cons (scm_from_locale_string (argv[i]), lst);
- return lst;
- }
- char **
- scm_i_allocate_string_pointers (SCM list)
- #define FUNC_NAME "scm_i_allocate_string_pointers"
- {
- char **result;
- int len = scm_ilength (list);
- int i;
- if (len < 0)
- scm_wrong_type_arg_msg (NULL, 0, list, "proper list");
- result = scm_gc_malloc ((len + 1) * sizeof (char *),
- "string pointers");
- result[len] = NULL;
-
- for (i = 0; i < len && scm_is_pair (list); i++)
- {
- SCM str;
- size_t len;
- str = SCM_CAR (list);
- len = scm_c_string_length (str);
- result[i] = scm_gc_malloc_pointerless (len + 1, "string pointers");
- memcpy (result[i], scm_i_string_chars (str), len);
- result[i][len] = '\0';
- list = SCM_CDR (list);
- }
- return result;
- }
- #undef FUNC_NAME
- void
- scm_i_get_substring_spec (size_t len,
- SCM start, size_t *cstart,
- SCM end, size_t *cend)
- {
- if (SCM_UNBNDP (start))
- *cstart = 0;
- else
- *cstart = scm_to_unsigned_integer (start, 0, len);
- if (SCM_UNBNDP (end))
- *cend = len;
- else
- *cend = scm_to_unsigned_integer (end, *cstart, len);
- }
-
- static SCM
- string_handle_ref (scm_t_array_handle *h, size_t index)
- {
- return scm_c_string_ref (h->array, index);
- }
- static void
- string_handle_set (scm_t_array_handle *h, size_t index, SCM val)
- {
- scm_c_string_set_x (h->array, index, val);
- }
- static void
- string_get_handle (SCM v, scm_t_array_handle *h)
- {
- h->array = v;
- h->ndims = 1;
- h->dims = &h->dim0;
- h->dim0.lbnd = 0;
- h->dim0.ubnd = scm_c_string_length (v) - 1;
- h->dim0.inc = 1;
- h->element_type = SCM_ARRAY_ELEMENT_TYPE_CHAR;
- h->elements = h->writable_elements = NULL;
- }
- SCM_ARRAY_IMPLEMENTATION (scm_tc7_string, 0x7f,
- string_handle_ref, string_handle_set,
- string_get_handle)
- SCM_VECTOR_IMPLEMENTATION (SCM_ARRAY_ELEMENT_TYPE_CHAR, scm_make_string)
- void
- scm_init_strings ()
- {
- scm_nullstr = scm_i_make_string (0, NULL, 1);
- #include "libguile/strings.x"
- }
|