PageRenderTime 93ms CodeModel.GetById 37ms RepoModel.GetById 0ms app.codeStats 1ms

/src/racket/src/string.c

http://github.com/4z3/racket
C | 5644 lines | 4619 code | 681 blank | 344 comment | 1048 complexity | fbd548973c5671534bdc00809fd405bb MD5 | raw file
Possible License(s): LGPL-2.0, GPL-2.0, BSD-3-Clause
  1. /*
  2. Racket
  3. Copyright (c) 2004-2010 PLT Scheme Inc.
  4. Copyright (c) 1995-2001 Matthew Flatt
  5. This library is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU Library General Public
  7. License as published by the Free Software Foundation; either
  8. version 2 of the License, or (at your option) any later version.
  9. This library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. Library General Public License for more details.
  13. You should have received a copy of the GNU Library General Public
  14. License along with this library; if not, write to the Free
  15. Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  16. Boston, MA 02110-1301 USA.
  17. libscheme
  18. Copyright (c) 1994 Brent Benson
  19. All rights reserved.
  20. */
  21. #include "schpriv.h"
  22. #include "schvers.h"
  23. #include <string.h>
  24. #include <ctype.h>
  25. #ifndef DONT_USE_LOCALE
  26. # include <locale.h>
  27. # ifdef MZ_NO_ICONV
  28. # define USE_ICONV_DLL
  29. # endif
  30. # ifndef USE_ICONV_DLL
  31. # include <iconv.h>
  32. # include <langinfo.h>
  33. # endif
  34. # include <wchar.h>
  35. # include <wctype.h>
  36. # include <errno.h>
  37. # ifdef MACOS_UNICODE_SUPPORT
  38. # include <CoreFoundation/CFString.h>
  39. # include <CoreFoundation/CFLocale.h>
  40. # endif
  41. # ifdef WINDOWS_UNICODE_SUPPORT
  42. # include <windows.h>
  43. # endif
  44. #endif
  45. #ifndef SCHEME_PLATFORM_LIBRARY_SUBPATH
  46. # include "schsys.h"
  47. #endif
  48. #include "schustr.inc"
  49. #ifdef USE_ICONV_DLL
  50. typedef intptr_t iconv_t;
  51. typedef int *(*errno_proc_t)();
  52. typedef size_t (*iconv_proc_t)(iconv_t cd,
  53. char **inbuf, size_t *inbytesleft,
  54. char **outbuf, size_t *outbytesleft);
  55. typedef iconv_t (*iconv_open_proc_t)(const char *tocode, const char *fromcode);
  56. typedef void (*iconv_close_proc_t)(iconv_t cd);
  57. typedef char *(*locale_charset_proc_t)();
  58. static errno_proc_t iconv_errno;
  59. static iconv_proc_t iconv;
  60. static iconv_open_proc_t iconv_open;
  61. static iconv_close_proc_t iconv_close;
  62. static locale_charset_proc_t locale_charset; /* Not used, currently */
  63. #define mzCHK_PROC(x) x
  64. static int get_iconv_errno(void)
  65. {
  66. int *a;
  67. a = iconv_errno();
  68. return *a;
  69. }
  70. # undef HAVE_CODESET
  71. # define HAVE_CODESET 1
  72. # define CODESET 0
  73. # define ICONV_errno get_iconv_errno()
  74. extern wchar_t *scheme_get_dll_path(wchar_t *s);
  75. static int iconv_ready = 0;
  76. static void init_iconv()
  77. {
  78. # ifdef MZ_NO_ICONV
  79. # else
  80. HMODULE m;
  81. m = LoadLibraryW(scheme_get_dll_path(L"iconv.dll"));
  82. if (!m)
  83. m = LoadLibraryW(scheme_get_dll_path(L"libiconv.dll"));
  84. if (!m)
  85. m = LoadLibraryW(scheme_get_dll_path(L"libiconv-2.dll"));
  86. if (!m)
  87. m = LoadLibrary("iconv.dll");
  88. if (!m)
  89. m = LoadLibrary("libiconv.dll");
  90. if (!m)
  91. m = LoadLibrary("libiconv-2.dll");
  92. if (m) {
  93. iconv = (iconv_proc_t)GetProcAddress(m, "libiconv");
  94. iconv_open = (iconv_open_proc_t)GetProcAddress(m, "libiconv_open");
  95. iconv_close = (iconv_close_proc_t)GetProcAddress(m, "libiconv_close");
  96. locale_charset = (locale_charset_proc_t)GetProcAddress(m, "locale_charset");
  97. /* Make sure we have all of them or none: */
  98. if (!iconv || !iconv_open || !iconv_close) {
  99. iconv = NULL;
  100. iconv_open = NULL;
  101. iconv_close = NULL;
  102. }
  103. }
  104. if (iconv) {
  105. iconv_errno = (errno_proc_t)GetProcAddress(m, "_errno");
  106. if (!iconv_errno) {
  107. /* The iconv.dll distributed with PLT Scheme links to msvcrt.dll.
  108. It's a slighly dangerous assumption that whaetever iconv we
  109. found also uses msvcrt.dll. */
  110. m = LoadLibrary("msvcrt.dll");
  111. if (m) {
  112. iconv_errno = (errno_proc_t)GetProcAddress(m, "_errno");
  113. if (!iconv_errno) {
  114. iconv = NULL;
  115. iconv_open = NULL;
  116. iconv_close = NULL;
  117. }
  118. }
  119. }
  120. }
  121. # endif
  122. iconv_ready = 1;
  123. }
  124. #else
  125. # define ICONV_errno errno
  126. # define iconv_ready 1
  127. # define mzCHK_PROC(x) 1
  128. static void init_iconv() { }
  129. #endif
  130. #ifdef MACOS_UNICODE_SUPPORT
  131. # define mzLOCALE_IS_UTF_8(s) (!s || !(*s))
  132. #endif
  133. #ifdef WINDOWS_UNICODE_SUPPORT
  134. # define mzLOCALE_IS_UTF_8(s) (!s || !(*s))
  135. #endif
  136. #ifndef mzLOCALE_IS_UTF_8
  137. # define mzLOCALE_IS_UTF_8(s) !mzCHK_PROC(iconv_open)
  138. #endif
  139. #define mzICONV_KIND 0
  140. #define mzUTF8_KIND 1
  141. #define mzUTF8_TO_UTF16_KIND 2
  142. #define mzUTF16_TO_UTF8_KIND 3
  143. typedef struct Scheme_Converter {
  144. Scheme_Object so;
  145. short closed;
  146. short kind;
  147. iconv_t cd;
  148. int permissive;
  149. Scheme_Custodian_Reference *mref;
  150. } Scheme_Converter;
  151. /* locals */
  152. /* These two locale variables are only valid when reset_locale()
  153. is called after continuation marks (and hence parameterization)
  154. may have changed. Similarly, setlocale() is only up-to-date
  155. when reset_locale() has been called. */
  156. THREAD_LOCAL_DECL(static int locale_on);
  157. THREAD_LOCAL_DECL(static void *current_locale_name_ptr);
  158. static void reset_locale(void);
  159. #define current_locale_name ((const mzchar *)current_locale_name_ptr)
  160. #ifdef USE_ICONV_DLL
  161. static char *nl_langinfo(int which)
  162. {
  163. int i;
  164. reset_locale();
  165. if (!current_locale_name)
  166. current_locale_name_ptr = "\0\0\0\0";
  167. if ((current_locale_name[0] == 'C')
  168. && !current_locale_name[1])
  169. return "US-ASCII";
  170. for (i = 0; current_locale_name[i]; i++) {
  171. if (current_locale_name[i] == '.') {
  172. if (current_locale_name[i + 1]) {
  173. int len, j = 0;
  174. char *enc;
  175. i++;
  176. len = scheme_char_strlen(current_locale_name) - i;
  177. enc = (char *)scheme_malloc_atomic(len + 1);
  178. while (current_locale_name[i]) {
  179. if (current_locale_name[i] > 127)
  180. return "UTF-8";
  181. enc[j++] = current_locale_name[i++];
  182. }
  183. enc[j] = 0;
  184. return enc;
  185. }
  186. }
  187. }
  188. return "UTF-8";
  189. }
  190. #endif
  191. #ifdef DONT_USE_LOCALE
  192. # define mz_iconv_nl_langinfo() ""
  193. #else
  194. static char *mz_iconv_nl_langinfo(){
  195. char *s;
  196. # if HAVE_CODESET
  197. s = nl_langinfo(CODESET);
  198. # else
  199. s = NULL;
  200. # endif
  201. if (!s)
  202. return "";
  203. else
  204. return s;
  205. }
  206. #endif
  207. READ_ONLY static const char * const STRING_IS_NOT_UTF_8 = "string is not a well-formed UTF-8 encoding: ";
  208. static Scheme_Object *make_string (int argc, Scheme_Object *argv[]);
  209. static Scheme_Object *string (int argc, Scheme_Object *argv[]);
  210. static Scheme_Object *string_p (int argc, Scheme_Object *argv[]);
  211. static Scheme_Object *string_length (int argc, Scheme_Object *argv[]);
  212. static Scheme_Object *string_eq (int argc, Scheme_Object *argv[]);
  213. static Scheme_Object *string_locale_eq (int argc, Scheme_Object *argv[]);
  214. static Scheme_Object *string_ci_eq (int argc, Scheme_Object *argv[]);
  215. static Scheme_Object *string_locale_ci_eq (int argc, Scheme_Object *argv[]);
  216. static Scheme_Object *string_lt (int argc, Scheme_Object *argv[]);
  217. static Scheme_Object *string_locale_lt (int argc, Scheme_Object *argv[]);
  218. static Scheme_Object *string_gt (int argc, Scheme_Object *argv[]);
  219. static Scheme_Object *string_locale_gt (int argc, Scheme_Object *argv[]);
  220. static Scheme_Object *string_lt_eq (int argc, Scheme_Object *argv[]);
  221. static Scheme_Object *string_gt_eq (int argc, Scheme_Object *argv[]);
  222. static Scheme_Object *string_ci_lt (int argc, Scheme_Object *argv[]);
  223. static Scheme_Object *string_locale_ci_lt (int argc, Scheme_Object *argv[]);
  224. static Scheme_Object *string_ci_gt (int argc, Scheme_Object *argv[]);
  225. static Scheme_Object *string_locale_ci_gt (int argc, Scheme_Object *argv[]);
  226. static Scheme_Object *string_ci_lt_eq (int argc, Scheme_Object *argv[]);
  227. static Scheme_Object *string_ci_gt_eq (int argc, Scheme_Object *argv[]);
  228. static Scheme_Object *string_upcase (int argc, Scheme_Object *argv[]);
  229. static Scheme_Object *string_downcase (int argc, Scheme_Object *argv[]);
  230. static Scheme_Object *string_titlecase (int argc, Scheme_Object *argv[]);
  231. static Scheme_Object *string_foldcase (int argc, Scheme_Object *argv[]);
  232. static Scheme_Object *string_locale_upcase (int argc, Scheme_Object *argv[]);
  233. static Scheme_Object *string_locale_downcase (int argc, Scheme_Object *argv[]);
  234. static Scheme_Object *substring (int argc, Scheme_Object *argv[]);
  235. static Scheme_Object *string_append (int argc, Scheme_Object *argv[]);
  236. static Scheme_Object *string_to_list (int argc, Scheme_Object *argv[]);
  237. static Scheme_Object *list_to_string (int argc, Scheme_Object *argv[]);
  238. static Scheme_Object *string_copy (int argc, Scheme_Object *argv[]);
  239. static Scheme_Object *string_copy_bang (int argc, Scheme_Object *argv[]);
  240. static Scheme_Object *string_fill (int argc, Scheme_Object *argv[]);
  241. static Scheme_Object *string_to_immutable (int argc, Scheme_Object *argv[]);
  242. static Scheme_Object *string_normalize_c (int argc, Scheme_Object *argv[]);
  243. static Scheme_Object *string_normalize_kc (int argc, Scheme_Object *argv[]);
  244. static Scheme_Object *string_normalize_d (int argc, Scheme_Object *argv[]);
  245. static Scheme_Object *string_normalize_kd (int argc, Scheme_Object *argv[]);
  246. #if defined(MZ_USE_PLACES) && defined(MZ_PRECISE_GC)
  247. static Scheme_Object *make_shared_byte_string (int argc, Scheme_Object *argv[]);
  248. static Scheme_Object *shared_byte_string (int argc, Scheme_Object *argv[]);
  249. #endif
  250. static Scheme_Object *make_byte_string (int argc, Scheme_Object *argv[]);
  251. static Scheme_Object *byte_string (int argc, Scheme_Object *argv[]);
  252. static Scheme_Object *byte_p (int argc, Scheme_Object *argv[]);
  253. static Scheme_Object *byte_string_p (int argc, Scheme_Object *argv[]);
  254. static Scheme_Object *byte_string_length (int argc, Scheme_Object *argv[]);
  255. static Scheme_Object *byte_string_eq (int argc, Scheme_Object *argv[]);
  256. static Scheme_Object *byte_string_lt (int argc, Scheme_Object *argv[]);
  257. static Scheme_Object *byte_string_gt (int argc, Scheme_Object *argv[]);
  258. static Scheme_Object *byte_substring (int argc, Scheme_Object *argv[]);
  259. static Scheme_Object *byte_string_append (int argc, Scheme_Object *argv[]);
  260. static Scheme_Object *byte_string_to_list (int argc, Scheme_Object *argv[]);
  261. static Scheme_Object *list_to_byte_string (int argc, Scheme_Object *argv[]);
  262. static Scheme_Object *byte_string_copy (int argc, Scheme_Object *argv[]);
  263. static Scheme_Object *byte_string_copy_bang (int argc, Scheme_Object *argv[]);
  264. static Scheme_Object *byte_string_fill (int argc, Scheme_Object *argv[]);
  265. static Scheme_Object *byte_string_to_immutable (int argc, Scheme_Object *argv[]);
  266. static Scheme_Object *byte_string_utf8_index (int argc, Scheme_Object *argv[]);
  267. static Scheme_Object *byte_string_utf8_ref (int argc, Scheme_Object *argv[]);
  268. static Scheme_Object *byte_string_utf8_length (int argc, Scheme_Object *argv[]);
  269. static Scheme_Object *byte_string_to_char_string (int argc, Scheme_Object *argv[]);
  270. static Scheme_Object *byte_string_to_char_string_locale (int argc, Scheme_Object *argv[]);
  271. static Scheme_Object *byte_string_to_char_string_latin1 (int argc, Scheme_Object *argv[]);
  272. static Scheme_Object *char_string_to_byte_string (int argc, Scheme_Object *argv[]);
  273. static Scheme_Object *char_string_to_byte_string_locale (int argc, Scheme_Object *argv[]);
  274. static Scheme_Object *char_string_to_byte_string_latin1 (int argc, Scheme_Object *argv[]);
  275. static Scheme_Object *char_string_utf8_length (int argc, Scheme_Object *argv[]);
  276. static Scheme_Object *version(int argc, Scheme_Object *argv[]);
  277. static Scheme_Object *format(int argc, Scheme_Object *argv[]);
  278. static Scheme_Object *sch_printf(int argc, Scheme_Object *argv[]);
  279. static Scheme_Object *sch_eprintf(int argc, Scheme_Object *argv[]);
  280. static Scheme_Object *sch_fprintf(int argc, Scheme_Object *argv[]);
  281. static Scheme_Object *banner(int argc, Scheme_Object *argv[]);
  282. static Scheme_Object *sch_getenv(int argc, Scheme_Object *argv[]);
  283. static Scheme_Object *sch_putenv(int argc, Scheme_Object *argv[]);
  284. static Scheme_Object *system_type(int argc, Scheme_Object *argv[]);
  285. static Scheme_Object *system_library_subpath(int argc, Scheme_Object *argv[]);
  286. static Scheme_Object *cmdline_args(int argc, Scheme_Object *argv[]);
  287. static Scheme_Object *current_locale(int argc, Scheme_Object *argv[]);
  288. static Scheme_Object *locale_string_encoding(int argc, Scheme_Object *argv[]);
  289. static Scheme_Object *system_language_country(int argc, Scheme_Object *argv[]);
  290. static Scheme_Object *byte_string_open_converter(int argc, Scheme_Object *argv[]);
  291. static Scheme_Object *byte_string_close_converter(int argc, Scheme_Object *argv[]);
  292. static Scheme_Object *byte_string_convert(int argc, Scheme_Object *argv[]);
  293. static Scheme_Object *byte_string_convert_end(int argc, Scheme_Object *argv[]);
  294. static Scheme_Object *byte_converter_p(int argc, Scheme_Object *argv[]);
  295. #ifdef MZ_PRECISE_GC
  296. static void register_traversers(void);
  297. #endif
  298. static int mz_char_strcmp(const char *who, const mzchar *str1, intptr_t l1, const mzchar *str2, intptr_t l2, int locale, int size_shortcut);
  299. static int mz_char_strcmp_ci(const char *who, const mzchar *str1, intptr_t l1, const mzchar *str2, intptr_t l2, int locale, int size_shortcut);
  300. static int mz_strcmp(const char *who, unsigned char *str1, intptr_t l1, unsigned char *str2, intptr_t l2);
  301. XFORM_NONGCING static intptr_t utf8_decode_x(const unsigned char *s, intptr_t start, intptr_t end,
  302. unsigned int *us, intptr_t dstart, intptr_t dend,
  303. intptr_t *ipos, intptr_t *jpos,
  304. char compact, char utf16,
  305. int *state, int might_continue, int permissive);
  306. XFORM_NONGCING static intptr_t utf8_encode_x(const unsigned int *us, intptr_t start, intptr_t end,
  307. unsigned char *s, intptr_t dstart, intptr_t dend,
  308. intptr_t *_ipos, intptr_t *_opos, char utf16);
  309. static char *string_to_from_locale(int to_bytes,
  310. char *in, intptr_t delta, intptr_t len,
  311. intptr_t *olen, int perm,
  312. int *no_cvt);
  313. #define portable_isspace(x) (((x) < 128) && isspace(x))
  314. ROSYM static Scheme_Object *sys_symbol;
  315. ROSYM static Scheme_Object *platform_3m_path, *platform_cgc_path;
  316. READ_ONLY static Scheme_Object *zero_length_char_string;
  317. READ_ONLY static Scheme_Object *zero_length_byte_string;
  318. SHARED_OK static Scheme_Hash_Table *putenv_str_table;
  319. SHARED_OK static char *embedding_banner;
  320. SHARED_OK static Scheme_Object *vers_str;
  321. SHARED_OK static Scheme_Object *banner_str;
  322. READ_ONLY static Scheme_Object *complete_symbol, *continues_symbol, *aborts_symbol, *error_symbol;
  323. void
  324. scheme_init_string (Scheme_Env *env)
  325. {
  326. Scheme_Object *p;
  327. REGISTER_SO(sys_symbol);
  328. sys_symbol = scheme_intern_symbol(SYSTEM_TYPE_NAME);
  329. REGISTER_SO(zero_length_char_string);
  330. REGISTER_SO(zero_length_byte_string);
  331. zero_length_char_string = scheme_alloc_char_string(0, 0);
  332. zero_length_byte_string = scheme_alloc_byte_string(0, 0);
  333. REGISTER_SO(complete_symbol);
  334. REGISTER_SO(continues_symbol);
  335. REGISTER_SO(aborts_symbol);
  336. REGISTER_SO(error_symbol);
  337. complete_symbol = scheme_intern_symbol("complete");
  338. continues_symbol = scheme_intern_symbol("continues");
  339. aborts_symbol = scheme_intern_symbol("aborts");
  340. error_symbol = scheme_intern_symbol("error");
  341. REGISTER_SO(platform_3m_path);
  342. #ifdef UNIX_FILE_SYSTEM
  343. # define MZ3M_SUBDIR "/3m"
  344. #else
  345. # ifdef DOS_FILE_SYSTEM
  346. # define MZ3M_SUBDIR "\\3m"
  347. # else
  348. # define MZ3M_SUBDIR ":3m"
  349. # endif
  350. #endif
  351. REGISTER_SO(platform_3m_path);
  352. REGISTER_SO(platform_cgc_path);
  353. platform_cgc_path = scheme_make_path(SCHEME_PLATFORM_LIBRARY_SUBPATH);
  354. platform_3m_path = scheme_make_path(SCHEME_PLATFORM_LIBRARY_SUBPATH MZ3M_SUBDIR);
  355. REGISTER_SO(putenv_str_table);
  356. REGISTER_SO(embedding_banner);
  357. REGISTER_SO(vers_str);
  358. REGISTER_SO(banner_str);
  359. vers_str = scheme_make_utf8_string(scheme_version());
  360. SCHEME_SET_CHAR_STRING_IMMUTABLE(vers_str);
  361. banner_str = scheme_make_utf8_string(scheme_banner());
  362. SCHEME_SET_CHAR_STRING_IMMUTABLE(banner_str);
  363. p = scheme_make_folding_prim(string_p, "string?", 1, 1, 1);
  364. SCHEME_PRIM_PROC_FLAGS(p) |= SCHEME_PRIM_IS_UNARY_INLINED;
  365. scheme_add_global_constant("string?", p, env);
  366. scheme_add_global_constant("make-string",
  367. scheme_make_immed_prim(make_string,
  368. "make-string",
  369. 1, 2),
  370. env);
  371. scheme_add_global_constant("string",
  372. scheme_make_immed_prim(string,
  373. "string",
  374. 0, -1),
  375. env);
  376. scheme_add_global_constant("string-length",
  377. scheme_make_folding_prim(string_length,
  378. "string-length",
  379. 1, 1, 1),
  380. env);
  381. p = scheme_make_immed_prim(scheme_checked_string_ref, "string-ref", 2, 2);
  382. SCHEME_PRIM_PROC_FLAGS(p) |= SCHEME_PRIM_IS_BINARY_INLINED;
  383. scheme_add_global_constant("string-ref", p, env);
  384. p = scheme_make_immed_prim(scheme_checked_string_set, "string-set!", 3, 3);
  385. SCHEME_PRIM_PROC_FLAGS(p) |= SCHEME_PRIM_IS_NARY_INLINED;
  386. scheme_add_global_constant("string-set!", p, env);
  387. scheme_add_global_constant("string=?",
  388. scheme_make_immed_prim(string_eq,
  389. "string=?",
  390. 2, -1),
  391. env);
  392. scheme_add_global_constant("string-locale=?",
  393. scheme_make_immed_prim(string_locale_eq,
  394. "string-locale=?",
  395. 2, -1),
  396. env);
  397. scheme_add_global_constant("string-ci=?",
  398. scheme_make_immed_prim(string_ci_eq,
  399. "string-ci=?",
  400. 2, -1),
  401. env);
  402. scheme_add_global_constant("string-locale-ci=?",
  403. scheme_make_immed_prim(string_locale_ci_eq,
  404. "string-locale-ci=?",
  405. 2, -1),
  406. env);
  407. scheme_add_global_constant("string<?",
  408. scheme_make_immed_prim(string_lt,
  409. "string<?",
  410. 2, -1),
  411. env);
  412. scheme_add_global_constant("string-locale<?",
  413. scheme_make_immed_prim(string_locale_lt,
  414. "string-locale<?",
  415. 2, -1),
  416. env);
  417. scheme_add_global_constant("string>?",
  418. scheme_make_immed_prim(string_gt,
  419. "string>?",
  420. 2, -1),
  421. env);
  422. scheme_add_global_constant("string-locale>?",
  423. scheme_make_immed_prim(string_locale_gt,
  424. "string-locale>?",
  425. 2, -1),
  426. env);
  427. scheme_add_global_constant("string<=?",
  428. scheme_make_immed_prim(string_lt_eq,
  429. "string<=?",
  430. 2, -1),
  431. env);
  432. scheme_add_global_constant("string>=?",
  433. scheme_make_immed_prim(string_gt_eq,
  434. "string>=?",
  435. 2, -1),
  436. env);
  437. scheme_add_global_constant("string-ci<?",
  438. scheme_make_immed_prim(string_ci_lt,
  439. "string-ci<?",
  440. 2, -1),
  441. env);
  442. scheme_add_global_constant("string-locale-ci<?",
  443. scheme_make_immed_prim(string_locale_ci_lt,
  444. "string-locale-ci<?",
  445. 2, -1),
  446. env);
  447. scheme_add_global_constant("string-ci>?",
  448. scheme_make_immed_prim(string_ci_gt,
  449. "string-ci>?",
  450. 2, -1),
  451. env);
  452. scheme_add_global_constant("string-locale-ci>?",
  453. scheme_make_immed_prim(string_locale_ci_gt,
  454. "string-locale-ci>?",
  455. 2, -1),
  456. env);
  457. scheme_add_global_constant("string-ci<=?",
  458. scheme_make_immed_prim(string_ci_lt_eq,
  459. "string-ci<=?",
  460. 2, -1),
  461. env);
  462. scheme_add_global_constant("string-ci>=?",
  463. scheme_make_immed_prim(string_ci_gt_eq,
  464. "string-ci>=?",
  465. 2, -1),
  466. env);
  467. scheme_add_global_constant("substring",
  468. scheme_make_immed_prim(substring,
  469. "substring",
  470. 2, 3),
  471. env);
  472. scheme_add_global_constant("string-append",
  473. scheme_make_immed_prim(string_append,
  474. "string-append",
  475. 0, -1),
  476. env);
  477. scheme_add_global_constant("string->list",
  478. scheme_make_immed_prim(string_to_list,
  479. "string->list",
  480. 1, 1),
  481. env);
  482. scheme_add_global_constant("list->string",
  483. scheme_make_immed_prim(list_to_string,
  484. "list->string",
  485. 1, 1),
  486. env);
  487. scheme_add_global_constant("string-copy",
  488. scheme_make_immed_prim(string_copy,
  489. "string-copy",
  490. 1, 1),
  491. env);
  492. scheme_add_global_constant("string-copy!",
  493. scheme_make_immed_prim(string_copy_bang,
  494. "string-copy!",
  495. 3, 5),
  496. env);
  497. scheme_add_global_constant("string-fill!",
  498. scheme_make_immed_prim(string_fill,
  499. "string-fill!",
  500. 2, 2),
  501. env);
  502. scheme_add_global_constant("string->immutable-string",
  503. scheme_make_immed_prim(string_to_immutable,
  504. "string->immutable-string",
  505. 1, 1),
  506. env);
  507. scheme_add_global_constant("string-normalize-nfc",
  508. scheme_make_immed_prim(string_normalize_c,
  509. "string-normalize-nfc",
  510. 1, 1),
  511. env);
  512. scheme_add_global_constant("string-normalize-nfkc",
  513. scheme_make_immed_prim(string_normalize_kc,
  514. "string-normalize-nfkc",
  515. 1, 1),
  516. env);
  517. scheme_add_global_constant("string-normalize-nfd",
  518. scheme_make_immed_prim(string_normalize_d,
  519. "string-normalize-nfd",
  520. 1, 1),
  521. env);
  522. scheme_add_global_constant("string-normalize-nfkd",
  523. scheme_make_immed_prim(string_normalize_kd,
  524. "string-normalize-nfkd",
  525. 1, 1),
  526. env);
  527. scheme_add_global_constant("string-upcase",
  528. scheme_make_immed_prim(string_upcase,
  529. "string-upcase",
  530. 1, 1),
  531. env);
  532. scheme_add_global_constant("string-downcase",
  533. scheme_make_immed_prim(string_downcase,
  534. "string-downcase",
  535. 1, 1),
  536. env);
  537. scheme_add_global_constant("string-titlecase",
  538. scheme_make_immed_prim(string_titlecase,
  539. "string-titlecase",
  540. 1, 1),
  541. env);
  542. scheme_add_global_constant("string-foldcase",
  543. scheme_make_immed_prim(string_foldcase,
  544. "string-foldcase",
  545. 1, 1),
  546. env);
  547. scheme_add_global_constant("string-locale-upcase",
  548. scheme_make_immed_prim(string_locale_upcase,
  549. "string-locale-upcase",
  550. 1, 1),
  551. env);
  552. scheme_add_global_constant("string-locale-downcase",
  553. scheme_make_immed_prim(string_locale_downcase,
  554. "string-locale-downcase",
  555. 1, 1),
  556. env);
  557. scheme_add_global_constant("current-locale",
  558. scheme_register_parameter(current_locale,
  559. "current-locale",
  560. MZCONFIG_LOCALE),
  561. env);
  562. scheme_add_global_constant("locale-string-encoding",
  563. scheme_make_immed_prim(locale_string_encoding,
  564. "locale-string-encoding",
  565. 0, 0),
  566. env);
  567. scheme_add_global_constant("system-language+country",
  568. scheme_make_immed_prim(system_language_country,
  569. "system-language+country",
  570. 0, 0),
  571. env);
  572. scheme_add_global_constant("bytes-converter?",
  573. scheme_make_immed_prim(byte_converter_p,
  574. "bytes-converter?",
  575. 1, 1),
  576. env);
  577. scheme_add_global_constant("bytes-convert",
  578. scheme_make_prim_w_arity2(byte_string_convert,
  579. "bytes-convert",
  580. 1, 7,
  581. 3, 3),
  582. env);
  583. scheme_add_global_constant("bytes-convert-end",
  584. scheme_make_prim_w_arity2(byte_string_convert_end,
  585. "bytes-convert-end",
  586. 0, 3,
  587. 2, 2),
  588. env);
  589. scheme_add_global_constant("bytes-open-converter",
  590. scheme_make_immed_prim(byte_string_open_converter,
  591. "bytes-open-converter",
  592. 2, 2),
  593. env);
  594. scheme_add_global_constant("bytes-close-converter",
  595. scheme_make_immed_prim(byte_string_close_converter,
  596. "bytes-close-converter",
  597. 1, 1),
  598. env);
  599. scheme_add_global_constant("format",
  600. scheme_make_noncm_prim(format,
  601. "format",
  602. 1, -1),
  603. env);
  604. scheme_add_global_constant("printf",
  605. scheme_make_noncm_prim(sch_printf,
  606. "printf",
  607. 1, -1),
  608. env);
  609. scheme_add_global_constant("eprintf",
  610. scheme_make_noncm_prim(sch_eprintf,
  611. "eprintf",
  612. 1, -1),
  613. env);
  614. scheme_add_global_constant("fprintf",
  615. scheme_make_noncm_prim(sch_fprintf,
  616. "fprintf",
  617. 2, -1),
  618. env);
  619. scheme_add_global_constant("byte?",
  620. scheme_make_folding_prim(byte_p,
  621. "byte?",
  622. 1, 1, 1),
  623. env);
  624. p = scheme_make_folding_prim(byte_string_p, "bytes?", 1, 1, 1);
  625. SCHEME_PRIM_PROC_FLAGS(p) |= SCHEME_PRIM_IS_NARY_INLINED;
  626. scheme_add_global_constant("bytes?", p, env);
  627. scheme_add_global_constant("make-bytes",
  628. scheme_make_immed_prim(make_byte_string,
  629. "make-bytes",
  630. 1, 2),
  631. env);
  632. scheme_add_global_constant("bytes",
  633. scheme_make_immed_prim(byte_string,
  634. "bytes",
  635. 0, -1),
  636. env);
  637. #if defined(MZ_USE_PLACES) && defined(MZ_PRECISE_GC)
  638. GLOBAL_PRIM_W_ARITY("make-shared-bytes", make_shared_byte_string, 1, 2, env);
  639. GLOBAL_PRIM_W_ARITY("shared-bytes", shared_byte_string, 0, -1, env);
  640. #else
  641. GLOBAL_PRIM_W_ARITY("make-shared-bytes", make_byte_string, 1, 2, env);
  642. GLOBAL_PRIM_W_ARITY("shared-bytes", byte_string, 0, -1, env);
  643. #endif
  644. scheme_add_global_constant("bytes-length",
  645. scheme_make_folding_prim(byte_string_length,
  646. "bytes-length",
  647. 1, 1, 1),
  648. env);
  649. p = scheme_make_immed_prim(scheme_checked_byte_string_ref, "bytes-ref", 2, 2);
  650. SCHEME_PRIM_PROC_FLAGS(p) |= SCHEME_PRIM_IS_BINARY_INLINED;
  651. scheme_add_global_constant("bytes-ref", p, env);
  652. p = scheme_make_immed_prim(scheme_checked_byte_string_set, "bytes-set!", 3, 3);
  653. SCHEME_PRIM_PROC_FLAGS(p) |= SCHEME_PRIM_IS_NARY_INLINED;
  654. scheme_add_global_constant("bytes-set!", p, env);
  655. scheme_add_global_constant("bytes=?",
  656. scheme_make_immed_prim(byte_string_eq,
  657. "bytes=?",
  658. 2, -1),
  659. env);
  660. scheme_add_global_constant("bytes<?",
  661. scheme_make_immed_prim(byte_string_lt,
  662. "bytes<?",
  663. 2, -1),
  664. env);
  665. scheme_add_global_constant("bytes>?",
  666. scheme_make_immed_prim(byte_string_gt,
  667. "bytes>?",
  668. 2, -1),
  669. env);
  670. scheme_add_global_constant("subbytes",
  671. scheme_make_immed_prim(byte_substring,
  672. "subbytes",
  673. 2, 3),
  674. env);
  675. scheme_add_global_constant("bytes-append",
  676. scheme_make_immed_prim(byte_string_append,
  677. "bytes-append",
  678. 0, -1),
  679. env);
  680. scheme_add_global_constant("bytes->list",
  681. scheme_make_immed_prim(byte_string_to_list,
  682. "bytes->list",
  683. 1, 1),
  684. env);
  685. scheme_add_global_constant("list->bytes",
  686. scheme_make_immed_prim(list_to_byte_string,
  687. "list->bytes",
  688. 1, 1),
  689. env);
  690. scheme_add_global_constant("bytes-copy",
  691. scheme_make_immed_prim(byte_string_copy,
  692. "bytes-copy",
  693. 1, 1),
  694. env);
  695. scheme_add_global_constant("bytes-copy!",
  696. scheme_make_immed_prim(byte_string_copy_bang,
  697. "bytes-copy!",
  698. 3, 5),
  699. env);
  700. scheme_add_global_constant("bytes-fill!",
  701. scheme_make_immed_prim(byte_string_fill,
  702. "bytes-fill!",
  703. 2, 2),
  704. env);
  705. scheme_add_global_constant("bytes->immutable-bytes",
  706. scheme_make_immed_prim(byte_string_to_immutable,
  707. "bytes->immutable-bytes",
  708. 1, 1),
  709. env);
  710. scheme_add_global_constant("bytes-utf-8-index",
  711. scheme_make_immed_prim(byte_string_utf8_index,
  712. "bytes-utf-8-index",
  713. 2, 4),
  714. env);
  715. scheme_add_global_constant("bytes-utf-8-length",
  716. scheme_make_immed_prim(byte_string_utf8_length,
  717. "bytes-utf-8-length",
  718. 1, 4),
  719. env);
  720. scheme_add_global_constant("bytes-utf-8-ref",
  721. scheme_make_immed_prim(byte_string_utf8_ref,
  722. "bytes-utf-8-ref",
  723. 2, 4),
  724. env);
  725. scheme_add_global_constant("bytes->string/utf-8",
  726. scheme_make_immed_prim(byte_string_to_char_string,
  727. "bytes->string/utf-8",
  728. 1, 4),
  729. env);
  730. scheme_add_global_constant("bytes->string/locale",
  731. scheme_make_immed_prim(byte_string_to_char_string_locale,
  732. "bytes->string/locale",
  733. 1, 4),
  734. env);
  735. scheme_add_global_constant("bytes->string/latin-1",
  736. scheme_make_immed_prim(byte_string_to_char_string_latin1,
  737. "bytes->string/latin-1",
  738. 1, 4),
  739. env);
  740. scheme_add_global_constant("string->bytes/utf-8",
  741. scheme_make_immed_prim(char_string_to_byte_string,
  742. "string->bytes/utf-8",
  743. 1, 4),
  744. env);
  745. scheme_add_global_constant("string->bytes/locale",
  746. scheme_make_immed_prim(char_string_to_byte_string_locale,
  747. "string->bytes/locale",
  748. 1, 4),
  749. env);
  750. scheme_add_global_constant("string->bytes/latin-1",
  751. scheme_make_immed_prim(char_string_to_byte_string_latin1,
  752. "string->bytes/latin-1",
  753. 1, 4),
  754. env);
  755. scheme_add_global_constant("string-utf-8-length",
  756. scheme_make_immed_prim(char_string_utf8_length,
  757. "string-utf-8-length",
  758. 1, 3),
  759. env);
  760. /* In principle, `version' could be foldable, but it invites
  761. more problems than it solves... */
  762. scheme_add_global_constant("version",
  763. scheme_make_immed_prim(version,
  764. "version",
  765. 0, 0),
  766. env);
  767. scheme_add_global_constant("banner",
  768. scheme_make_immed_prim(banner,
  769. "banner",
  770. 0, 0),
  771. env);
  772. scheme_add_global_constant("getenv",
  773. scheme_make_immed_prim(sch_getenv,
  774. "getenv",
  775. 1, 1),
  776. env);
  777. scheme_add_global_constant("putenv",
  778. scheme_make_immed_prim(sch_putenv,
  779. "putenv",
  780. 2, 2),
  781. env);
  782. /* Don't make these folding, since they're platform-specific: */
  783. scheme_add_global_constant("system-type",
  784. scheme_make_immed_prim(system_type,
  785. "system-type",
  786. 0, 1),
  787. env);
  788. scheme_add_global_constant("system-library-subpath",
  789. scheme_make_immed_prim(system_library_subpath,
  790. "system-library-subpath",
  791. 0, 1),
  792. env);
  793. scheme_add_global_constant("current-command-line-arguments",
  794. scheme_register_parameter(cmdline_args,
  795. "current-command-line-arguments",
  796. MZCONFIG_CMDLINE_ARGS),
  797. env);
  798. #ifdef MZ_PRECISE_GC
  799. register_traversers();
  800. #endif
  801. }
  802. void scheme_init_string_places(void) {
  803. REGISTER_SO(current_locale_name_ptr);
  804. current_locale_name_ptr = "xxxx\0\0\0\0";
  805. }
  806. /**********************************************************************/
  807. /* UTF-8 char constructors */
  808. /**********************************************************************/
  809. Scheme_Object *scheme_make_sized_offset_utf8_string(char *chars, intptr_t d, intptr_t len)
  810. {
  811. intptr_t ulen;
  812. mzchar *us;
  813. if (len) {
  814. ulen = scheme_utf8_decode((unsigned char *)chars, d, d + len,
  815. NULL, 0, -1,
  816. NULL, 0 /* not UTF-16 */, 0xFFFD);
  817. us = scheme_malloc_atomic(sizeof(mzchar) * (ulen + 1));
  818. scheme_utf8_decode((unsigned char *)chars, d, d + len,
  819. us, 0, -1,
  820. NULL, 0 /* not UTF-16 */, 0xFFFD);
  821. us[ulen] = 0;
  822. } else {
  823. us = (mzchar *)"\0\0\0";
  824. ulen = 0;
  825. }
  826. return scheme_make_sized_offset_char_string(us, 0, ulen, 0);
  827. }
  828. Scheme_Object *
  829. scheme_make_sized_utf8_string(char *chars, intptr_t len)
  830. {
  831. return scheme_make_sized_offset_utf8_string(chars, 0, len);
  832. }
  833. Scheme_Object *
  834. scheme_make_immutable_sized_utf8_string(char *chars, intptr_t len)
  835. {
  836. Scheme_Object *s;
  837. s = scheme_make_sized_offset_utf8_string(chars, 0, len);
  838. if (len)
  839. SCHEME_SET_CHAR_STRING_IMMUTABLE(s);
  840. return s;
  841. }
  842. Scheme_Object *
  843. scheme_make_utf8_string(const char *chars)
  844. {
  845. return scheme_make_sized_offset_utf8_string((char *)chars, 0, -1);
  846. }
  847. Scheme_Object *
  848. scheme_make_locale_string(const char *chars)
  849. {
  850. return scheme_byte_string_to_char_string_locale(scheme_make_byte_string((char *)chars));
  851. }
  852. /**********************************************************************/
  853. /* index helpers */
  854. /**********************************************************************/
  855. void scheme_out_of_string_range(const char *name, const char *which,
  856. Scheme_Object *i, Scheme_Object *s,
  857. intptr_t start, intptr_t len)
  858. {
  859. int is_byte;
  860. is_byte = SCHEME_BYTE_STRINGP(s);
  861. if (len) {
  862. char *sstr;
  863. intptr_t slen;
  864. sstr = scheme_make_provided_string(s, 2, &slen);
  865. scheme_raise_exn(MZEXN_FAIL_CONTRACT,
  866. "%s: %sindex %s out of range [%d, %d] for %s%s: %t",
  867. name, which,
  868. scheme_make_provided_string(i, 2, NULL),
  869. start, len,
  870. is_byte ? "byte-" : "",
  871. SCHEME_CHAPERONE_VECTORP(s) ? "vector" : "string",
  872. sstr, slen);
  873. } else {
  874. scheme_raise_exn(MZEXN_FAIL_CONTRACT,
  875. "%s: %sindex %s out of range for empty %s%s",
  876. name, which,
  877. scheme_make_provided_string(i, 0, NULL),
  878. is_byte ? "byte-" : "",
  879. SCHEME_CHAPERONE_VECTORP(s) ? "vector" : "string");
  880. }
  881. }
  882. intptr_t scheme_extract_index(const char *name, int pos, int argc, Scheme_Object **argv, intptr_t top, int false_ok)
  883. {
  884. intptr_t i;
  885. int is_top = 0;
  886. if (SCHEME_INTP(argv[pos])) {
  887. i = SCHEME_INT_VAL(argv[pos]);
  888. } else if (SCHEME_BIGNUMP(argv[pos])) {
  889. if (SCHEME_BIGPOS(argv[pos])) {
  890. i = top; /* out-of-bounds */
  891. is_top = 1;
  892. } else
  893. i = -1; /* negative */
  894. } else
  895. i = -1;
  896. if (!is_top && (i < 0))
  897. scheme_wrong_type(name,
  898. (false_ok ? "non-negative exact integer or #f" : "non-negative exact integer"),
  899. pos, argc, argv);
  900. return i;
  901. }
  902. void scheme_get_substring_indices(const char *name, Scheme_Object *str,
  903. int argc, Scheme_Object **argv,
  904. int spos, int fpos, intptr_t *_start, intptr_t *_finish)
  905. {
  906. intptr_t len;
  907. intptr_t start, finish;
  908. if (SCHEME_CHAPERONE_VECTORP(str))
  909. len = SCHEME_VEC_SIZE(str);
  910. else if (SCHEME_CHAR_STRINGP(str))
  911. len = SCHEME_CHAR_STRTAG_VAL(str);
  912. else
  913. len = SCHEME_BYTE_STRTAG_VAL(str);
  914. if (argc > spos)
  915. start = scheme_extract_index(name, spos, argc, argv, len + 1, 0);
  916. else
  917. start = 0;
  918. if (argc > fpos)
  919. finish = scheme_extract_index(name, fpos, argc, argv, len + 1, 0);
  920. else
  921. finish = len;
  922. if (!(start <= len)) {
  923. scheme_out_of_string_range(name, (fpos < 100) ? "starting " : "", argv[spos], str, 0, len);
  924. }
  925. if (!(finish >= start && finish <= len)) {
  926. scheme_out_of_string_range(name, "ending ", argv[fpos], str, start, len);
  927. }
  928. *_start = start;
  929. *_finish = finish;
  930. }
  931. void scheme_do_get_substring_indices(const char *name, Scheme_Object *str,
  932. int argc, Scheme_Object **argv,
  933. int spos, int fpos, intptr_t *_start, intptr_t *_finish, intptr_t len)
  934. {
  935. if (argc > spos) {
  936. if (SCHEME_INTP(argv[spos])) {
  937. intptr_t start = SCHEME_INT_VAL(argv[spos]);
  938. if ((start >= 0) && (start < len)) {
  939. *_start = start;
  940. if (argc > fpos) {
  941. intptr_t finish = SCHEME_INT_VAL(argv[fpos]);
  942. if ((finish >= start) && (finish <= len)) {
  943. *_finish = finish;
  944. return;
  945. }
  946. } else {
  947. *_finish = len;
  948. return;
  949. }
  950. }
  951. }
  952. } else {
  953. *_start = 0;
  954. *_finish = len;
  955. return;
  956. }
  957. scheme_get_substring_indices(name, str, argc, argv, spos, fpos, _start, _finish);
  958. }
  959. /**********************************************************************/
  960. /* char strings */
  961. /**********************************************************************/
  962. #define SCHEME_X_STR_VAL(x) SCHEME_CHAR_STR_VAL(x)
  963. #define SCHEME_X_STRTAG_VAL(x) SCHEME_CHAR_STRTAG_VAL(x)
  964. #define SCHEME_X_STRINGP(x) SCHEME_CHAR_STRINGP(x)
  965. #define SCHEME_MUTABLE_X_STRINGP(x) SCHEME_MUTABLE_CHAR_STRINGP(x)
  966. #define SCHEME_SET_X_STRING_IMMUTABLE(x) SCHEME_SET_CHAR_STRING_IMMUTABLE(x)
  967. #define scheme_x_string_type scheme_char_string_type
  968. #define X(a, b) a##_char##b
  969. #define X_(a, b) a##_##b
  970. #define X__(a) a
  971. #define EMPTY (mzchar *)"\0\0\0"
  972. #define Xchar mzchar
  973. #define uXchar mzchar
  974. #define XSTR ""
  975. #define XSTRINGSTR "string"
  976. #define SUBXSTR "substring"
  977. #define CHARP(x) SCHEME_CHARP(x)
  978. #define CHAR_VAL(x) SCHEME_CHAR_VAL(x)
  979. #define CHAR_STR "character"
  980. #define MAKE_CHAR(x) _scheme_make_char(x)
  981. #define xstrlen scheme_char_strlen
  982. #include "strops.inc"
  983. #define GEN_STRING_COMP(name, scheme_name, comp, op, ul, size_shortcut) \
  984. static Scheme_Object * name (int argc, Scheme_Object *argv[]) \
  985. { mzchar *s, *prev; int i, sl, pl; int falz = 0;\
  986. if (!SCHEME_CHAR_STRINGP(argv[0])) \
  987. scheme_wrong_type(scheme_name, "string", 0, argc, argv); \
  988. prev = SCHEME_CHAR_STR_VAL(argv[0]); pl = SCHEME_CHAR_STRTAG_VAL(argv[0]); \
  989. for (i = 1; i < argc; i++) { \
  990. if (!SCHEME_CHAR_STRINGP(argv[i])) \
  991. scheme_wrong_type(scheme_name, "string", i, argc, argv); \
  992. s = SCHEME_CHAR_STR_VAL(argv[i]); sl = SCHEME_CHAR_STRTAG_VAL(argv[i]); \
  993. if (!falz) if (!(comp(scheme_name, \
  994. prev, pl, \
  995. s, sl, ul, size_shortcut) op 0)) falz = 1; \
  996. prev = s; pl = sl; \
  997. } \
  998. return falz ? scheme_false : scheme_true; \
  999. }
  1000. GEN_STRING_COMP(string_eq, "string=?", mz_char_strcmp, ==, 0, 1)
  1001. GEN_STRING_COMP(string_lt, "string<?", mz_char_strcmp, <, 0, 0)
  1002. GEN_STRING_COMP(string_gt, "string>?", mz_char_strcmp, >, 0, 0)
  1003. GEN_STRING_COMP(string_lt_eq, "string<=?", mz_char_strcmp, <=, 0, 0)
  1004. GEN_STRING_COMP(string_gt_eq, "string>=?", mz_char_strcmp, >=, 0, 0)
  1005. GEN_STRING_COMP(string_ci_eq, "string-ci=?", mz_char_strcmp_ci, ==, 0, 0)
  1006. GEN_STRING_COMP(string_ci_lt, "string-ci<?", mz_char_strcmp_ci, <, 0, 0)
  1007. GEN_STRING_COMP(string_ci_gt, "string-ci>?", mz_char_strcmp_ci, >, 0, 0)
  1008. GEN_STRING_COMP(string_ci_lt_eq, "string-ci<=?", mz_char_strcmp_ci, <=, 0, 0)
  1009. GEN_STRING_COMP(string_ci_gt_eq, "string-ci>=?", mz_char_strcmp_ci, >=, 0, 0)
  1010. GEN_STRING_COMP(string_locale_eq, "string-locale=?", mz_char_strcmp, ==, 1, 0)
  1011. GEN_STRING_COMP(string_locale_lt, "string-locale<?", mz_char_strcmp, <, 1, 0)
  1012. GEN_STRING_COMP(string_locale_gt, "string-locale>?", mz_char_strcmp, >, 1, 0)
  1013. GEN_STRING_COMP(string_locale_ci_eq, "string-locale-ci=?", mz_char_strcmp_ci, ==, 1, 0)
  1014. GEN_STRING_COMP(string_locale_ci_lt, "string-locale-ci<?", mz_char_strcmp_ci, <, 1, 0)
  1015. GEN_STRING_COMP(string_locale_ci_gt, "string-locale-ci>?", mz_char_strcmp_ci, >, 1, 0)
  1016. /**********************************************************************/
  1017. /* byte strings */
  1018. /**********************************************************************/
  1019. #define SCHEME_BYTEP(x) ((SCHEME_INTP(x)) && (SCHEME_INT_VAL(x) >= 0) && (SCHEME_INT_VAL(x) <= 255))
  1020. #define BYTE_STR "exact integer in [0,255]"
  1021. static Scheme_Object *
  1022. byte_p(int argc, Scheme_Object *argv[])
  1023. {
  1024. return (SCHEME_BYTEP(argv[0]) ? scheme_true : scheme_false);
  1025. }
  1026. #define SCHEME_X_STR_VAL(x) SCHEME_BYTE_STR_VAL(x)
  1027. #define SCHEME_X_STRTAG_VAL(x) SCHEME_BYTE_STRTAG_VAL(x)
  1028. #define SCHEME_X_STRINGP(x) SCHEME_BYTE_STRINGP(x)
  1029. #define SCHEME_MUTABLE_X_STRINGP(x) SCHEME_MUTABLE_BYTE_STRINGP(x)
  1030. #define SCHEME_SET_X_STRING_IMMUTABLE(x) SCHEME_SET_BYTE_STRING_IMMUTABLE(x)
  1031. #define scheme_x_string_type scheme_byte_string_type
  1032. #define X(a, b) a##_byte##b
  1033. #define X_(a, b) a##_byte_##b
  1034. #define X__(a) byte_##a
  1035. #define EMPTY ""
  1036. #define Xchar char
  1037. #define uXchar unsigned char
  1038. #define XSTR "byte "
  1039. #define XSTRINGSTR "bytes"
  1040. #define SUBXSTR "subbytes"
  1041. #define CHARP(x) SCHEME_BYTEP(x)
  1042. #define CHAR_VAL(x) SCHEME_INT_VAL(x)
  1043. #define CHAR_STR BYTE_STR
  1044. #define MAKE_CHAR(x) scheme_make_integer_value(x)
  1045. #define xstrlen strlen
  1046. #define GENERATING_BYTE
  1047. #include "strops.inc"
  1048. #undef GENERATING_BYTE
  1049. /* comparisons */
  1050. #define GEN_BYTE_STRING_COMP(name, scheme_name, comp, op) \
  1051. static Scheme_Object * name (int argc, Scheme_Object *argv[]) \
  1052. { char *s, *prev; int i, sl, pl; int falz = 0;\
  1053. if (!SCHEME_BYTE_STRINGP(argv[0])) \
  1054. scheme_wrong_type(scheme_name, "byte string", 0, argc, argv); \
  1055. prev = SCHEME_BYTE_STR_VAL(argv[0]); pl = SCHEME_BYTE_STRTAG_VAL(argv[0]); \
  1056. for (i = 1; i < argc; i++) { \
  1057. if (!SCHEME_BYTE_STRINGP(argv[i])) \
  1058. scheme_wrong_type(scheme_name, "byte string", i, argc, argv); \
  1059. s = SCHEME_BYTE_STR_VAL(argv[i]); sl = SCHEME_BYTE_STRTAG_VAL(argv[i]); \
  1060. if (!falz) if (!(comp(scheme_name, \
  1061. (unsigned char *)prev, pl, \
  1062. (unsigned char *)s, sl) op 0)) falz = 1; \
  1063. prev = s; pl = sl; \
  1064. } \
  1065. return falz ? scheme_false : scheme_true; \
  1066. }
  1067. GEN_BYTE_STRING_COMP(byte_string_eq, "bytes=?", mz_strcmp, ==)
  1068. GEN_BYTE_STRING_COMP(byte_string_lt, "bytes<?", mz_strcmp, <)
  1069. GEN_BYTE_STRING_COMP(byte_string_gt, "bytes>?", mz_strcmp, >)
  1070. /**********************************************************************/
  1071. /* byte string <-> char string */
  1072. /**********************************************************************/
  1073. /************************* bytes->string *************************/
  1074. static Scheme_Object *
  1075. do_byte_string_to_char_string(const char *who,
  1076. Scheme_Object *bstr,
  1077. intptr_t istart, intptr_t ifinish,
  1078. int perm, int as_locale)
  1079. {
  1080. int i, ulen;
  1081. char *chars;
  1082. unsigned int *v;
  1083. chars = SCHEME_BYTE_STR_VAL(bstr);
  1084. ulen = utf8_decode_x((unsigned char *)chars, istart, ifinish,
  1085. NULL, 0, -1,
  1086. NULL, NULL, 0, 0,
  1087. NULL, 0,
  1088. (perm > -1) ? 0xD800 : 0);
  1089. if (ulen < 0) {
  1090. scheme_arg_mismatch(who,
  1091. STRING_IS_NOT_UTF_8,
  1092. bstr);
  1093. }
  1094. v = (unsigned int *)scheme_malloc_atomic((ulen + 1) * sizeof(unsigned int));
  1095. utf8_decode_x((unsigned char *)chars, istart, ifinish,
  1096. v, 0, -1,
  1097. NULL, NULL, 0, 0,
  1098. NULL, 0,
  1099. (perm > -1) ? 0xD800 : 0);
  1100. if (perm > -1) {
  1101. for (i = 0; i < ulen; i++) {
  1102. if (v[i] == 0xD800)
  1103. v[i] = perm;
  1104. }
  1105. }
  1106. v[ulen] = 0;
  1107. return scheme_make_sized_char_string(v, ulen, 0);
  1108. }
  1109. static Scheme_Object *
  1110. do_byte_string_to_char_string_locale(const char *who,
  1111. Scheme_Object *bstr,
  1112. intptr_t istart, intptr_t ifinish,
  1113. int perm)
  1114. {
  1115. char *us;
  1116. intptr_t olen;
  1117. reset_locale();
  1118. if (!iconv_ready) init_iconv();
  1119. if (mzLOCALE_IS_UTF_8(current_locale_name) || !locale_on || !mzCHK_PROC(iconv_open))
  1120. return do_byte_string_to_char_string(who, bstr, istart, ifinish, perm, 1);
  1121. if (istart < ifinish) {
  1122. int no_cvt;
  1123. us = string_to_from_locale(0, SCHEME_BYTE_STR_VAL(bstr),
  1124. istart, ifinish - istart,
  1125. &olen, perm, &no_cvt);
  1126. if (!us) {
  1127. if (no_cvt) {
  1128. return do_byte_string_to_char_string(who, bstr, istart, ifinish, perm, 1);
  1129. } else {
  1130. scheme_arg_mismatch(who,
  1131. "byte string is not a valid encoding for the current locale: ",
  1132. bstr);
  1133. }
  1134. }
  1135. ((mzchar *)us)[olen] = 0;
  1136. } else {
  1137. us = "\0\0\0";
  1138. olen = 0;
  1139. }
  1140. return scheme_make_sized_char_string((mzchar *)us, olen, 0);
  1141. }
  1142. static Scheme_Object *
  1143. do_string_to_vector(const char *who, int mode, int argc, Scheme_Object *argv[])
  1144. {
  1145. int permc;
  1146. intptr_t istart, ifinish;
  1147. if (!SCHEME_BYTE_STRINGP(argv[0]))
  1148. scheme_wrong_type(who, "byte string", 0, argc, argv);
  1149. if ((argc < 2) || SCHEME_FALSEP(argv[1]))
  1150. permc = -1;
  1151. else {
  1152. if (!SCHEME_CHARP(argv[1]))
  1153. scheme_wrong_type(who, "character or #f", 1, argc, argv);
  1154. permc = SCHEME_CHAR_VAL(argv[1]);
  1155. }
  1156. scheme_get_substring_indices(who, argv[0], argc, argv,
  1157. 2, 3,
  1158. &istart, &ifinish);
  1159. if (mode == 0)
  1160. return do_byte_string_to_char_string(who, argv[0], istart, ifinish, permc, 0);
  1161. else if (mode == 1)
  1162. return do_byte_string_to_char_string_locale(who, argv[0], istart, ifinish, permc);
  1163. else {
  1164. /* Latin-1 */
  1165. mzchar *us;
  1166. unsigned char *s;
  1167. intptr_t i, len;
  1168. len = ifinish - istart;
  1169. s = (unsigned char *)SCHEME_BYTE_STR_VAL(argv[0]);
  1170. us = (mzchar *)scheme_malloc_atomic((len + 1) * sizeof(mzchar));
  1171. for (i = istart; i < ifinish; i++) {
  1172. us[i - istart] = s[i];
  1173. }
  1174. us[len] = 0;
  1175. return scheme_make_sized_char_string(us, len, 0);
  1176. }
  1177. }
  1178. static Scheme_Object *
  1179. byte_string_to_char_string (int argc, Scheme_Object *argv[])
  1180. {
  1181. return do_string_to_vector("bytes->string/utf-8", 0, argc, argv);
  1182. }
  1183. static Scheme_Object *
  1184. byte_string_to_char_string_locale (int argc, Scheme_Object *argv[])
  1185. {
  1186. return do_string_to_vector("bytes->string/locale", 1, argc, argv);
  1187. }
  1188. static Scheme_Object *
  1189. byte_string_to_char_string_latin1 (int argc, Scheme_Object *argv[])
  1190. {
  1191. return do_string_to_vector("bytes->string/latin-1", 2, argc, argv);
  1192. }
  1193. Scheme_Object *scheme_byte_string_to_char_string(Scheme_Object *o)
  1194. {
  1195. return do_byte_string_to_char_string("s->s", o, 0, SCHEME_BYTE_STRLEN_VAL(o), 0xFFFD, 0);
  1196. }
  1197. Scheme_Object *scheme_byte_string_to_char_string_locale(Scheme_Object *o)
  1198. {
  1199. return do_byte_string_to_char_string_locale("s->s", o, 0, SCHEME_BYTE_STRLEN_VAL(o), 0xFFFD);
  1200. }
  1201. /************************* string->bytes *************************/
  1202. static Scheme_Object *do_char_string_to_byte_string(Scheme_Object *s, intptr_t istart, intptr_t ifinish,
  1203. int as_locale)
  1204. {
  1205. char *bs;
  1206. int slen;
  1207. slen = scheme_utf8_encode(SCHEME_CHAR_STR_VAL(s), istart, ifinish,
  1208. NULL, 0,
  1209. 0 /* UTF-16 */);
  1210. bs = (char *)scheme_malloc_atomic(slen + 1);
  1211. scheme_utf8_encode(SCHEME_CHAR_STR_VAL(s), istart, ifinish,
  1212. (unsigned char *)bs, 0,
  1213. 0 /* UTF-16 */);
  1214. bs[slen] = 0;
  1215. return scheme_make_sized_byte_string(bs, slen, 0);
  1216. }
  1217. static Scheme_Object *
  1218. do_char_string_to_byte_string_locale(const char *who,
  1219. Scheme_Object *cstr,
  1220. intptr_t istart, intptr_t ifinish,
  1221. int perm)
  1222. {
  1223. char *s;
  1224. intptr_t olen;
  1225. reset_locale();
  1226. if (!iconv_ready) init_iconv();
  1227. if (mzLOCALE_IS_UTF_8(current_locale_name) || !locale_on || !mzCHK_PROC(iconv_open))
  1228. return do_char_string_to_byte_string(cstr, istart, ifinish, 1);
  1229. if (istart < ifinish) {
  1230. int no_cvt;
  1231. s = string_to_from_locale(1, (char *)SCHEME_CHAR_STR_VAL(cstr),
  1232. istart, ifinish - istart,
  1233. &olen, perm, &no_cvt);
  1234. if (!s) {
  1235. if (no_cvt) {
  1236. return do_char_string_to_byte_string(cstr, istart, ifinish, 1);
  1237. } else {
  1238. scheme_arg_mismatch(who,
  1239. "string cannot be encoded for the current locale: ",
  1240. cstr);
  1241. }
  1242. }
  1243. s[olen] = 0;
  1244. } else {
  1245. s = "";
  1246. olen = 0;
  1247. }
  1248. return scheme_make_sized_byte_string(s, olen, 0);
  1249. }
  1250. Scheme_Object *scheme_char_string_to_byte_string(Scheme_Object *s)
  1251. {
  1252. return do_char_string_to_byte_string(s, 0, SCHEME_CHAR_STRLEN_VAL(s), 0);
  1253. }
  1254. Scheme_Object *scheme_char_string_to_byte_string_locale(Scheme_Object *s)
  1255. {
  1256. return do_char_string_to_byte_string_locale("s->s", s, 0, SCHEME_CHAR_STRLEN_VAL(s), '?');
  1257. }
  1258. static Scheme_Object *do_chars_to_bytes(const char *who, int mode,
  1259. int argc, Scheme_Object *argv[])
  1260. {
  1261. intptr_t istart, ifinish;
  1262. int permc;
  1263. if (!SCHEME_CHAR_STRINGP(argv[0]))
  1264. scheme_wrong_type(who, "string", 0, argc, argv);
  1265. if ((argc < 2) || SCHEME_FALSEP(argv[1]))
  1266. permc = -1;
  1267. else {
  1268. if (!SCHEME_BYTEP(argv[1]))
  1269. scheme_wrong_type(who, "byte or #f", 1, argc, argv);
  1270. permc = SCHEME_INT_VAL(argv[1]);
  1271. }
  1272. scheme_get_substring_indices(who, argv[0], argc, argv,
  1273. 2, 3, &istart, &ifinish);
  1274. if (mode == 1)
  1275. return do_char_string_to_byte_string_locale(who, argv[0], istart, ifinish, permc);
  1276. else if (mode == 0)
  1277. return do_char_string_to_byte_string(argv[0], istart, ifinish, 0);
  1278. else {
  1279. /* Latin-1 */
  1280. mzchar *us;
  1281. unsigned char *s;
  1282. intptr_t i, len;
  1283. len = ifinish - istart;
  1284. us = SCHEME_CHAR_STR_VAL(argv[0]);
  1285. s = (unsigned char *)scheme_malloc_atomic(len + 1);
  1286. for (i = istart; i < ifinish; i++) {
  1287. if (us[i] < 256)
  1288. s[i - istart] = us[i];
  1289. else if (permc >= 0) {
  1290. s[i - istart] = permc;
  1291. } else {
  1292. scheme_arg_mismatch(who,
  1293. "string cannot be encoded in Latin-1: ",
  1294. argv[0]);
  1295. }
  1296. }
  1297. s[len] = 0;
  1298. return scheme_make_sized_byte_string((char *)s, len, 0);
  1299. }
  1300. }
  1301. static Scheme_Object *char_string_to_byte_string(int argc, Scheme_Object *argv[])
  1302. {
  1303. return do_chars_to_bytes("string->bytes/utf-8", 0, argc, argv);
  1304. }
  1305. static Scheme_Object *char_string_to_byte_string_locale(int argc, Scheme_Object *argv[])
  1306. {
  1307. return do_chars_to_bytes("string->bytes/locale", 1, argc, argv);
  1308. }
  1309. static Scheme_Object *char_string_to_byte_string_latin1(int argc, Scheme_Object *argv[])
  1310. {
  1311. return do_chars_to_bytes("string->bytes/latin-1", 2, argc, argv);
  1312. }
  1313. /************************* Other *************************/
  1314. static Scheme_Object *char_string_utf8_length (int argc, Scheme_Object *argv[])
  1315. {
  1316. intptr_t istart, ifinish, len;
  1317. if (!SCHEME_CHAR_STRINGP(argv[0]))
  1318. scheme_wrong_type("string-utf-8-length", "string", 0, argc, argv);
  1319. scheme_get_substring_indices("string-utf-8-length", argv[0], argc, argv,
  1320. 1, 2, &istart, &ifinish);
  1321. len = scheme_utf8_encode(SCHEME_CHAR_STR_VAL(argv[0]), istart, ifinish,
  1322. NULL, 0, 0);
  1323. return scheme_make_integer(len);
  1324. }
  1325. static Scheme_Object *
  1326. byte_string_utf8_length (int argc, Scheme_Object *argv[])
  1327. {
  1328. int len, perm;
  1329. intptr_t istart, ifinish;
  1330. char *chars;
  1331. if (!SCHEME_BYTE_STRINGP(argv[0]))
  1332. scheme_wrong_type("bytes-utf-8-length", "string", 0, argc, argv);
  1333. chars = SCHEME_BYTE_STR_VAL(argv[0]);
  1334. if ((argc > 1) && !SCHEME_FALSEP(argv[1])) {
  1335. if (!SCHEME_CHARP(argv[1]))
  1336. scheme_wrong_type("bytes-utf-8-length", "character or #f", 1, argc, argv);
  1337. perm = 1;
  1338. } else
  1339. perm = 0;
  1340. scheme_get_substring_indices("bytes-utf-8-length", argv[0], argc, argv,
  1341. 2, 3,
  1342. &istart, &ifinish);
  1343. len = scheme_utf8_decode((unsigned char *)chars, istart, ifinish,
  1344. NULL, 0, -1,
  1345. NULL, 0, perm);
  1346. if (len < 0)
  1347. return scheme_false;
  1348. else
  1349. return scheme_make_integer(len);
  1350. }
  1351. static Scheme_Object *
  1352. byte_string_utf8_index(int argc, Scheme_Object *argv[])
  1353. {
  1354. intptr_t istart, ifinish, pos = -1, opos, ipos;
  1355. int result, perm;
  1356. char *chars;
  1357. if (!SCHEME_BYTE_STRINGP(argv[0]))
  1358. scheme_wrong_type("bytes-utf-8-index", "byte string", 0, argc, argv);
  1359. chars = SCHEME_BYTE_STR_VAL(argv[0]);
  1360. if (SCHEME_INTP(argv[1])) {
  1361. pos = SCHEME_INT_VAL(argv[1]);
  1362. } else if (SCHEME_BIGNUMP(argv[1])) {
  1363. if (SCHEME_BIGPOS(argv[1]))
  1364. pos = 0x7FFFFFFF;
  1365. }
  1366. if (pos < 0) {
  1367. scheme_wrong_type("bytes-utf-8-index", "non-negative exact integer", 1, argc, argv);
  1368. }
  1369. if ((argc > 2) && !SCHEME_FALSEP(argv[2])) {
  1370. if (!SCHEME_CHARP(argv[2]))
  1371. scheme_wrong_type("bytes-utf-8-index", "character or #f", 1, argc, argv);
  1372. perm = 1;
  1373. } else
  1374. perm = 0;
  1375. scheme_get_substring_indices("bytes-utf-8-index", argv[0], argc, argv,
  1376. 3, 4,
  1377. &istart, &ifinish);
  1378. result = utf8_decode_x((unsigned char *)chars, istart, ifinish,
  1379. NULL, 0, pos,
  1380. &ipos, &opos,
  1381. 0, 0, NULL, 0, perm ? 1 : 0);
  1382. if (((result < 0) && (result != -3))
  1383. || ((ipos == ifinish) && (opos <= pos)))
  1384. return scheme_false;
  1385. else
  1386. return scheme_make_integer(ipos);
  1387. }
  1388. static Scheme_Object *
  1389. byte_string_utf8_ref(int argc, Scheme_Object *argv[])
  1390. {
  1391. intptr_t istart, ifinish, pos = -1, opos, ipos;
  1392. char *chars;
  1393. unsigned int us[1];
  1394. Scheme_Object *perm;
  1395. if (!SCHEME_BYTE_STRINGP(argv[0]))
  1396. scheme_wrong_type("bytes-utf-8-ref", "byte string", 0, argc, argv);
  1397. chars = SCHEME_BYTE_STR_VAL(argv[0]);
  1398. if (SCHEME_INTP(argv[1])) {
  1399. pos = SCHEME_INT_VAL(argv[1]);
  1400. } else if (SCHEME_BIGNUMP(argv[1])) {
  1401. if (SCHEME_BIGPOS(argv[1]))
  1402. pos = 0x7FFFFFFF;
  1403. }
  1404. if (pos < 0) {
  1405. scheme_wrong_type("bytes-utf-8-ref", "non-negative exact integer", 1, argc, argv);
  1406. }
  1407. if ((argc > 2) && !SCHEME_FALSEP(argv[2])) {
  1408. if (!SCHEME_CHARP(argv[2]))
  1409. scheme_wrong_type("bytes-utf-8-ref", "character or #f", 1, argc, argv);
  1410. perm = argv[2];
  1411. } else
  1412. perm = 0;
  1413. scheme_get_substring_indices("bytes-utf-8-ref", argv[0], argc, argv,
  1414. 3, 4,
  1415. &istart, &ifinish);
  1416. if (pos > 0) {
  1417. utf8_decode_x((unsigned char *)chars, istart, ifinish,
  1418. NULL, 0, pos,
  1419. &ipos, &opos,
  1420. 0, 0, NULL, 0, perm ? 1 : 0);
  1421. if (opos < pos)
  1422. return scheme_false;
  1423. istart = ipos;
  1424. }
  1425. utf8_decode_x((unsigned char *)chars, istart, ifinish,
  1426. us, 0, 1,
  1427. &ipos, &opos,
  1428. 0, 0, NULL, 0, perm ? 0xFFFF : 0);
  1429. if (opos < 1)
  1430. return scheme_false;
  1431. else if (us[0] == 0xFFFF)
  1432. return perm;
  1433. else
  1434. return scheme_make_character(us[0]);
  1435. }
  1436. /********************************************************************/
  1437. /* format */
  1438. /********************************************************************/
  1439. void scheme_do_format(const char *procname, Scheme_Object *port,
  1440. const mzchar *format, int flen,
  1441. int fpos, int offset, int argc, Scheme_Object **argv)
  1442. {
  1443. int i, start, end;
  1444. int used = offset;
  1445. int num_err = 0, char_err = 0, end_ok = 0;
  1446. Scheme_Object *a[2];
  1447. if (!format) {
  1448. if (!SCHEME_CHAR_STRINGP(argv[fpos])) {
  1449. scheme_wrong_type(procname, "format-string", fpos, argc, argv);
  1450. return;
  1451. }
  1452. format = SCHEME_CHAR_STR_VAL(argv[fpos]);
  1453. flen = SCHEME_CHAR_STRTAG_VAL(argv[fpos]);
  1454. } else if (flen == -1)
  1455. flen = strlen((char *)format);
  1456. /* Check string first: */
  1457. end = flen - 1;
  1458. for (i = 0; i < end; i++) {
  1459. if (format[i] == '~') {
  1460. i++;
  1461. if (scheme_isspace(format[i])) {
  1462. /* skip spaces... */
  1463. } else switch (format[i]) {
  1464. case '~':
  1465. if (i == end)
  1466. end_ok = 1;
  1467. break;
  1468. case '%':
  1469. case 'n':
  1470. case 'N':
  1471. break;
  1472. case 'a':
  1473. case 'A':
  1474. case 's':
  1475. case 'S':
  1476. case 'v':
  1477. case 'V':
  1478. case 'e':
  1479. case 'E':
  1480. used++;
  1481. break;
  1482. case '.':
  1483. switch (format[i+1]) {
  1484. case 'a':
  1485. case 'A':
  1486. case 's':
  1487. case 'S':
  1488. case 'v':
  1489. case 'V':
  1490. break;
  1491. default:
  1492. scheme_wrong_type(procname,
  1493. "pattern-string (tag `~.' not followed by `a', `s', or `v')",
  1494. fpos, argc, argv);
  1495. break;
  1496. }
  1497. used++;
  1498. break;
  1499. case 'x':
  1500. case 'X':
  1501. case 'o':
  1502. case 'O':
  1503. case 'b':
  1504. case 'B':
  1505. if (!num_err && !char_err && (used < argc)) {
  1506. Scheme_Object *o = argv[used];
  1507. if (!SCHEME_EXACT_REALP(o)
  1508. && (!SCHEME_COMPLEXP(o)
  1509. || !SCHEME_EXACT_REALP(scheme_complex_real_part(o))))
  1510. num_err = used + 1;
  1511. }
  1512. used++;
  1513. break;
  1514. case 'c':
  1515. case 'C':
  1516. if (!num_err && !char_err && (used < argc)) {
  1517. if (!SCHEME_CHARP(argv[used]))
  1518. char_err = used + 1;
  1519. }
  1520. used++;
  1521. break;
  1522. default:
  1523. {
  1524. char buffer[64];
  1525. sprintf(buffer, "pattern-string (tag `~%c' not allowed)", format[i]);
  1526. scheme_wrong_type(procname, buffer, fpos, argc, argv);
  1527. return;
  1528. }
  1529. }
  1530. }
  1531. }
  1532. if ((format[end] == '~') && !end_ok) {
  1533. scheme_wrong_type(procname, "pattern-string (cannot end in ~)", fpos, argc, argv);
  1534. return;
  1535. }
  1536. if (used != argc) {
  1537. char *args;
  1538. intptr_t alen;
  1539. args = scheme_make_args_string("", -1, argc, argv, &alen);
  1540. if (used > argc) {
  1541. scheme_raise_exn(MZEXN_FAIL_CONTRACT,
  1542. "%s: format string requires %d arguments, given %d%t",
  1543. procname, used - offset, argc - offset, args, alen);
  1544. } else {
  1545. scheme_raise_exn(MZEXN_FAIL_CONTRACT,
  1546. "%s: format string requires %d arguments, given %d%t",
  1547. procname, used - offset, argc - offset, args, alen);
  1548. }
  1549. return;
  1550. }
  1551. if (num_err || char_err) {
  1552. int pos = (num_err ? num_err : char_err) - 1;
  1553. char *args, *bstr;
  1554. intptr_t alen;
  1555. intptr_t blen;
  1556. char *type = (num_err ? "exact-number" : "character");
  1557. Scheme_Object *bad = argv[pos];
  1558. args = scheme_make_args_string("other ", pos, argc, argv, &alen);
  1559. bstr = scheme_make_provided_string(bad, 1, &blen);
  1560. scheme_raise_exn(MZEXN_FAIL_CONTRACT,
  1561. "%s: format string requires argument of type <%s>, given %t%t",
  1562. procname, type,
  1563. bstr, blen,
  1564. args, alen);
  1565. return;
  1566. }
  1567. for (used = offset, i = start = 0; i < flen; i++) {
  1568. if (format[i] == '~') {
  1569. if (start < i) {
  1570. (void)scheme_put_char_string(procname, port, format, start, i - start);
  1571. }
  1572. i++;
  1573. if (scheme_isspace(format[i])) {
  1574. /* skip spaces (at most one newline) */
  1575. do {
  1576. if ((format[i] == '\n') || (format[i] == '\r')) {
  1577. /* got one */
  1578. if ((format[i] == '\r') && (format[i + 1] == '\n'))
  1579. i++; /* Windows-style CR-NL */
  1580. i++;
  1581. while (portable_isspace(format[i])
  1582. && !((format[i] == '\n') || (format[i] == '\r'))) {
  1583. i++;
  1584. }
  1585. break;
  1586. } else
  1587. i++;
  1588. } while (scheme_isspace(format[i]));
  1589. --i; /* back up over something */
  1590. } else switch (format[i]) {
  1591. case '~':
  1592. scheme_write_byte_string("~", 1, port);
  1593. break;
  1594. case '%':
  1595. case 'n':
  1596. case 'N':
  1597. scheme_write_byte_string("\n", 1, port);
  1598. break;
  1599. case 'c':
  1600. case 'C':
  1601. case 'a':
  1602. case 'A':
  1603. a[0] = argv[used++];
  1604. a[1] = port;
  1605. _scheme_apply(scheme_display_proc, 2, a);
  1606. break;
  1607. case 's':
  1608. case 'S':
  1609. a[0] = argv[used++];
  1610. a[1] = port;
  1611. _scheme_apply(scheme_write_proc, 2, a);
  1612. break;
  1613. case 'v':
  1614. case 'V':
  1615. a[0] = argv[used++];
  1616. a[1] = port;
  1617. _scheme_apply(scheme_print_proc, 2, a);
  1618. break;
  1619. case 'e':
  1620. case 'E':
  1621. {
  1622. intptr_t len;
  1623. char *s;
  1624. s = scheme_make_provided_string(argv[used++], 0, &len);
  1625. scheme_write_byte_string(s, len, port);
  1626. }
  1627. break;
  1628. case '.':
  1629. {
  1630. intptr_t len;
  1631. char *s;
  1632. len = scheme_get_print_width();
  1633. i++;
  1634. switch (format[i]) {
  1635. case 'a':
  1636. case 'A':
  1637. s = scheme_display_to_string_w_max(argv[used++], &len, len);
  1638. break;
  1639. case 's':
  1640. case 'S':
  1641. s = scheme_write_to_string_w_max(argv[used++], &len, len);
  1642. break;
  1643. case 'v':
  1644. case 'V':
  1645. s = scheme_print_to_string_w_max(argv[used++], &len, len);
  1646. break;
  1647. default:
  1648. s = "???";
  1649. len = 3;
  1650. }
  1651. scheme_write_byte_string(s, len, port);
  1652. }
  1653. break;
  1654. case 'x':
  1655. case 'X':
  1656. case 'o':
  1657. case 'O':
  1658. case 'b':
  1659. case 'B':
  1660. {
  1661. char *s;
  1662. int radix;
  1663. switch(format[i]) {
  1664. case 'x':
  1665. case 'X':
  1666. radix = 16;
  1667. break;
  1668. case 'o':
  1669. case 'O':
  1670. radix = 8;
  1671. break;
  1672. default:
  1673. case 'b':
  1674. case 'B':
  1675. radix = 2;
  1676. break;
  1677. }
  1678. s = scheme_number_to_string(radix, argv[used++]);
  1679. scheme_write_byte_string(s, strlen(s), port);
  1680. }
  1681. break;
  1682. }
  1683. SCHEME_USE_FUEL(1);
  1684. start = i + 1;
  1685. }
  1686. }
  1687. SCHEME_USE_FUEL(flen);
  1688. if (start < i) {
  1689. (void)scheme_put_char_string(procname, port, format, start, i - start);
  1690. }
  1691. }
  1692. char *scheme_format(mzchar *format, int flen, int argc, Scheme_Object **argv, intptr_t *rlen)
  1693. {
  1694. Scheme_Object *port;
  1695. port = scheme_make_byte_string_output_port();
  1696. scheme_do_format("format", port, format, flen, 0, 0, argc, argv);
  1697. return scheme_get_sized_byte_string_output(port, rlen);
  1698. }
  1699. void scheme_printf(mzchar *format, int flen, int argc, Scheme_Object **argv)
  1700. {
  1701. scheme_do_format("printf", scheme_get_param(scheme_current_config(), MZCONFIG_OUTPUT_PORT),
  1702. format, flen, 0, 0, argc, argv);
  1703. }
  1704. char *scheme_format_utf8(char *format, int flen, int argc, Scheme_Object **argv, intptr_t *rlen)
  1705. {
  1706. mzchar *s;
  1707. intptr_t srlen;
  1708. if (flen == -1)
  1709. flen = strlen(format);
  1710. s = scheme_utf8_decode_to_buffer_len((unsigned char *)format, flen, NULL, 0, &srlen);
  1711. if (s)
  1712. return scheme_format(s, srlen, argc, argv, rlen);
  1713. else
  1714. return "";
  1715. }
  1716. void scheme_printf_utf8(char *format, int flen, int argc, Scheme_Object **argv)
  1717. {
  1718. mzchar *s;
  1719. intptr_t srlen;
  1720. if (flen == -1)
  1721. flen = strlen(format);
  1722. s = scheme_utf8_decode_to_buffer_len((unsigned char *)format, flen, NULL, 0, &srlen);
  1723. if (s)
  1724. scheme_printf(s, srlen, argc, argv);
  1725. }
  1726. static Scheme_Object *
  1727. format(int argc, Scheme_Object *argv[])
  1728. {
  1729. Scheme_Object *port;
  1730. char *s;
  1731. intptr_t len;
  1732. port = scheme_make_byte_string_output_port();
  1733. scheme_do_format("format", port, NULL, 0, 0, 1, argc, argv);
  1734. s = scheme_get_sized_byte_string_output(port, &len);
  1735. return scheme_make_sized_utf8_string(s, len);
  1736. }
  1737. #ifdef INSTRUMENT_PRIMITIVES
  1738. extern int g_print_prims;
  1739. #endif
  1740. static Scheme_Object *
  1741. sch_printf(int argc, Scheme_Object *argv[])
  1742. {
  1743. scheme_do_format("printf", scheme_get_param(scheme_current_config(), MZCONFIG_OUTPUT_PORT),
  1744. NULL, 0, 0, 1, argc, argv);
  1745. return scheme_void;
  1746. }
  1747. static Scheme_Object *
  1748. sch_eprintf(int argc, Scheme_Object *argv[])
  1749. {
  1750. scheme_do_format("eprintf", scheme_get_param(scheme_current_config(), MZCONFIG_ERROR_PORT),
  1751. NULL, 0, 0, 1, argc, argv);
  1752. return scheme_void;
  1753. }
  1754. static Scheme_Object *
  1755. sch_fprintf(int argc, Scheme_Object *argv[])
  1756. {
  1757. if (!SCHEME_OUTPUT_PORTP(argv[0]))
  1758. scheme_wrong_type("fprintf", "output-port", 0, argc, argv);
  1759. scheme_do_format("fprintf", argv[0], NULL, 0, 1, 2, argc, argv);
  1760. return scheme_void;
  1761. }
  1762. /********************************************************************/
  1763. /* misc */
  1764. /********************************************************************/
  1765. static Scheme_Object *
  1766. version(int argc, Scheme_Object *argv[])
  1767. {
  1768. return vers_str;
  1769. }
  1770. static Scheme_Object *
  1771. banner(int argc, Scheme_Object *argv[])
  1772. {
  1773. return banner_str;
  1774. }
  1775. char *scheme_version(void)
  1776. {
  1777. return MZSCHEME_VERSION;
  1778. }
  1779. #ifdef MZ_PRECISE_GC
  1780. /* don't print " [3m]", which is the default: */
  1781. # define VERSION_SUFFIX ""
  1782. #else
  1783. # ifdef USE_SENORA_GC
  1784. # define VERSION_SUFFIX " [cgc~]"
  1785. # else
  1786. # define VERSION_SUFFIX " [cgc]"
  1787. # endif
  1788. #endif
  1789. char *scheme_banner(void)
  1790. {
  1791. if (embedding_banner)
  1792. return embedding_banner;
  1793. else
  1794. return ("Welcome to Racket"
  1795. " v" MZSCHEME_VERSION VERSION_SUFFIX
  1796. ".\n");
  1797. }
  1798. void scheme_set_banner(char *s)
  1799. {
  1800. embedding_banner = s;
  1801. }
  1802. int scheme_byte_string_has_null(Scheme_Object *o)
  1803. {
  1804. const char *s = SCHEME_BYTE_STR_VAL(o);
  1805. int i = SCHEME_BYTE_STRTAG_VAL(o);
  1806. while (i--) {
  1807. if (!s[i])
  1808. return 1;
  1809. }
  1810. return 0;
  1811. }
  1812. int scheme_any_string_has_null(Scheme_Object *o)
  1813. {
  1814. if (SCHEME_BYTE_STRINGP(o))
  1815. return scheme_byte_string_has_null(o);
  1816. else {
  1817. const mzchar *s = SCHEME_CHAR_STR_VAL(o);
  1818. int i = SCHEME_CHAR_STRTAG_VAL(o);
  1819. while (i--) {
  1820. if (!s[i])
  1821. return 1;
  1822. }
  1823. return 0;
  1824. }
  1825. }
  1826. /***********************************************************************/
  1827. /* Environment Variables */
  1828. /***********************************************************************/
  1829. #if defined(MZ_USE_PLACES) && defined(MZ_PRECISE_GC)
  1830. static char* clone_str_with_gc(const char* buffer) {
  1831. int length;
  1832. char *newbuffer;
  1833. length = strlen(buffer);
  1834. newbuffer = scheme_malloc_atomic(length+1);
  1835. memcpy(newbuffer, buffer, length+1);
  1836. return newbuffer;
  1837. }
  1838. #endif
  1839. static void create_putenv_str_table_if_needed() {
  1840. if (!putenv_str_table) {
  1841. putenv_str_table = scheme_make_hash_table(SCHEME_hash_string);
  1842. }
  1843. }
  1844. #ifndef DOS_FILE_SYSTEM
  1845. static void putenv_str_table_put_name(Scheme_Object *name, Scheme_Object *value) {
  1846. #if defined(MZ_USE_PLACES) && defined(MZ_PRECISE_GC)
  1847. void *original_gc;
  1848. Scheme_Object *name_copy;
  1849. original_gc = GC_switch_to_master_gc();
  1850. scheme_start_atomic();
  1851. name_copy = (Scheme_Object *) clone_str_with_gc((const char *) name);
  1852. create_putenv_str_table_if_needed();
  1853. scheme_hash_set(putenv_str_table, name_copy, value);
  1854. scheme_end_atomic_no_swap();
  1855. GC_switch_back_from_master(original_gc);
  1856. #else
  1857. create_putenv_str_table_if_needed();
  1858. scheme_hash_set(putenv_str_table, name, value);
  1859. #endif
  1860. }
  1861. #endif
  1862. #ifndef GETENV_FUNCTION
  1863. static void putenv_str_table_put_name_value(Scheme_Object *name, Scheme_Object *value) {
  1864. #if defined(MZ_USE_PLACES) && defined(MZ_PRECISE_GC)
  1865. void *original_gc;
  1866. Scheme_Object *name_copy;
  1867. Scheme_Object *value_copy;
  1868. original_gc = GC_switch_to_master_gc();
  1869. scheme_start_atomic();
  1870. name_copy = (Scheme_Object *) clone_str_with_gc((const char *) name);
  1871. value_copy = (Scheme_Object *) clone_str_with_gc((const char *) value);
  1872. create_putenv_str_table_if_needed();
  1873. scheme_hash_set(putenv_str_table, name_copy, value_copy);
  1874. scheme_end_atomic_no_swap();
  1875. GC_switch_back_from_master(original_gc);
  1876. #else
  1877. create_putenv_str_table_if_needed();
  1878. scheme_hash_set(putenv_str_table, name, value);
  1879. #endif
  1880. }
  1881. #endif
  1882. #if !defined(GETENV_FUNCTION) || defined(MZ_PRECISE_GC)
  1883. static Scheme_Object *putenv_str_table_get(Scheme_Object *name) {
  1884. #if defined(MZ_USE_PLACES) && defined(MZ_PRECISE_GC)
  1885. void *original_gc;
  1886. Scheme_Object *value;
  1887. original_gc = GC_switch_to_master_gc();
  1888. scheme_start_atomic();
  1889. create_putenv_str_table_if_needed();
  1890. value = scheme_hash_get(putenv_str_table, name);
  1891. scheme_end_atomic_no_swap();
  1892. GC_switch_back_from_master(original_gc);
  1893. return value;
  1894. #else
  1895. create_putenv_str_table_if_needed();
  1896. return scheme_hash_get(putenv_str_table, name);
  1897. #endif
  1898. }
  1899. #endif
  1900. static int sch_bool_getenv(const char* name);
  1901. void
  1902. scheme_init_getenv(void)
  1903. {
  1904. #ifndef GETENV_FUNCTION
  1905. FILE *f = fopen("Environment", "r");
  1906. if (f) {
  1907. Scheme_Object *p = scheme_make_file_input_port(f);
  1908. mz_jmp_buf *savebuf, newbuf;
  1909. savebuf = scheme_current_thread->error_buf;
  1910. scheme_current_thread->error_buf = &newbuf;
  1911. if (!scheme_setjmp(newbuf)) {
  1912. while (1) {
  1913. Scheme_Object *v = scheme_read(p);
  1914. if (SCHEME_EOFP(v))
  1915. break;
  1916. if (SCHEME_PAIRP(v) && SCHEME_PAIRP(SCHEME_CDR(v))
  1917. && SCHEME_NULLP(SCHEME_CDR(SCHEME_CDR(v)))) {
  1918. Scheme_Object *key = SCHEME_CAR(v);
  1919. Scheme_Object *val = SCHEME_CADR(v);
  1920. if (SCHEME_STRINGP(key) && SCHEME_STRINGP(val)) {
  1921. Scheme_Object *a[2];
  1922. a[0] = key;
  1923. a[1] = val;
  1924. sch_putenv(2, a);
  1925. v = NULL;
  1926. }
  1927. }
  1928. if (v)
  1929. scheme_signal_error("bad environment specification: %V", v);
  1930. }
  1931. }
  1932. scheme_current_thread->error_buf = savebuf;
  1933. scheme_close_input_port(p);
  1934. }
  1935. #endif
  1936. if (sch_bool_getenv("PLTNOMZJIT")) {
  1937. scheme_set_startup_use_jit(0);
  1938. }
  1939. }
  1940. #ifdef DOS_FILE_SYSTEM
  1941. # include <windows.h>
  1942. static char *dos_win_getenv(const char *name) {
  1943. int value_size;
  1944. value_size = GetEnvironmentVariable(name, NULL, 0);
  1945. if (value_size) {
  1946. char *value;
  1947. int got;
  1948. value = scheme_malloc_atomic(value_size);
  1949. got = GetEnvironmentVariable(name, value, value_size);
  1950. if (got < value_size)
  1951. value[got] = 0;
  1952. return value;
  1953. }
  1954. return NULL;
  1955. }
  1956. #endif
  1957. static int sch_bool_getenv(const char* name) {
  1958. int rc = 0;
  1959. #ifdef GETENV_FUNCTION
  1960. # ifdef DOS_FILE_SYSTEM
  1961. if (GetEnvironmentVariable(name, NULL, 0)) rc = 1;
  1962. # else
  1963. if (getenv(name)) rc = 1;
  1964. # endif
  1965. #else
  1966. if (putenv_str_table_get(name)) rc = 1;
  1967. #endif
  1968. return rc;
  1969. }
  1970. static Scheme_Object *sch_getenv(int argc, Scheme_Object *argv[])
  1971. {
  1972. char *name;
  1973. char *value;
  1974. Scheme_Object *bs;
  1975. if (!SCHEME_CHAR_STRINGP(argv[0]) || scheme_any_string_has_null(argv[0]))
  1976. scheme_wrong_type("getenv", CHAR_STRING_W_NO_NULLS, 0, argc, argv);
  1977. bs = scheme_char_string_to_byte_string_locale(argv[0]);
  1978. name = SCHEME_BYTE_STR_VAL(bs);
  1979. #ifdef GETENV_FUNCTION
  1980. # ifdef DOS_FILE_SYSTEM
  1981. value = dos_win_getenv(name);
  1982. # else
  1983. value = getenv(name);
  1984. # endif
  1985. #else
  1986. {
  1987. Scheme_Object *hash_value;
  1988. hash_value = putenv_str_table_get(name);
  1989. return hash_value ? hash_value : scheme_false;
  1990. }
  1991. #endif
  1992. return value ? scheme_make_locale_string(value) : scheme_false;
  1993. }
  1994. #ifndef DOS_FILE_SYSTEM
  1995. static int sch_unix_putenv(const char *var, const char *val, const intptr_t varlen, const intptr_t vallen) {
  1996. char *buffer;
  1997. intptr_t total_length;
  1998. total_length = varlen + vallen + 2;
  1999. #ifdef MZ_PRECISE_GC
  2000. /* Can't put moveable string into array. */
  2001. buffer = malloc(total_length);
  2002. #else
  2003. buffer = (char *)scheme_malloc_atomic(total_length);
  2004. #endif
  2005. memcpy(buffer, var, varlen);
  2006. buffer[varlen] = '=';
  2007. memcpy(buffer + varlen + 1, val, vallen + 1);
  2008. #ifdef MZ_PRECISE_GC
  2009. {
  2010. /* Free old, if in table: */
  2011. char *oldbuffer;
  2012. oldbuffer = (char *)putenv_str_table_get((Scheme_Object *)var);
  2013. if (oldbuffer)
  2014. free(oldbuffer);
  2015. }
  2016. #endif
  2017. /* if precise the buffer needs to be remembered so it can be freed */
  2018. /* if not precise the buffer needs to be rooted so it doesn't get collected prematurely */
  2019. putenv_str_table_put_name((Scheme_Object *)var, (Scheme_Object *)buffer);
  2020. return putenv(buffer);
  2021. }
  2022. #endif
  2023. static Scheme_Object *sch_putenv(int argc, Scheme_Object *argv[])
  2024. {
  2025. Scheme_Object *varbs;
  2026. Scheme_Object *valbs;
  2027. char *var;
  2028. char *val;
  2029. int rc = 0;
  2030. if (!SCHEME_CHAR_STRINGP(argv[0]) || scheme_any_string_has_null(argv[0]))
  2031. scheme_wrong_type("putenv", CHAR_STRING_W_NO_NULLS, 0, argc, argv);
  2032. if (!SCHEME_CHAR_STRINGP(argv[1]) || scheme_any_string_has_null(argv[1]))
  2033. scheme_wrong_type("putenv", CHAR_STRING_W_NO_NULLS, 1, argc, argv);
  2034. varbs = scheme_char_string_to_byte_string_locale(argv[0]);
  2035. var = SCHEME_BYTE_STR_VAL(varbs);
  2036. valbs = scheme_char_string_to_byte_string_locale(argv[1]);
  2037. val = SCHEME_BYTE_STR_VAL(valbs);
  2038. #ifdef GETENV_FUNCTION
  2039. # ifdef DOS_FILE_SYSTEM
  2040. rc = !SetEnvironmentVariable(var, val);
  2041. # else
  2042. rc = sch_unix_putenv(var, val, SCHEME_BYTE_STRLEN_VAL(varbs), SCHEME_BYTE_STRLEN_VAL(valbs));
  2043. # endif
  2044. #else
  2045. putenv_str_table_put_name_value(argv[0], argv[1]);
  2046. #endif
  2047. return rc ? scheme_false : scheme_true;
  2048. }
  2049. /***********************************************************************/
  2050. /* End Environment Variables */
  2051. /***********************************************************************/
  2052. static void machine_details(char *s);
  2053. static Scheme_Object *system_type(int argc, Scheme_Object *argv[])
  2054. {
  2055. if (argc) {
  2056. Scheme_Object *sym;
  2057. sym = scheme_intern_symbol("link");
  2058. if (SAME_OBJ(argv[0], sym)) {
  2059. #if defined(OS_X) && !defined(XONX)
  2060. return scheme_intern_symbol("framework");
  2061. #else
  2062. # ifdef DOS_FILE_SYSTEM
  2063. return scheme_intern_symbol("dll");
  2064. # else
  2065. # ifdef MZ_USES_SHARED_LIB
  2066. return scheme_intern_symbol("shared");
  2067. # else
  2068. return scheme_intern_symbol("static");
  2069. # endif
  2070. # endif
  2071. #endif
  2072. }
  2073. sym = scheme_intern_symbol("machine");
  2074. if (SAME_OBJ(argv[0], sym)) {
  2075. char buff[1024];
  2076. machine_details(buff);
  2077. return scheme_make_utf8_string(buff);
  2078. }
  2079. sym = scheme_intern_symbol("gc");
  2080. if (SAME_OBJ(argv[0], sym)) {
  2081. #ifdef MZ_PRECISE_GC
  2082. return scheme_intern_symbol("3m");
  2083. #else
  2084. return scheme_intern_symbol("cgc");
  2085. #endif
  2086. }
  2087. sym = scheme_intern_symbol("so-suffix");
  2088. if (SAME_OBJ(argv[0], sym)) {
  2089. #ifdef DOS_FILE_SYSTEM
  2090. return scheme_make_byte_string(".dll");
  2091. #else
  2092. # ifdef OS_X
  2093. return scheme_make_byte_string(".dylib");
  2094. # else
  2095. # ifdef USE_CYGWIN_SO_SUFFIX
  2096. return scheme_make_byte_string(".dll");
  2097. # else
  2098. return scheme_make_byte_string(".so");
  2099. # endif
  2100. # endif
  2101. #endif
  2102. }
  2103. sym = scheme_intern_symbol("os");
  2104. if (!SAME_OBJ(argv[0], sym)) {
  2105. scheme_wrong_type("system-type", "'os, 'link, 'machine, 'gc, or 'so-suffix", 0, argc, argv);
  2106. return NULL;
  2107. }
  2108. }
  2109. return sys_symbol;
  2110. }
  2111. static Scheme_Object *system_library_subpath(int argc, Scheme_Object *argv[])
  2112. {
  2113. if (argc > 0) {
  2114. Scheme_Object *sym;
  2115. if (SCHEME_FALSEP(argv[0]))
  2116. return platform_cgc_path;
  2117. sym = scheme_intern_symbol("cgc");
  2118. if (SAME_OBJ(sym, argv[0]))
  2119. return platform_cgc_path;
  2120. sym = scheme_intern_symbol("3m");
  2121. if (SAME_OBJ(sym, argv[0]))
  2122. return platform_3m_path;
  2123. scheme_wrong_type("system-library-subpath", "'cgc, '3m, or #f", 0, argc, argv);
  2124. return NULL;
  2125. } else {
  2126. #ifdef MZ_PRECISE_GC
  2127. return platform_3m_path;
  2128. #else
  2129. return platform_cgc_path;
  2130. #endif
  2131. }
  2132. }
  2133. const char *scheme_system_library_subpath()
  2134. {
  2135. return SCHEME_PLATFORM_LIBRARY_SUBPATH;
  2136. }
  2137. /* Our own strncpy - which would be really stupid, except the one for
  2138. the implementation in Solaris 2.6 is broken (it doesn't always stop
  2139. at the null terminator). */
  2140. int scheme_strncmp(const char *a, const char *b, int len)
  2141. {
  2142. while (len-- && (*a == *b) && *a) {
  2143. a++;
  2144. b++;
  2145. }
  2146. if (len < 0)
  2147. return 0;
  2148. else
  2149. return *a - *b;
  2150. }
  2151. static Scheme_Object *ok_cmdline(int argc, Scheme_Object **argv)
  2152. {
  2153. if (SCHEME_CHAPERONE_VECTORP(argv[0])) {
  2154. Scheme_Object *vec = argv[0], *vec2, *str;
  2155. int i, size = SCHEME_VEC_SIZE(vec);
  2156. if (!size)
  2157. return vec;
  2158. for (i = 0; i < size; i++) {
  2159. if (!SCHEME_CHAR_STRINGP(SCHEME_VEC_ELS(vec)[i]))
  2160. return NULL;
  2161. }
  2162. /* Make sure vector and strings are immutable: */
  2163. vec2 = scheme_make_vector(size, NULL);
  2164. if (size)
  2165. SCHEME_SET_VECTOR_IMMUTABLE(vec2);
  2166. for (i = 0; i < size; i++) {
  2167. str = SCHEME_VEC_ELS(vec)[i];
  2168. if (!SCHEME_IMMUTABLE_CHAR_STRINGP(str)) {
  2169. str = scheme_make_sized_char_string(SCHEME_CHAR_STR_VAL(str), SCHEME_CHAR_STRLEN_VAL(str), 0);
  2170. SCHEME_SET_CHAR_STRING_IMMUTABLE(str);
  2171. }
  2172. SCHEME_VEC_ELS(vec2)[i] = str;
  2173. }
  2174. return vec2;
  2175. }
  2176. return NULL;
  2177. }
  2178. static Scheme_Object *cmdline_args(int argc, Scheme_Object *argv[])
  2179. {
  2180. return scheme_param_config("current-command-line-arguments",
  2181. scheme_make_integer(MZCONFIG_CMDLINE_ARGS),
  2182. argc, argv,
  2183. -1, ok_cmdline, "vector of strings", 1);
  2184. }
  2185. /**********************************************************************/
  2186. /* locale ops */
  2187. /**********************************************************************/
  2188. static Scheme_Object *ok_locale(int argc, Scheme_Object **argv)
  2189. {
  2190. if (SCHEME_FALSEP(argv[0]))
  2191. return argv[0];
  2192. else if (SCHEME_CHAR_STRINGP(argv[0])) {
  2193. if (SCHEME_IMMUTABLEP(argv[0]))
  2194. return argv[0];
  2195. else {
  2196. Scheme_Object *str = argv[0];
  2197. str = scheme_make_immutable_sized_char_string(SCHEME_CHAR_STR_VAL(str), SCHEME_CHAR_STRLEN_VAL(str), 1);
  2198. return str;
  2199. }
  2200. }
  2201. return NULL;
  2202. }
  2203. static Scheme_Object *current_locale(int argc, Scheme_Object *argv[])
  2204. {
  2205. Scheme_Object *v;
  2206. v = scheme_param_config("current-locale",
  2207. scheme_make_integer(MZCONFIG_LOCALE),
  2208. argc, argv,
  2209. -1, ok_locale, "#f or string", 1);
  2210. return v;
  2211. }
  2212. static Scheme_Object *locale_string_encoding(int argc, Scheme_Object *argv[])
  2213. {
  2214. reset_locale();
  2215. if (mzLOCALE_IS_UTF_8(current_locale_name) || !locale_on)
  2216. return scheme_make_utf8_string("UTF-8");
  2217. #if HAVE_CODESET
  2218. return scheme_make_utf8_string(nl_langinfo(CODESET));
  2219. #else
  2220. /* nl_langinfo doesn't work, so just make up something */
  2221. return scheme_make_utf8_string("UTF-8");
  2222. #endif
  2223. }
  2224. static Scheme_Object *system_language_country(int argc, Scheme_Object *argv[])
  2225. {
  2226. #ifdef MACOS_UNICODE_SUPPORT
  2227. /* Mac OS X */
  2228. CFLocaleRef l;
  2229. CFStringRef s;
  2230. int len;
  2231. char *r;
  2232. l = CFLocaleCopyCurrent();
  2233. s = CFLocaleGetIdentifier(l);
  2234. len = CFStringGetLength(s);
  2235. r = (char *)scheme_malloc_atomic(len * 6 + 1);
  2236. CFStringGetCString(s, r, len * 6 + 1, kCFStringEncodingUTF8);
  2237. CFRelease(l);
  2238. return scheme_make_sized_utf8_string(r, 5);
  2239. #else
  2240. # ifdef WINDOWS_UNICODE_SUPPORT
  2241. /* Windows */
  2242. LCID l;
  2243. int llen, clen;
  2244. char *lang, *country, *s;
  2245. l = GetUserDefaultLCID();
  2246. llen = GetLocaleInfo(l, LOCALE_SENGLANGUAGE, NULL, 0);
  2247. lang = (char *)scheme_malloc_atomic(llen);
  2248. GetLocaleInfo(l, LOCALE_SENGLANGUAGE, lang, llen);
  2249. if (llen)
  2250. llen -= 1; /* drop nul terminator */
  2251. clen = GetLocaleInfo(l, LOCALE_SENGCOUNTRY, NULL, 0);
  2252. country = (char *)scheme_malloc_atomic(clen);
  2253. GetLocaleInfo(l, LOCALE_SENGCOUNTRY, country, clen);
  2254. if (clen)
  2255. clen -= 1; /* drop nul terminator */
  2256. s = (char *)scheme_malloc_atomic(clen + llen + 1);
  2257. memcpy(s, lang, llen);
  2258. memcpy(s + 1 + llen, country, clen);
  2259. s[llen] = '_';
  2260. return scheme_make_sized_utf8_string(s, llen + 1 + clen);
  2261. # else
  2262. /* Unix */
  2263. char *s;
  2264. s = getenv("LC_ALL");
  2265. if (!s)
  2266. s = getenv("LC_CTYPE");
  2267. if (!s)
  2268. s = getenv("LANG");
  2269. if (s) {
  2270. /* Check that the environment variable has the form
  2271. xx_XX[.ENC] */
  2272. if ((s[0] >= 'a') && (s[0] <= 'z')
  2273. && (s[1] >= 'a') && (s[1] <= 'z')
  2274. && (s[2] == '_')
  2275. && (s[3] >= 'A') && (s[3] <= 'Z')
  2276. && (s[4] >= 'A') && (s[4] <= 'Z')
  2277. && (!s[5] || s[5] == '.')) {
  2278. /* Good */
  2279. } else
  2280. s = NULL;
  2281. }
  2282. if (!s)
  2283. s = "en_US";
  2284. return scheme_make_sized_utf8_string(s, 5);
  2285. # endif
  2286. #endif
  2287. }
  2288. #ifndef DONT_USE_LOCALE
  2289. #define ICONV_ARG_CAST /* empty */
  2290. static char *do_convert(iconv_t cd,
  2291. /* if cd == -1 and either from_e or to_e can be NULL, then
  2292. reset_locale() must have been called */
  2293. const char *from_e, const char *to_e,
  2294. /* 1 => UCS-4 -> UTF-8; 2 => UTF-8 -> UCS-4; 0 => other */
  2295. int to_from_utf8,
  2296. /* in can be NULL to output just a shift; in that case,
  2297. id should be 0, too */
  2298. char *in, int id, int iilen,
  2299. char *out, int od, int iolen,
  2300. /* if grow, then reallocate when out isn't big enough */
  2301. int grow,
  2302. /* if add_end_shift, add a shift sequence to the end;
  2303. not useful if in is already NULL to indicate a shift */
  2304. int add_end_shift,
  2305. /* extra specifies the length of a terminator,
  2306. not included in iolen or *oolen */
  2307. int extra,
  2308. /* these two report actual read/wrote sizes: */
  2309. intptr_t *oilen, intptr_t *oolen,
  2310. /* status is set to
  2311. 0 for complete,
  2312. -1 for partial input,
  2313. -2 for error,
  2314. 1 for more avail */
  2315. int *status)
  2316. {
  2317. int dip, dop, close_it = 0, mz_utf8 = 0;
  2318. size_t il, ol, r;
  2319. GC_CAN_IGNORE char *ip, *op;
  2320. /* Defaults: */
  2321. *status = -1;
  2322. if (oilen)
  2323. *oilen = 0;
  2324. *oolen = 0;
  2325. if (cd == (iconv_t)-1) {
  2326. if (!iconv_ready) init_iconv();
  2327. if (mzCHK_PROC(iconv_open)) {
  2328. if (!from_e)
  2329. from_e = mz_iconv_nl_langinfo();
  2330. if (!to_e)
  2331. to_e = mz_iconv_nl_langinfo();
  2332. cd = iconv_open(to_e, from_e);
  2333. close_it = 1;
  2334. } else if (to_from_utf8) {
  2335. /* Assume UTF-8 */
  2336. mz_utf8 = 1;
  2337. }
  2338. }
  2339. if ((cd == (iconv_t)-1) && !mz_utf8) {
  2340. if (out) {
  2341. while (extra--) {
  2342. out[extra] = 0;
  2343. }
  2344. }
  2345. return out;
  2346. }
  2347. /* The converter is ready. Allocate out space, if necessary */
  2348. if (!out) {
  2349. if (iolen <= 0)
  2350. iolen = iilen;
  2351. out = (char *)scheme_malloc_atomic(iolen + extra);
  2352. od = 0;
  2353. }
  2354. /* il and ol are the number of available chars */
  2355. il = iilen;
  2356. ol = iolen;
  2357. /* dip and dop are the number of characters read so far;
  2358. we use these and NULL out the ip and op pointers
  2359. for the sake of precise GC */
  2360. dip = 0;
  2361. dop = 0;
  2362. if (!in)
  2363. add_end_shift = 0;
  2364. while (1) {
  2365. int icerr;
  2366. if (mz_utf8) {
  2367. /* Use our UTF-8 routines as if they were iconv */
  2368. if (to_from_utf8 == 1) {
  2369. /* UCS-4 -> UTF-8 */
  2370. /* We assume that in + id and iilen are mzchar-aligned */
  2371. int opos, uid, uilen;
  2372. uid = (id + dip) >> 2;
  2373. uilen = (iilen - dip) >> 2;
  2374. opos = scheme_utf8_encode((const unsigned int *)in, uid, uilen,
  2375. NULL, 0,
  2376. 0);
  2377. if (opos <= iolen) {
  2378. opos = scheme_utf8_encode((const unsigned int *)in, uid, uilen,
  2379. (unsigned char *)out, od + dop,
  2380. 0);
  2381. dop += opos;
  2382. dip += iilen;
  2383. icerr = 0;
  2384. r = (size_t)opos;
  2385. } else {
  2386. icerr = E2BIG;
  2387. r = (size_t)-1;
  2388. }
  2389. } else {
  2390. /* UTF-8 -> UCS-4 */
  2391. /* We assume that out + od is mzchar-aligned */
  2392. intptr_t ipos, opos;
  2393. r = utf8_decode_x((unsigned char *)in, id + dip, iilen,
  2394. (unsigned int *)out, (od + dop) >> 2, iolen >> 2,
  2395. &ipos, &opos,
  2396. 0, 0, NULL, 0, 0);
  2397. opos <<= 2;
  2398. dop = (opos - od);
  2399. dip = (ipos - id);
  2400. if ((r == -1) || (r == -2)) {
  2401. r = (size_t)-1;
  2402. icerr = EILSEQ;
  2403. } else if (r == -3) {
  2404. icerr = E2BIG;
  2405. r = (size_t)-1;
  2406. } else
  2407. icerr = 0;
  2408. }
  2409. } else {
  2410. ip = in XFORM_OK_PLUS id + dip;
  2411. op = out XFORM_OK_PLUS od + dop;
  2412. r = iconv(cd, ICONV_ARG_CAST &ip, &il, &op, &ol);
  2413. dip = ip - (in XFORM_OK_PLUS id);
  2414. dop = op - (out XFORM_OK_PLUS od);
  2415. ip = op = NULL;
  2416. icerr = ICONV_errno;
  2417. }
  2418. /* Record how many chars processed, now */
  2419. if (oilen)
  2420. *oilen = dip;
  2421. *oolen = dop;
  2422. /* Got all the chars? */
  2423. if (r == (size_t)-1) {
  2424. if (icerr == E2BIG) {
  2425. if (grow) {
  2426. /* Double the string size and try again */
  2427. char *naya;
  2428. naya = (char *)scheme_malloc_atomic((iolen * 2) + extra);
  2429. memcpy(naya, out + od, *oolen);
  2430. ol += iolen;
  2431. iolen += iolen;
  2432. out = naya;
  2433. od = 0;
  2434. } else {
  2435. *status = 1;
  2436. if (close_it)
  2437. iconv_close(cd);
  2438. while (extra--) {
  2439. out[od + dop + extra] = 0;
  2440. }
  2441. return out;
  2442. }
  2443. } else {
  2444. /* Either EINVAL (premature end) or EILSEQ (bad sequence) */
  2445. if (icerr == EILSEQ)
  2446. *status = -2;
  2447. if (close_it)
  2448. iconv_close(cd);
  2449. while (extra--) {
  2450. out[od + dop + extra] = 0;
  2451. }
  2452. return out;
  2453. }
  2454. } else {
  2455. /* All done... */
  2456. if (add_end_shift) {
  2457. add_end_shift = 0;
  2458. in = NULL;
  2459. dip = 0;
  2460. id = 0;
  2461. il = 0; /* should be redundant */
  2462. oilen = NULL; /* so it doesn't get set to 0 */
  2463. } else {
  2464. *status = 0;
  2465. if (close_it)
  2466. iconv_close(cd);
  2467. while (extra--) {
  2468. out[od + dop + extra] = 0;
  2469. }
  2470. return out;
  2471. }
  2472. }
  2473. }
  2474. }
  2475. #define MZ_SC_BUF_SIZE 32
  2476. static char *string_to_from_locale(int to_bytes,
  2477. char *in, intptr_t delta, intptr_t len,
  2478. intptr_t *olen, int perm,
  2479. int *no_cvt)
  2480. /* Call this function only when iconv is available, and only when
  2481. reset_locale() has been called */
  2482. {
  2483. Scheme_Object *parts = scheme_null, *one;
  2484. char *c;
  2485. intptr_t clen, used;
  2486. int status;
  2487. iconv_t cd;
  2488. if (!iconv_ready) init_iconv();
  2489. if (to_bytes)
  2490. cd = iconv_open(mz_iconv_nl_langinfo(), MZ_UCS4_NAME);
  2491. else
  2492. cd = iconv_open(MZ_UCS4_NAME, mz_iconv_nl_langinfo());
  2493. if (cd == (iconv_t)-1) {
  2494. *no_cvt = 1;
  2495. return NULL;
  2496. }
  2497. *no_cvt = 0;
  2498. while (len) {
  2499. /* We might have conversion errors... */
  2500. c = do_convert(cd, NULL, NULL, 0,
  2501. (char *)in, (to_bytes ? 4 : 1) * delta, (to_bytes ? 4 : 1) * len,
  2502. NULL, 0, (to_bytes ? 1 : 4) * (len + 1),
  2503. 1 /* grow */, 1, (to_bytes ? 1 : 4) /* terminator size */,
  2504. &used, &clen,
  2505. &status);
  2506. if (to_bytes)
  2507. used >>= 2;
  2508. if ((perm < 0) && (used < len)) {
  2509. iconv_close(cd);
  2510. return NULL;
  2511. }
  2512. delta += used;
  2513. len -= used;
  2514. if (!len && SCHEME_NULLP(parts)) {
  2515. if (to_bytes) {
  2516. *olen = clen;
  2517. c[*olen] = 0;
  2518. } else {
  2519. *olen = (clen >> 2);
  2520. ((mzchar *)c)[*olen] = 0;
  2521. }
  2522. iconv_close(cd);
  2523. return c;
  2524. }
  2525. /* We can get here if there was some conversion error at some
  2526. point. We're building up a list of parts. */
  2527. if (to_bytes) {
  2528. one = scheme_make_sized_byte_string(c, clen, 0);
  2529. } else {
  2530. one = scheme_make_sized_char_string((mzchar *)c, clen >> 2, 0);
  2531. }
  2532. parts = scheme_make_pair(one, parts);
  2533. if (len) {
  2534. /* Conversion error, so skip one char. */
  2535. if (to_bytes) {
  2536. char bc[1];
  2537. bc[0] = perm;
  2538. one = scheme_make_sized_byte_string(bc, 1, 1);
  2539. } else {
  2540. mzchar bc[1];
  2541. bc[0] = perm;
  2542. one = scheme_make_sized_char_string(bc, 1, 1);
  2543. }
  2544. parts = scheme_make_pair(one, parts);
  2545. delta += 1;
  2546. len -= 1;
  2547. }
  2548. }
  2549. iconv_close(cd);
  2550. if (to_bytes) {
  2551. parts = append_all_byte_strings_backwards(parts);
  2552. *olen = SCHEME_BYTE_STRTAG_VAL(parts);
  2553. return SCHEME_BYTE_STR_VAL(parts);
  2554. } else {
  2555. parts = append_all_strings_backwards(parts);
  2556. *olen = SCHEME_CHAR_STRTAG_VAL(parts);
  2557. return (char *)SCHEME_CHAR_STR_VAL(parts);
  2558. }
  2559. }
  2560. static char *locale_recase(int to_up,
  2561. /* in must be null-terminated, iilen doesn't include it */
  2562. char *in, int id, int iilen,
  2563. /* iolen, in contrast, includes the terminator */
  2564. char *out, int od, int iolen,
  2565. intptr_t *oolen)
  2566. /* Assumes that reset_locale() has been called */
  2567. {
  2568. #ifdef NO_MBTOWC_FUNCTIONS
  2569. /* No wide-char functions...
  2570. The C library's toupper and tolower is supposed to be
  2571. locale-sensitive. It can't be right for characters that are
  2572. encoded in multiple bytes, but probably it will do the right
  2573. thing in common cases. */
  2574. int i;
  2575. /* First, copy "in" to "out" */
  2576. if (iilen + 1 >= iolen) {
  2577. out = (char *)scheme_malloc_atomic(iilen + 1);
  2578. od = 0;
  2579. }
  2580. memcpy(out + od, in + id, iilen);
  2581. out[od + iilen] = 0;
  2582. *oolen = iilen;
  2583. /* Re-case chars in "out" */
  2584. for (i = 0; i < iilen; i++) {
  2585. if (to_up)
  2586. out[od + i] = toupper(out[od + i]);
  2587. else
  2588. out[od + i] = tolower(out[od + i]);
  2589. }
  2590. return out;
  2591. #else
  2592. /* To change the case, convert the string to multibyte, re-case the
  2593. multibyte, then convert back. */
  2594. # define MZ_WC_BUF_SIZE 32
  2595. GC_CAN_IGNORE mbstate_t state;
  2596. size_t wl, wl2, ml, ml2;
  2597. wchar_t *wc, *ws, wcbuf[MZ_WC_BUF_SIZE], cwc;
  2598. const char *s;
  2599. unsigned int j;
  2600. /* The "n" versions are apparently not too standard: */
  2601. # define mz_mbsnrtowcs(t, f, fl, tl, s) mbsrtowcs(t, f, tl, s)
  2602. # define mz_wcsnrtombs(t, f, fl, tl, s) wcsrtombs(t, f, tl, s)
  2603. /* ----- to wide char ---- */
  2604. /* Get length */
  2605. memset(&state, 0, sizeof(mbstate_t));
  2606. s = in XFORM_OK_PLUS id;
  2607. wl = mz_mbsnrtowcs(NULL, &s, iilen, 0, &state);
  2608. s = NULL;
  2609. /* Allocate space */
  2610. if (wl < MZ_WC_BUF_SIZE) {
  2611. wc = wcbuf;
  2612. } else {
  2613. wc = (wchar_t *)scheme_malloc_atomic(sizeof(wchar_t) * (wl + 1));
  2614. }
  2615. /* Convert */
  2616. memset(&state, 0, sizeof(mbstate_t));
  2617. s = in XFORM_OK_PLUS id;
  2618. wl2 = mz_mbsnrtowcs(wc, &s, iilen, wl + 1, &state);
  2619. s = NULL;
  2620. wc[wl] = 0; /* just in case */
  2621. /* ---- re-case ---- */
  2622. if (to_up) {
  2623. for (j = 0; j < wl; j++) {
  2624. cwc = towupper(wc[j]);
  2625. wc[j] = cwc;
  2626. }
  2627. } else {
  2628. for (j = 0; j < wl; j++) {
  2629. cwc = towlower(wc[j]);
  2630. wc[j] = cwc;
  2631. }
  2632. }
  2633. /* ---- back to multibyte ---- */
  2634. /* Measure */
  2635. memset(&state, 0, sizeof(mbstate_t));
  2636. ws = wc;
  2637. ml = mz_wcsnrtombs(NULL, (const wchar_t **)&ws, wl, 0, &state);
  2638. ws = NULL;
  2639. /* Allocate space */
  2640. *oolen = ml;
  2641. if (ml + 1 >= (unsigned int)iolen) {
  2642. out = (char *)scheme_malloc_atomic(ml + 1);
  2643. od = 0;
  2644. }
  2645. /* Convert */
  2646. memset(&state, 0, sizeof(mbstate_t));
  2647. ws = wc;
  2648. ml2 = mz_wcsnrtombs(out + od, (const wchar_t **)&ws, wl, ml + 1, &state);
  2649. ws = NULL;
  2650. out[od + ml] = 0;
  2651. return out;
  2652. #endif
  2653. }
  2654. int mz_locale_strcoll(char *s1, int d1, int l1, char *s2, int d2, int l2, int cvt_case)
  2655. /* The s1 and s2 arguments are actually UCS-4.
  2656. Assumes that reset_locale() has been called. */
  2657. {
  2658. intptr_t clen1, clen2, used1, used2, origl1, origl2;
  2659. char *c1, *c2, buf1[MZ_SC_BUF_SIZE], buf2[MZ_SC_BUF_SIZE];
  2660. char case_buf1[MZ_SC_BUF_SIZE], case_buf2[MZ_SC_BUF_SIZE];
  2661. int status, got_more;
  2662. /* First, convert UCS-4 to locale-specific encoding. If some
  2663. characters don't fit into the encoding, then we'll have leftover
  2664. characters. Count unconvertable charc as greater than anything
  2665. that can be converted */
  2666. origl1 = l1;
  2667. origl2 = l2;
  2668. /* Loop to check both convertable and unconvertable parts */
  2669. while (1) {
  2670. if (!origl1 && !origl2)
  2671. return 0;
  2672. if (!origl1)
  2673. return -1;
  2674. if (!origl2)
  2675. return 1;
  2676. /* Loop to get consistent parts of the wto strings, in case
  2677. a conversion fails. */
  2678. got_more = 0;
  2679. l1 = origl1;
  2680. l2 = origl2;
  2681. while (1) {
  2682. c1 = do_convert((iconv_t)-1, MZ_UCS4_NAME, NULL, 1,
  2683. s1, d1 * 4, 4 * l1,
  2684. buf1, 0, MZ_SC_BUF_SIZE - 1,
  2685. 1 /* grow */, 0, 1 /* terminator size */,
  2686. &used1, &clen1,
  2687. &status);
  2688. c2 = do_convert((iconv_t)-1, MZ_UCS4_NAME, NULL, 1,
  2689. s2, d2 * 4, 4 * l2,
  2690. buf2, 0, MZ_SC_BUF_SIZE - 1,
  2691. 1 /* grow */, 0, 1 /* terminator size */,
  2692. &used2, &clen2,
  2693. &status);
  2694. if ((used1 < 4 * l1) || (used2 < 4 * l2)) {
  2695. if (got_more) {
  2696. /* Something went wrong. We've already tried to
  2697. even out the parts that work. Let's give up
  2698. on the first characters */
  2699. clen1 = clen2 = 0;
  2700. break;
  2701. } else if (used1 == used2) {
  2702. /* Not everything, but both ended at the same point */
  2703. break;
  2704. } else {
  2705. /* Pick the smallest */
  2706. if (used2 < used1) {
  2707. used1 = used2;
  2708. got_more = 1;
  2709. } else
  2710. got_more = 2;
  2711. l2 = (used1 >> 2);
  2712. l1 = (used1 >> 2);
  2713. if (!l1) {
  2714. /* Nothing to get this time. */
  2715. clen1 = clen2 = 0;
  2716. c1 = c2 = "";
  2717. used1 = used2 = 0;
  2718. break;
  2719. }
  2720. }
  2721. } else
  2722. /* Got all that we wanted */
  2723. break;
  2724. }
  2725. if (cvt_case) {
  2726. if (clen1)
  2727. c1 = locale_recase(0, c1, 0, clen1,
  2728. case_buf1, 0, MZ_SC_BUF_SIZE - 1,
  2729. &clen1);
  2730. else
  2731. c1 = NULL;
  2732. if (clen2)
  2733. c2 = locale_recase(0, c2, 0, clen2,
  2734. case_buf2, 0, MZ_SC_BUF_SIZE - 1,
  2735. &clen2);
  2736. else
  2737. c2 = NULL;
  2738. /* There shouldn't have been conversion errors, but just in
  2739. case, care of NULL. */
  2740. if (!c1) c1 = "";
  2741. if (!c2) c2 = "";
  2742. }
  2743. /* Collate, finally. */
  2744. status = strcoll(c1, c2);
  2745. /* If one is bigger than the other, we're done. */
  2746. if (status)
  2747. return status;
  2748. /* Otherwise, is there more to check? */
  2749. origl1 -= (used1 >> 2);
  2750. origl2 -= (used2 >> 2);
  2751. d1 += (used1 >> 2);
  2752. d2 += (used2 >> 2);
  2753. if (!origl1 && !origl2)
  2754. return 0;
  2755. /* There's more. It must be that the next character wasn't
  2756. convertable in one of the encodings. */
  2757. if (got_more)
  2758. return ((got_more == 2) ? 1 : -1);
  2759. if (!origl1)
  2760. return -1;
  2761. /* Compare an unconverable character directly. No case conversions
  2762. if it's outside the locale. */
  2763. if (((unsigned int *)s1)[d1] > ((unsigned int *)s2)[d2])
  2764. return 1;
  2765. else if (((unsigned int *)s1)[d1] < ((unsigned int *)s2)[d2])
  2766. return -1;
  2767. else {
  2768. /* We've skipped one unconvertable char, and they still look the
  2769. same. Now try again. */
  2770. origl1 -= 1;
  2771. origl2 -= 1;
  2772. d1 += 1;
  2773. d2 += 1;
  2774. }
  2775. }
  2776. }
  2777. #ifdef MACOS_UNICODE_SUPPORT
  2778. int mz_native_strcoll(char *s1, int d1, int l1, char *s2, int d2, int l2, int cvt_case)
  2779. /* The s1 and s2 arguments are actually UTF-16. */
  2780. {
  2781. CFStringRef str1, str2;
  2782. CFComparisonResult r;
  2783. str1 = CFStringCreateWithBytes(NULL, (unsigned char *)s1 XFORM_OK_PLUS (d1 * 2), (l1 * 2),
  2784. kCFStringEncodingUnicode, FALSE);
  2785. str2 = CFStringCreateWithBytes(NULL, (unsigned char *)s2 XFORM_OK_PLUS (d2 * 2), (l2 * 2),
  2786. kCFStringEncodingUnicode, FALSE);
  2787. r = CFStringCompare(str1, str2, (kCFCompareLocalized
  2788. | (cvt_case ? kCFCompareCaseInsensitive : 0)));
  2789. CFRelease(str1);
  2790. CFRelease(str2);
  2791. return (int)r;
  2792. }
  2793. #endif
  2794. #ifdef WINDOWS_UNICODE_SUPPORT
  2795. int mz_native_strcoll(char *s1, int d1, int l1, char *s2, int d2, int l2, int cvt_case)
  2796. /* The s1 and s2 arguments are actually UTF-16. */
  2797. {
  2798. int r;
  2799. r = CompareStringW(LOCALE_USER_DEFAULT,
  2800. ((cvt_case ? NORM_IGNORECASE : 0)
  2801. | NORM_IGNOREKANATYPE
  2802. | NORM_IGNOREWIDTH),
  2803. (wchar_t *)s1 + d1, l1, (wchar_t *)s2 + d2, l2);
  2804. return r - 2;
  2805. }
  2806. #endif
  2807. typedef int (*strcoll_proc)(char *s1, int d1, int l1, char *s2, int d2, int l2, int cvt_case);
  2808. int do_locale_comp(const char *who, const mzchar *us1, intptr_t ul1, const mzchar *us2, intptr_t ul2, int cvt_case)
  2809. {
  2810. int xl1;
  2811. int v, endres, utf16 = 0;
  2812. GC_CAN_IGNORE strcoll_proc mz_strcoll = mz_locale_strcoll;
  2813. #if defined(MACOS_UNICODE_SUPPORT) || defined(WINDOWS_UNICODE_SUPPORT)
  2814. if (current_locale_name && !*current_locale_name) {
  2815. utf16 = 1;
  2816. mz_strcoll = mz_native_strcoll;
  2817. }
  2818. #endif
  2819. if (utf16) {
  2820. us1 = (mzchar *)scheme_ucs4_to_utf16(us1, 0, ul1, NULL, 0, &ul1, 1);
  2821. us2 = (mzchar *)scheme_ucs4_to_utf16(us2, 0, ul2, NULL, 0, &ul2, 1);
  2822. ((short *)us1)[ul1] = 0;
  2823. ((short *)us2)[ul2] = 0;
  2824. }
  2825. if (ul1 > ul2) {
  2826. ul1 = ul2;
  2827. endres = 1;
  2828. } else {
  2829. if (ul2 > ul1)
  2830. endres = -1;
  2831. else
  2832. endres = 0;
  2833. }
  2834. /* Walk back through the strings looking for nul characters. If we
  2835. find one, compare the part after the null character to update
  2836. endres, then continue. Unfortunately, we do too much work if an
  2837. earlier part of the string (tested later) determines the result,
  2838. but hopefully nul characters are rare. */
  2839. xl1 = 0;
  2840. while (ul1--) {
  2841. if ((utf16 && (!(((short *)us1)[ul1]) || !(((short *)us2)[ul1])))
  2842. || (!utf16 && (!(us1[ul1]) || !(us2[ul1])))) {
  2843. if (utf16) {
  2844. if (((short *)us1)[ul1])
  2845. endres = 1;
  2846. else if (((short *)us2)[ul1])
  2847. endres = -1;
  2848. } else {
  2849. if (us1[ul1])
  2850. endres = 1;
  2851. else if (us2[ul1])
  2852. endres = -1;
  2853. }
  2854. if (xl1)
  2855. v = mz_strcoll((char *)us1, ul1 + 1, xl1, (char *)us2, ul1 + 1, xl1, cvt_case);
  2856. else
  2857. v = 0;
  2858. if (v)
  2859. endres = v;
  2860. xl1 = 0;
  2861. } else {
  2862. xl1++;
  2863. }
  2864. }
  2865. v = mz_strcoll((char *)us1, 0, xl1, (char *)us2, 0, xl1, cvt_case);
  2866. if (v)
  2867. endres = v;
  2868. return endres;
  2869. }
  2870. mzchar *do_locale_recase(int to_up, mzchar *in, int delta, int len, intptr_t *olen)
  2871. {
  2872. Scheme_Object *parts = scheme_null;
  2873. char *c, buf[MZ_SC_BUF_SIZE], case_buf[MZ_SC_BUF_SIZE];
  2874. intptr_t clen, used;
  2875. int status;
  2876. while (len) {
  2877. /* We might have conversion errors... */
  2878. c = do_convert((iconv_t)-1, MZ_UCS4_NAME, NULL, 1,
  2879. (char *)in, 4 * delta, 4 * len,
  2880. buf, 0, MZ_SC_BUF_SIZE - 1,
  2881. 1 /* grow */, 0, 1 /* terminator size */,
  2882. &used, &clen,
  2883. &status);
  2884. used >>= 2;
  2885. delta += used;
  2886. len -= used;
  2887. c = locale_recase(to_up, c, 0, clen,
  2888. case_buf, 0, MZ_SC_BUF_SIZE - 1,
  2889. &clen);
  2890. if (!c)
  2891. clen = 0;
  2892. c = do_convert((iconv_t)-1, NULL, MZ_UCS4_NAME, 2,
  2893. c, 0, clen,
  2894. NULL, 0, 0,
  2895. 1 /* grow */, 0, sizeof(mzchar) /* terminator size */,
  2896. &used, &clen,
  2897. &status);
  2898. if (!len && SCHEME_NULLP(parts)) {
  2899. *olen = (clen >> 2);
  2900. ((mzchar *)c)[*olen] = 0;
  2901. return (mzchar *)c;
  2902. }
  2903. /* We can get here if there was some conversion error at some
  2904. point. We're building up a list of parts. */
  2905. parts = scheme_make_pair(scheme_make_sized_char_string((mzchar *)c, clen >> 2, 0),
  2906. parts);
  2907. if (len) {
  2908. /* Conversion error, so skip one char. */
  2909. parts = scheme_make_pair(scheme_make_sized_offset_char_string(in, delta, 1, 1),
  2910. parts);
  2911. delta += 1;
  2912. len -= 1;
  2913. }
  2914. }
  2915. parts = append_all_strings_backwards(parts);
  2916. *olen = SCHEME_CHAR_STRTAG_VAL(parts);
  2917. return SCHEME_CHAR_STR_VAL(parts);
  2918. }
  2919. #ifdef MACOS_UNICODE_SUPPORT
  2920. mzchar *do_native_recase(int to_up, mzchar *in, int delta, int len, intptr_t *olen)
  2921. /* The in argument is actually UTF-16. */
  2922. {
  2923. CFMutableStringRef mstr;
  2924. CFStringRef str;
  2925. GC_CAN_IGNORE CFRange rng;
  2926. char *result;
  2927. str = CFStringCreateWithBytes(NULL, ((unsigned char *)in) XFORM_OK_PLUS (delta * 2), (len * 2),
  2928. kCFStringEncodingUnicode, FALSE);
  2929. mstr = CFStringCreateMutableCopy(NULL, 0, str);
  2930. CFRelease(str);
  2931. if (to_up)
  2932. CFStringUppercase(mstr, NULL);
  2933. else
  2934. CFStringLowercase(mstr, NULL);
  2935. len = CFStringGetLength(mstr);
  2936. *olen = len;
  2937. result = (char *)scheme_malloc_atomic((len + 1) * 2);
  2938. rng = CFRangeMake(0, len);
  2939. CFStringGetCharacters(mstr, rng, (UniChar *)result);
  2940. CFRelease(mstr);
  2941. ((UniChar *)result)[len] = 0;
  2942. return (mzchar *)result;
  2943. }
  2944. #endif
  2945. #ifdef WINDOWS_UNICODE_SUPPORT
  2946. mzchar *do_native_recase(int to_up, mzchar *in, int delta, int len, intptr_t *olen)
  2947. /* The in argument is actually UTF-16. */
  2948. {
  2949. char *result;
  2950. result = (char *)scheme_malloc_atomic((len + 1) * 2);
  2951. memcpy(result, ((char *)in) + (2 * delta), len * 2);
  2952. ((wchar_t*)result)[len] = 0;
  2953. if (to_up)
  2954. CharUpperBuffW((wchar_t *)result, len);
  2955. else
  2956. CharLowerBuffW((wchar_t *)result, len);
  2957. *olen = len;
  2958. return (mzchar *)result;
  2959. }
  2960. #endif
  2961. typedef mzchar *(*recase_proc)(int to_up, mzchar *in, int delta, int len, intptr_t *olen);
  2962. static Scheme_Object *mz_recase(const char *who, int to_up, mzchar *us, intptr_t ulen)
  2963. {
  2964. intptr_t ulen1;
  2965. int utf16 = 0, i, delta = 0;
  2966. mzchar *us1;
  2967. recase_proc mz_do_recase = do_locale_recase;
  2968. Scheme_Object *s, *parts = scheme_null;
  2969. reset_locale();
  2970. #if defined(MACOS_UNICODE_SUPPORT) || defined(WINDOWS_UNICODE_SUPPORT)
  2971. if (current_locale_name && !*current_locale_name) {
  2972. utf16 = 1;
  2973. mz_do_recase = do_native_recase;
  2974. }
  2975. #endif
  2976. if (utf16) {
  2977. us = (mzchar *)scheme_ucs4_to_utf16(us, 0, ulen, NULL, 0, &ulen, 1);
  2978. ((short *)us)[ulen] = 0;
  2979. }
  2980. /* If there are nulls in the string, then we have to make multiple
  2981. calls to mz_do_recase */
  2982. i = 0;
  2983. while (1) {
  2984. for (; i < ulen; i++) {
  2985. if (utf16) {
  2986. if (!((short *)us)[i])
  2987. break;
  2988. } else if (!us[i])
  2989. break;
  2990. }
  2991. us1 = mz_do_recase(to_up, us, delta, i - delta, &ulen1);
  2992. if (utf16) {
  2993. us1 = scheme_utf16_to_ucs4((unsigned short *)us1, 0, ulen1, NULL, 0, &ulen1, 1);
  2994. us1[ulen1] = 0;
  2995. }
  2996. s = scheme_make_sized_char_string((mzchar *)us1, ulen1, 0);
  2997. if (SCHEME_NULLP(parts) && (i == ulen))
  2998. return s;
  2999. parts = scheme_make_pair(s, parts);
  3000. if (i == ulen)
  3001. break;
  3002. /* upcasing and encoding a nul char is easy: */
  3003. s = scheme_make_sized_char_string((mzchar *)"\0\0\0\0", 1, 0);
  3004. parts = scheme_make_pair(s, parts);
  3005. i++;
  3006. delta = i;
  3007. if (i == ulen)
  3008. break;
  3009. }
  3010. return append_all_strings_backwards(parts);
  3011. }
  3012. #endif
  3013. static Scheme_Object *
  3014. unicode_recase(const char *who, int to_up, int argc, Scheme_Object *argv[])
  3015. {
  3016. intptr_t len;
  3017. mzchar *chars;
  3018. if (!SCHEME_CHAR_STRINGP(argv[0]))
  3019. scheme_wrong_type(who, "string", 0, argc, argv);
  3020. chars = SCHEME_CHAR_STR_VAL(argv[0]);
  3021. len = SCHEME_CHAR_STRTAG_VAL(argv[0]);
  3022. return mz_recase(who, to_up, chars, len);
  3023. }
  3024. static Scheme_Object *
  3025. string_locale_upcase(int argc, Scheme_Object *argv[])
  3026. {
  3027. return unicode_recase("string-locale-upcase", 1, argc, argv);
  3028. }
  3029. static Scheme_Object *
  3030. string_locale_downcase(int argc, Scheme_Object *argv[])
  3031. {
  3032. return unicode_recase("string-locale-downcase", 0, argc, argv);
  3033. }
  3034. static void reset_locale(void)
  3035. {
  3036. Scheme_Object *v;
  3037. const mzchar *name;
  3038. /* This function needs to work before threads are set up: */
  3039. if (scheme_current_thread) {
  3040. v = scheme_get_param(scheme_current_config(), MZCONFIG_LOCALE);
  3041. } else {
  3042. v = scheme_make_immutable_sized_utf8_string("", 0);
  3043. }
  3044. locale_on = SCHEME_TRUEP(v);
  3045. if (locale_on) {
  3046. name = SCHEME_CHAR_STR_VAL(v);
  3047. #ifndef DONT_USE_LOCALE
  3048. if ((current_locale_name != name)
  3049. && (!current_locale_name
  3050. || mz_char_strcmp("result-locale",
  3051. current_locale_name, scheme_char_strlen(current_locale_name),
  3052. name, SCHEME_CHAR_STRLEN_VAL(v),
  3053. 0, 1))) {
  3054. /* We only need CTYPE and COLLATE; two calls seem to be much
  3055. faster than one call with ALL */
  3056. char *n, buf[32];
  3057. n = scheme_utf8_encode_to_buffer(name, SCHEME_CHAR_STRLEN_VAL(v), buf, 32);
  3058. if (!setlocale(LC_CTYPE, n))
  3059. setlocale(LC_CTYPE, "C");
  3060. if (!setlocale(LC_COLLATE, n))
  3061. setlocale(LC_COLLATE, "C");
  3062. }
  3063. #endif
  3064. current_locale_name_ptr = (void *)name;
  3065. }
  3066. }
  3067. static int find_special_casing(int ch)
  3068. {
  3069. /* Binary search */
  3070. int i, lo, hi, j;
  3071. i = NUM_SPECIAL_CASINGS >> 1;
  3072. lo = i;
  3073. hi = NUM_SPECIAL_CASINGS - i - 1;
  3074. while (1) {
  3075. if (uchar_special_casings[i * 10] == ch)
  3076. return i * 10;
  3077. if (uchar_special_casings[i * 10] > ch) {
  3078. j = i - lo;
  3079. i = j + (lo >> 1);
  3080. hi = lo - (i - j) - 1;
  3081. lo = i - j;
  3082. } else {
  3083. j = i + 1;
  3084. i = j + (hi >> 1);
  3085. lo = i - j;
  3086. hi = hi - (i - j) - 1;
  3087. }
  3088. }
  3089. }
  3090. static int is_final_sigma(int mode, mzchar *s, int d, int i, int len)
  3091. {
  3092. int j;
  3093. if (mode == 3)
  3094. return 1;
  3095. /* find a cased char before, skipping case-ignorable: */
  3096. for (j = i - 1; j >= d; j--) {
  3097. if (!scheme_iscaseignorable(s[j])) {
  3098. if (scheme_iscased(s[j]))
  3099. break;
  3100. else
  3101. return 0;
  3102. }
  3103. }
  3104. if (j < d)
  3105. return 0;
  3106. /* next non-case-ignorable must not be cased: */
  3107. for (j = i + 1; j < d + len; j++) {
  3108. if (!scheme_iscaseignorable(s[j])) {
  3109. return !scheme_iscased(s[j]);
  3110. }
  3111. }
  3112. return 1;
  3113. }
  3114. mzchar *scheme_string_recase(mzchar *s, int d, int len, int mode, int inplace, int *_len)
  3115. {
  3116. mzchar *t;
  3117. int i, extra = 0, pos, special = 0, td, prev_was_cased = 0, xmode = mode;
  3118. for (i = 0; i < len; i++) {
  3119. if (scheme_isspecialcasing(s[d+i])) {
  3120. pos = find_special_casing(s[d+i]);
  3121. if (!uchar_special_casings[pos + 9] || is_final_sigma(xmode, s, d, i, len)) {
  3122. special = 1;
  3123. extra += (uchar_special_casings[pos + 1 + (xmode << 1)] - 1);
  3124. }
  3125. }
  3126. if (mode == 2) {
  3127. if (!scheme_iscaseignorable(s[d+i]))
  3128. prev_was_cased = scheme_iscased(s[d+i]);
  3129. xmode = (prev_was_cased ? 0 : 2);
  3130. }
  3131. }
  3132. if (_len)
  3133. *_len = len + extra;
  3134. if (!extra && inplace) {
  3135. t = s;
  3136. td = d;
  3137. } else {
  3138. t = scheme_malloc_atomic(sizeof(mzchar) * (len + extra + 1));
  3139. td = 0;
  3140. }
  3141. if (!special) {
  3142. if (mode == 0) {
  3143. for (i = 0; i < len; i++) {
  3144. t[i+td] = scheme_tolower(s[i+d]);
  3145. }
  3146. } else if (mode == 1) {
  3147. for (i = 0; i < len; i++) {
  3148. t[i+td] = scheme_toupper(s[i+d]);
  3149. }
  3150. } else if (mode == 2) {
  3151. prev_was_cased = 0;
  3152. for (i = 0; i < len; i++) {
  3153. if (!prev_was_cased)
  3154. t[i+td] = scheme_totitle(s[i+d]);
  3155. else
  3156. t[i+td] = scheme_tolower(s[i+d]);
  3157. if (!scheme_iscaseignorable(s[i+d]))
  3158. prev_was_cased = scheme_iscased(s[i+d]);
  3159. }
  3160. } else /* if (mode == 3) */ {
  3161. for (i = 0; i < len; i++) {
  3162. t[i+td] = scheme_tofold(s[i+d]);
  3163. }
  3164. }
  3165. } else {
  3166. int j = 0, c;
  3167. prev_was_cased = 0;
  3168. for (i = 0; i < len; i++) {
  3169. if (mode == 0) {
  3170. t[j+td] = scheme_tolower(s[i+d]);
  3171. } else if (mode == 1) {
  3172. t[j+td] = scheme_toupper(s[i+d]);
  3173. } else if (mode == 2) {
  3174. if (!prev_was_cased) {
  3175. xmode = 2;
  3176. t[j+td] = scheme_totitle(s[i+d]);
  3177. } else {
  3178. xmode = 0;
  3179. t[j+td] = scheme_tolower(s[i+d]);
  3180. }
  3181. if (!scheme_iscaseignorable(s[i+d]))
  3182. prev_was_cased = scheme_iscased(s[i+d]);
  3183. } else /* if (mode == 3) */ {
  3184. t[j+td] = scheme_tofold(s[i+d]);
  3185. }
  3186. if (scheme_isspecialcasing(s[i+d])) {
  3187. pos = find_special_casing(s[i+d]);
  3188. if (!uchar_special_casings[pos + 9] || is_final_sigma(xmode, s, d, i, len)) {
  3189. c = uchar_special_casings[pos + 1 + (xmode << 1)];
  3190. pos = uchar_special_casings[pos + 2 + (xmode << 1)];
  3191. while (c--) {
  3192. t[(j++)+td] = uchar_special_casing_data[pos++];
  3193. }
  3194. } else
  3195. j++;
  3196. } else
  3197. j++;
  3198. }
  3199. }
  3200. t[len+extra+td] = 0;
  3201. return t;
  3202. }
  3203. static Scheme_Object *string_recase (const char *name, int argc, Scheme_Object *argv[], int mode)
  3204. {
  3205. mzchar *s;
  3206. int len;
  3207. if (!SCHEME_CHAR_STRINGP(argv[0]))
  3208. scheme_wrong_type(name, "string", 0, argc, argv);
  3209. s = SCHEME_CHAR_STR_VAL(argv[0]);
  3210. len = SCHEME_CHAR_STRLEN_VAL(argv[0]);
  3211. s = scheme_string_recase(s, 0, len, mode, 0, &len);
  3212. return scheme_make_sized_char_string(s, len, 0);
  3213. }
  3214. static Scheme_Object *string_upcase (int argc, Scheme_Object *argv[])
  3215. {
  3216. return string_recase("string-upcase", argc, argv, 1);
  3217. }
  3218. static Scheme_Object *string_downcase (int argc, Scheme_Object *argv[])
  3219. {
  3220. return string_recase("string-downcase", argc, argv, 0);
  3221. }
  3222. static Scheme_Object *string_titlecase (int argc, Scheme_Object *argv[])
  3223. {
  3224. return string_recase("string-titlecase", argc, argv, 2);
  3225. }
  3226. static Scheme_Object *string_foldcase (int argc, Scheme_Object *argv[])
  3227. {
  3228. return string_recase("string-foldcase", argc, argv, 3);
  3229. }
  3230. /**********************************************************************/
  3231. /* normalization */
  3232. /**********************************************************************/
  3233. #define MZ_JAMO_INITIAL_CONSONANT_START 0x1100
  3234. #define MZ_JAMO_INITIAL_CONSONANT_COUNT 19
  3235. #define MZ_JAMO_INITIAL_CONSONANT_END (MZ_JAMO_INITIAL_CONSONANT_START + MZ_JAMO_INITIAL_CONSONANT_COUNT - 1)
  3236. #define MZ_JAMO_VOWEL_START 0x1161
  3237. #define MZ_JAMO_VOWEL_COUNT 21
  3238. #define MZ_JAMO_VOWEL_END (MZ_JAMO_VOWEL_START + MZ_JAMO_VOWEL_COUNT - 1)
  3239. /* First in this range is not actually a consonant, but a placeholder for "no consonant" */
  3240. #define MZ_JAMO_TRAILING_CONSONANT_START 0x11A7
  3241. #define MZ_JAMO_TRAILING_CONSONANT_COUNT 28
  3242. #define MZ_JAMO_TRAILING_CONSONANT_END (MZ_JAMO_TRAILING_CONSONANT_START + MZ_JAMO_TRAILING_CONSONANT_COUNT - 1)
  3243. #define MZ_JAMO_SYLLABLE_START 0xAC00
  3244. #define MZ_JAMO_SYLLABLE_END (MZ_JAMO_SYLLABLE_START + 11171)
  3245. static mzchar get_composition(mzchar a, mzchar b)
  3246. {
  3247. uintptr_t key = (a << 16) | b;
  3248. int pos = (COMPOSE_TABLE_SIZE >> 1), new_pos;
  3249. int below_len = pos;
  3250. int above_len = (COMPOSE_TABLE_SIZE - pos - 1);
  3251. if (a > 0xFFFF) return 0;
  3252. /* Binary search: */
  3253. while (key != utable_compose_pairs[pos]) {
  3254. if (key > utable_compose_pairs[pos]) {
  3255. if (!above_len)
  3256. return 0;
  3257. new_pos = pos + (above_len >> 1) + 1;
  3258. below_len = (new_pos - pos - 1);
  3259. above_len = (above_len - below_len - 1);
  3260. pos = new_pos;
  3261. } else if (key < utable_compose_pairs[pos]) {
  3262. if (!below_len)
  3263. return 0;
  3264. new_pos = pos - ((below_len >> 1) + 1);
  3265. above_len = (pos - new_pos - 1);
  3266. below_len = (below_len - above_len - 1);
  3267. pos = new_pos;
  3268. }
  3269. }
  3270. return utable_compose_result[pos];
  3271. }
  3272. mzchar get_canon_decomposition(mzchar key, mzchar *b)
  3273. {
  3274. int pos = (DECOMPOSE_TABLE_SIZE >> 1), new_pos;
  3275. int below_len = pos;
  3276. int above_len = (DECOMPOSE_TABLE_SIZE - pos - 1);
  3277. /* Binary search: */
  3278. while (key != utable_decomp_keys[pos]) {
  3279. if (key > utable_decomp_keys[pos]) {
  3280. if (!above_len)
  3281. return 0;
  3282. new_pos = pos + (above_len >> 1) + 1;
  3283. below_len = (new_pos - pos - 1);
  3284. above_len = (above_len - below_len - 1);
  3285. pos = new_pos;
  3286. } else if (key < utable_decomp_keys[pos]) {
  3287. if (!below_len)
  3288. return 0;
  3289. new_pos = pos - ((below_len >> 1) + 1);
  3290. above_len = (pos - new_pos - 1);
  3291. below_len = (below_len - above_len - 1);
  3292. pos = new_pos;
  3293. }
  3294. }
  3295. pos = utable_decomp_indices[pos];
  3296. if (pos < 0) {
  3297. pos = -(pos + 1);
  3298. pos <<= 1;
  3299. *b = utable_compose_long_pairs[pos + 1];
  3300. return utable_compose_long_pairs[pos];
  3301. } else {
  3302. key = utable_compose_pairs[pos];
  3303. *b = (key & 0xFFFF);
  3304. return (key >> 16);
  3305. }
  3306. }
  3307. int get_kompat_decomposition(mzchar key, unsigned short **chars)
  3308. {
  3309. int pos = (KOMPAT_DECOMPOSE_TABLE_SIZE >> 1), new_pos;
  3310. int below_len = pos;
  3311. int above_len = (KOMPAT_DECOMPOSE_TABLE_SIZE - pos - 1);
  3312. /* Binary search: */
  3313. while (key != utable_kompat_decomp_keys[pos]) {
  3314. if (key > utable_kompat_decomp_keys[pos]) {
  3315. if (!above_len)
  3316. return 0;
  3317. new_pos = pos + (above_len >> 1) + 1;
  3318. below_len = (new_pos - pos - 1);
  3319. above_len = (above_len - below_len - 1);
  3320. pos = new_pos;
  3321. } else if (key < utable_kompat_decomp_keys[pos]) {
  3322. if (!below_len)
  3323. return 0;
  3324. new_pos = pos - ((below_len >> 1) + 1);
  3325. above_len = (pos - new_pos - 1);
  3326. below_len = (below_len - above_len - 1);
  3327. pos = new_pos;
  3328. }
  3329. }
  3330. *chars = utable_kompat_decomp_strs XFORM_OK_PLUS utable_kompat_decomp_indices[pos];
  3331. return utable_kompat_decomp_lens[pos];
  3332. }
  3333. static Scheme_Object *normalize_c(Scheme_Object *o)
  3334. /* Assumes then given string is in normal form D */
  3335. {
  3336. mzchar *s, *s2, tmp, last_c0 = 0;
  3337. int len, i, j = 0, last_c0_pos = 0, last_cc = 0;
  3338. s = SCHEME_CHAR_STR_VAL(o);
  3339. len = SCHEME_CHAR_STRLEN_VAL(o);
  3340. s2 = (mzchar *)scheme_malloc_atomic((len + 1) * sizeof(mzchar));
  3341. memcpy(s2, s, len * sizeof(mzchar));
  3342. for (i = 0; i < len; i++) {
  3343. if ((i + 1 < len)
  3344. && (s2[i] >= MZ_JAMO_INITIAL_CONSONANT_START)
  3345. && (s2[i] <= MZ_JAMO_INITIAL_CONSONANT_END)
  3346. && (s2[i+1] >= MZ_JAMO_VOWEL_START)
  3347. && (s2[i+1] <= MZ_JAMO_VOWEL_END)) {
  3348. /* Need Hangul composition */
  3349. if ((i + 2 < len)
  3350. && (s2[i+2] > MZ_JAMO_TRAILING_CONSONANT_START)
  3351. && (s2[i+2] <= MZ_JAMO_TRAILING_CONSONANT_END)) {
  3352. /* 3-char composition */
  3353. tmp = (MZ_JAMO_SYLLABLE_START
  3354. + ((s2[i] - MZ_JAMO_INITIAL_CONSONANT_START)
  3355. * MZ_JAMO_VOWEL_COUNT * MZ_JAMO_TRAILING_CONSONANT_COUNT)
  3356. + ((s2[i+1] - MZ_JAMO_VOWEL_START)
  3357. * MZ_JAMO_TRAILING_CONSONANT_COUNT)
  3358. + (s2[i+2] - MZ_JAMO_TRAILING_CONSONANT_START));
  3359. i += 2;
  3360. } else {
  3361. /* 2-char composition */
  3362. tmp = (MZ_JAMO_SYLLABLE_START
  3363. + ((s2[i] - MZ_JAMO_INITIAL_CONSONANT_START)
  3364. * MZ_JAMO_VOWEL_COUNT * MZ_JAMO_TRAILING_CONSONANT_COUNT)
  3365. + ((s2[i+1] - MZ_JAMO_VOWEL_START)
  3366. * MZ_JAMO_TRAILING_CONSONANT_COUNT));
  3367. i++;
  3368. }
  3369. last_c0 = tmp;
  3370. last_c0_pos = j;
  3371. last_cc = 0;
  3372. s2[j++] = tmp;
  3373. } else {
  3374. int cc;
  3375. cc = scheme_combining_class(s2[i]);
  3376. if (last_c0 && (cc > last_cc))
  3377. tmp = get_composition(last_c0, s2[i]);
  3378. else
  3379. tmp = 0;
  3380. if (tmp) {
  3381. /* Need to compose */
  3382. s2[last_c0_pos] = tmp;
  3383. last_c0 = tmp;
  3384. } else if (!cc) {
  3385. /* Reset last_c0... */
  3386. tmp = s2[i];
  3387. if (scheme_needs_maybe_compose(tmp)) {
  3388. last_c0 = tmp;
  3389. last_c0_pos = j;
  3390. } else {
  3391. last_c0 = 0;
  3392. }
  3393. last_cc = -1;
  3394. s2[j++] = tmp;
  3395. } else {
  3396. s2[j++] = s2[i];
  3397. last_cc = cc;
  3398. }
  3399. }
  3400. }
  3401. s2[j] = 0;
  3402. if (len - j > 16) {
  3403. s2 = (mzchar *)scheme_malloc_atomic((j + 1) * sizeof(mzchar));
  3404. memcpy(s2, s, (j + 1) * sizeof(mzchar));
  3405. s2 = s;
  3406. }
  3407. return scheme_make_sized_char_string(s2, j, 0);
  3408. }
  3409. static Scheme_Object *normalize_d(Scheme_Object *o, int kompat)
  3410. {
  3411. mzchar *s, tmp, *s2;
  3412. int len, i, delta, j, swapped;
  3413. s = SCHEME_CHAR_STR_VAL(o);
  3414. len = SCHEME_CHAR_STRLEN_VAL(o);
  3415. /* Run through string list to predict expansion: */
  3416. delta = 0;
  3417. for (i = 0; i < len; i++) {
  3418. if (scheme_needs_decompose(s[i])) {
  3419. int klen;
  3420. mzchar snd;
  3421. GC_CAN_IGNORE unsigned short *start;
  3422. tmp = s[i];
  3423. while (scheme_needs_decompose(tmp)) {
  3424. if (kompat)
  3425. klen = get_kompat_decomposition(tmp, &start);
  3426. else
  3427. klen = 0;
  3428. if (klen) {
  3429. delta += (klen - 1);
  3430. break;
  3431. } else {
  3432. tmp = get_canon_decomposition(tmp, &snd);
  3433. if (tmp) {
  3434. if (snd) {
  3435. delta++;
  3436. if (kompat) {
  3437. klen = get_kompat_decomposition(snd, &start);
  3438. if (klen)
  3439. delta += (klen - 1);
  3440. }
  3441. }
  3442. } else
  3443. break;
  3444. }
  3445. }
  3446. } else if ((s[i] >= MZ_JAMO_SYLLABLE_START)
  3447. && (s[i] <= MZ_JAMO_SYLLABLE_END)) {
  3448. tmp = s[i];
  3449. tmp -= MZ_JAMO_SYLLABLE_START;
  3450. if (tmp % MZ_JAMO_TRAILING_CONSONANT_COUNT)
  3451. delta += 2;
  3452. else
  3453. delta += 1;
  3454. }
  3455. }
  3456. s2 = (mzchar *)scheme_malloc_atomic((len + delta + 1) * sizeof(mzchar));
  3457. j = 0;
  3458. for (i = 0; i < len; i++) {
  3459. if (scheme_needs_decompose(s[i])) {
  3460. mzchar snd, tmp2;
  3461. int snds = 0, klen = 0, k;
  3462. GC_CAN_IGNORE unsigned short*start;
  3463. tmp = s[i];
  3464. while (scheme_needs_decompose(tmp)) {
  3465. if (kompat)
  3466. klen = get_kompat_decomposition(tmp, &start);
  3467. else
  3468. klen = 0;
  3469. if (klen) {
  3470. for (k = 0; k < klen; k++) {
  3471. s2[j++] = start[k];
  3472. }
  3473. break;
  3474. } else {
  3475. tmp2 = get_canon_decomposition(tmp, &snd);
  3476. if (tmp2) {
  3477. tmp = tmp2;
  3478. if (snd) {
  3479. if (kompat)
  3480. klen = get_kompat_decomposition(snd, &start);
  3481. else
  3482. klen = 0;
  3483. if (klen) {
  3484. snds += klen;
  3485. for (k = 0; k < klen; k++) {
  3486. s2[len + delta - snds + k] = start[k];
  3487. }
  3488. klen = 0;
  3489. } else {
  3490. snds++;
  3491. s2[len + delta - snds] = snd;
  3492. }
  3493. }
  3494. } else
  3495. break;
  3496. }
  3497. }
  3498. if (!klen)
  3499. s2[j++] = tmp;
  3500. memcpy(s2 + j, s2 + len + delta - snds, snds * sizeof(mzchar));
  3501. j += snds;
  3502. } else if ((s[i] >= MZ_JAMO_SYLLABLE_START)
  3503. && (s[i] <= MZ_JAMO_SYLLABLE_END)) {
  3504. int l, v, t;
  3505. tmp = s[i];
  3506. tmp -= MZ_JAMO_SYLLABLE_START;
  3507. l = tmp / (MZ_JAMO_VOWEL_COUNT * MZ_JAMO_TRAILING_CONSONANT_COUNT);
  3508. v = (tmp % (MZ_JAMO_VOWEL_COUNT * MZ_JAMO_TRAILING_CONSONANT_COUNT)) / MZ_JAMO_TRAILING_CONSONANT_COUNT;
  3509. t = tmp % MZ_JAMO_TRAILING_CONSONANT_COUNT;
  3510. s2[j++] = MZ_JAMO_INITIAL_CONSONANT_START + l;
  3511. s2[j++] = MZ_JAMO_VOWEL_START + v;
  3512. if (t) {
  3513. s2[j++] = MZ_JAMO_TRAILING_CONSONANT_START + t;
  3514. }
  3515. } else {
  3516. s2[j++] = s[i];
  3517. }
  3518. }
  3519. s2[j] = 0;
  3520. len += delta;
  3521. /* Reorder pass: */
  3522. do {
  3523. swapped = 0;
  3524. for (i = 0; i < len; i++) {
  3525. if ((i + 1 < len)
  3526. && scheme_combining_class(s2[i])
  3527. && scheme_combining_class(s2[i+1])
  3528. && (scheme_combining_class(s2[i+1]) < scheme_combining_class(s2[i]))) {
  3529. /* Reorder and try again: */
  3530. tmp = s2[i + 1];
  3531. s2[i + 1] = s2[i];
  3532. s2[i] = tmp;
  3533. i--;
  3534. swapped = 1;
  3535. }
  3536. }
  3537. } while (swapped);
  3538. return scheme_make_sized_char_string(s2, len, 0);
  3539. }
  3540. static Scheme_Object *do_string_normalize_c (const char *who, int argc, Scheme_Object *argv[], int kompat)
  3541. {
  3542. Scheme_Object *o;
  3543. mzchar *s, last_c0 = 0, snd;
  3544. int len, i, last_cc = 0;
  3545. o = argv[0];
  3546. if (!SCHEME_CHAR_STRINGP(o))
  3547. scheme_wrong_type(who, "string", 0, argc, argv);
  3548. s = SCHEME_CHAR_STR_VAL(o);
  3549. len = SCHEME_CHAR_STRLEN_VAL(o);
  3550. for (i = 0; i < len; i++) {
  3551. if (scheme_needs_decompose(s[i])
  3552. && (kompat || get_canon_decomposition(s[i], &snd))) {
  3553. /* Decomposition may expose a different composition */
  3554. break;
  3555. } else if ((i + 1 < len)
  3556. && scheme_combining_class(s[i])
  3557. && scheme_combining_class(s[i+1])
  3558. && (scheme_combining_class(s[i+1]) < scheme_combining_class(s[i]))) {
  3559. /* Need to reorder */
  3560. break;
  3561. } else if ((s[i] >= MZ_JAMO_INITIAL_CONSONANT_START)
  3562. && (s[i] <= MZ_JAMO_INITIAL_CONSONANT_END)
  3563. && (s[i+1] >= MZ_JAMO_VOWEL_START)
  3564. && (s[i+1] <= MZ_JAMO_VOWEL_END)) {
  3565. /* Need Hangul composition */
  3566. break;
  3567. } else if (last_c0
  3568. && get_composition(last_c0, s[i])
  3569. && (scheme_combining_class(s[i]) > last_cc)) {
  3570. /* Need to compose */
  3571. break;
  3572. } else {
  3573. int cc;
  3574. cc = scheme_combining_class(s[i]);
  3575. if (!cc) {
  3576. if (scheme_needs_maybe_compose(s[i]))
  3577. last_c0 = s[i];
  3578. else
  3579. last_c0 = 0;
  3580. last_cc = -1;
  3581. } else
  3582. last_cc = cc;
  3583. }
  3584. }
  3585. if (i < len) {
  3586. o = normalize_c(normalize_d(o, kompat));
  3587. }
  3588. return o;
  3589. }
  3590. static Scheme_Object *string_normalize_c (int argc, Scheme_Object *argv[])
  3591. {
  3592. return do_string_normalize_c("string-normalize-nfc", argc, argv, 0);
  3593. }
  3594. static Scheme_Object *string_normalize_kc (int argc, Scheme_Object *argv[])
  3595. {
  3596. return do_string_normalize_c("string-normalize-nfkc", argc, argv, 1);
  3597. }
  3598. static Scheme_Object *do_string_normalize_d (const char *who, int argc, Scheme_Object *argv[], int kompat)
  3599. {
  3600. Scheme_Object *o;
  3601. mzchar *s;
  3602. int len, i;
  3603. o = argv[0];
  3604. if (!SCHEME_CHAR_STRINGP(o))
  3605. scheme_wrong_type(who, "string", 0, argc, argv);
  3606. s = SCHEME_CHAR_STR_VAL(o);
  3607. len = SCHEME_CHAR_STRLEN_VAL(o);
  3608. for (i = len; i--; ) {
  3609. if (scheme_needs_decompose(s[i])) {
  3610. /* Need to decompose */
  3611. mzchar snd;
  3612. if (kompat || get_canon_decomposition(s[i], &snd))
  3613. break;
  3614. } else if ((i + 1 < len)
  3615. && scheme_combining_class(s[i])
  3616. && scheme_combining_class(s[i+1])
  3617. && (scheme_combining_class(s[i+1]) < scheme_combining_class(s[i]))) {
  3618. /* Need to reorder */
  3619. break;
  3620. } else if ((s[i] >= MZ_JAMO_SYLLABLE_START)
  3621. && (s[i] <= MZ_JAMO_SYLLABLE_END)) {
  3622. /* Need Hangul decomposition */
  3623. break;
  3624. }
  3625. }
  3626. if (i >= 0) {
  3627. o = normalize_d(o, kompat);
  3628. }
  3629. return o;
  3630. }
  3631. static Scheme_Object *string_normalize_d (int argc, Scheme_Object *argv[])
  3632. {
  3633. return do_string_normalize_d("string-normalize-nfd", argc, argv, 0);
  3634. }
  3635. static Scheme_Object *string_normalize_kd (int argc, Scheme_Object *argv[])
  3636. {
  3637. return do_string_normalize_d("string-normalize-nfkd", argc, argv, 1);
  3638. }
  3639. /**********************************************************************/
  3640. /* strcmps */
  3641. /**********************************************************************/
  3642. intptr_t scheme_char_strlen(const mzchar *s)
  3643. {
  3644. intptr_t i;
  3645. for (i = 0; s[i]; i++) {
  3646. }
  3647. return i;
  3648. }
  3649. static int mz_char_strcmp(const char *who, const mzchar *str1, intptr_t l1, const mzchar *str2, intptr_t l2,
  3650. int use_locale, int size_shortcut)
  3651. {
  3652. intptr_t endres;
  3653. if (size_shortcut && (l1 != l2))
  3654. return 1;
  3655. #ifndef DONT_USE_LOCALE
  3656. if (use_locale) {
  3657. reset_locale();
  3658. if (locale_on) {
  3659. return do_locale_comp(who, str1, l1, str2, l2, 0);
  3660. }
  3661. }
  3662. #endif
  3663. if (l1 > l2) {
  3664. l1 = l2;
  3665. endres = 1;
  3666. } else {
  3667. if (l2 > l1)
  3668. endres = -1;
  3669. else
  3670. endres = 0;
  3671. }
  3672. while (l1--) {
  3673. unsigned int a, b;
  3674. a = *(str1++);
  3675. b = *(str2++);
  3676. a = a - b;
  3677. if (a)
  3678. return a;
  3679. }
  3680. return endres;
  3681. }
  3682. static int mz_char_strcmp_ci(const char *who, const mzchar *str1, intptr_t l1, const mzchar *str2, intptr_t l2,
  3683. int use_locale, int size_shortcut)
  3684. {
  3685. intptr_t p1, p2, sp1, sp2, a, b;
  3686. mzchar spec1[SPECIAL_CASE_FOLD_MAX], spec2[SPECIAL_CASE_FOLD_MAX];
  3687. if (size_shortcut && (l1 != l2))
  3688. return 1;
  3689. #ifndef DONT_USE_LOCALE
  3690. if (use_locale) {
  3691. reset_locale();
  3692. if (locale_on) {
  3693. return do_locale_comp(who, str1, l1, str2, l2, 1);
  3694. }
  3695. }
  3696. #endif
  3697. p1 = sp1 = 0;
  3698. p2 = sp2 = 0;
  3699. while (((p1 < l1) || sp1) && ((p2 < l2) || sp2)) {
  3700. if (sp1) {
  3701. a = spec1[--sp1];
  3702. } else {
  3703. a = str1[p1];
  3704. if (scheme_isspecialcasing(a)) {
  3705. int pos, i;
  3706. pos = find_special_casing(a);
  3707. sp1 = uchar_special_casings[pos + 7];
  3708. pos = uchar_special_casings[pos + 8];
  3709. for (i = sp1; i--; pos++) {
  3710. spec1[i] = uchar_special_casing_data[pos];
  3711. }
  3712. a = spec1[--sp1];
  3713. } else {
  3714. a = scheme_tofold(a);
  3715. }
  3716. p1++;
  3717. }
  3718. if (sp2) {
  3719. b = spec2[--sp2];
  3720. } else {
  3721. b = str2[p2];
  3722. if (scheme_isspecialcasing(b)) {
  3723. int pos, i;
  3724. pos = find_special_casing(b);
  3725. sp2 = uchar_special_casings[pos + 7];
  3726. pos = uchar_special_casings[pos + 8];
  3727. for (i = sp2; i--; pos++) {
  3728. spec2[i] = uchar_special_casing_data[pos];
  3729. }
  3730. b = spec2[--sp2];
  3731. } else {
  3732. b = scheme_tofold(b);
  3733. }
  3734. p2++;
  3735. }
  3736. a = a - b;
  3737. if (a)
  3738. return a;
  3739. }
  3740. return ((p1 < l1) || sp1) - ((p2 < l2) || sp2);
  3741. }
  3742. static int mz_strcmp(const char *who, unsigned char *str1, intptr_t l1, unsigned char *str2, intptr_t l2)
  3743. {
  3744. intptr_t endres;
  3745. if (l1 > l2) {
  3746. l1 = l2;
  3747. endres = 1;
  3748. } else {
  3749. if (l2 > l1)
  3750. endres = -1;
  3751. else
  3752. endres = 0;
  3753. }
  3754. while (l1--) {
  3755. unsigned int a, b;
  3756. a = *(str1++);
  3757. b = *(str2++);
  3758. a = a - b;
  3759. if (a)
  3760. return a;
  3761. }
  3762. return endres;
  3763. }
  3764. /**********************************************************************/
  3765. /* byte string conversion */
  3766. /**********************************************************************/
  3767. static void close_converter(Scheme_Object *o, void *data)
  3768. {
  3769. Scheme_Converter *c = (Scheme_Converter *)o;
  3770. if (!c->closed) {
  3771. c->closed = 1;
  3772. if (c->kind == mzICONV_KIND) {
  3773. iconv_close(c->cd);
  3774. c->cd = (iconv_t)-1;
  3775. }
  3776. if (c->mref) {
  3777. scheme_remove_managed(c->mref, (Scheme_Object *)c);
  3778. c->mref = NULL;
  3779. }
  3780. }
  3781. }
  3782. Scheme_Object *scheme_open_converter(const char *from_e, const char *to_e)
  3783. {
  3784. Scheme_Converter *c;
  3785. iconv_t cd;
  3786. int kind;
  3787. int permissive;
  3788. int need_regis = 1;
  3789. Scheme_Custodian_Reference *mref;
  3790. if (!iconv_ready) init_iconv();
  3791. if (!*to_e || !*from_e)
  3792. reset_locale();
  3793. if ((!strcmp(from_e, "UTF-8")
  3794. || !strcmp(from_e, "UTF-8-permissive")
  3795. || (!*from_e && mzLOCALE_IS_UTF_8(current_locale_name)))
  3796. && (!strcmp(to_e, "UTF-8")
  3797. || (!*to_e && mzLOCALE_IS_UTF_8(current_locale_name)))) {
  3798. /* Use the built-in UTF-8<->UTF-8 converter: */
  3799. kind = mzUTF8_KIND;
  3800. if (!strcmp(from_e, "UTF-8-permissive"))
  3801. permissive = 0xFFFD;
  3802. else
  3803. permissive = 0;
  3804. cd = (iconv_t)-1;
  3805. need_regis = (*to_e && *from_e);
  3806. } else if ((!strcmp(from_e, "platform-UTF-8")
  3807. || !strcmp(from_e, "platform-UTF-8-permissive"))
  3808. && !strcmp(to_e, "platform-UTF-16")) {
  3809. kind = mzUTF8_TO_UTF16_KIND;
  3810. if (!strcmp(from_e, "platform-UTF-8-permissive"))
  3811. permissive = 0xFFFD;
  3812. else
  3813. permissive = 0;
  3814. cd = (iconv_t)-1;
  3815. need_regis = 0;
  3816. } else if (!strcmp(from_e, "platform-UTF-16")
  3817. && !strcmp(to_e, "platform-UTF-8")) {
  3818. kind = mzUTF16_TO_UTF8_KIND;
  3819. permissive = 0;
  3820. cd = (iconv_t)-1;
  3821. need_regis = 0;
  3822. } else {
  3823. if (!iconv_ready) init_iconv();
  3824. if (!mzCHK_PROC(iconv_open))
  3825. return scheme_false;
  3826. if (!*from_e || !*to_e)
  3827. reset_locale();
  3828. if (!*from_e)
  3829. from_e = mz_iconv_nl_langinfo();
  3830. if (!*to_e)
  3831. to_e = mz_iconv_nl_langinfo();
  3832. cd = iconv_open(to_e, from_e);
  3833. if (cd == (iconv_t)-1)
  3834. return scheme_false;
  3835. kind = mzICONV_KIND;
  3836. permissive = 0;
  3837. }
  3838. c = MALLOC_ONE_TAGGED(Scheme_Converter);
  3839. c->so.type = scheme_string_converter_type;
  3840. c->closed = 0;
  3841. c->kind = kind;
  3842. c->permissive = permissive;
  3843. c->cd = cd;
  3844. if (!need_regis)
  3845. mref = NULL;
  3846. else
  3847. mref = scheme_add_managed(NULL,
  3848. (Scheme_Object *)c,
  3849. close_converter,
  3850. NULL, 1);
  3851. c->mref = mref;
  3852. return (Scheme_Object *)c;
  3853. }
  3854. static Scheme_Object *byte_string_open_converter(int argc, Scheme_Object **argv)
  3855. {
  3856. Scheme_Object *s1, *s2;
  3857. char *from_e, *to_e;
  3858. if (!SCHEME_CHAR_STRINGP(argv[0]))
  3859. scheme_wrong_type("bytes-open-converter", "byte string", 0, argc, argv);
  3860. if (!SCHEME_CHAR_STRINGP(argv[1]))
  3861. scheme_wrong_type("bytes-open-converter", "byte string", 1, argc, argv);
  3862. scheme_custodian_check_available(NULL, "bytes-open-converter", "converter");
  3863. s1 = scheme_char_string_to_byte_string(argv[0]);
  3864. s2 = scheme_char_string_to_byte_string(argv[1]);
  3865. if (scheme_byte_string_has_null(s1))
  3866. return scheme_false;
  3867. if (scheme_byte_string_has_null(s2))
  3868. return scheme_false;
  3869. from_e = SCHEME_BYTE_STR_VAL(s1);
  3870. to_e = SCHEME_BYTE_STR_VAL(s2);
  3871. return scheme_open_converter(from_e, to_e);
  3872. }
  3873. static Scheme_Object *convert_one(const char *who, int opos, int argc, Scheme_Object *argv[])
  3874. {
  3875. char *r, *instr;
  3876. int status;
  3877. intptr_t amt_read, amt_wrote;
  3878. intptr_t istart, ifinish, ostart, ofinish;
  3879. Scheme_Object *a[3], *status_sym;
  3880. Scheme_Converter *c;
  3881. if (!SAME_TYPE(SCHEME_TYPE(argv[0]), scheme_string_converter_type))
  3882. scheme_wrong_type(who, "converter", 0, argc, argv);
  3883. if (opos > 1) {
  3884. if (!SCHEME_BYTE_STRINGP(argv[1]))
  3885. scheme_wrong_type(who, "bytes", 1, argc, argv);
  3886. scheme_get_substring_indices(who, argv[1], argc, argv, 2, 3, &istart, &ifinish);
  3887. } else {
  3888. istart = 0;
  3889. ifinish = 4; /* This is really a guess about how much space we need for a shift terminator */
  3890. }
  3891. if (argc > opos) {
  3892. if (SCHEME_TRUEP(argv[opos])) {
  3893. if (!SCHEME_MUTABLE_BYTE_STRINGP(argv[opos]))
  3894. scheme_wrong_type(who, "mutable byte string", opos, argc, argv);
  3895. r = SCHEME_BYTE_STR_VAL(argv[opos]);
  3896. scheme_get_substring_indices(who, argv[opos], argc, argv, opos + 1, opos + 2, &ostart, &ofinish);
  3897. } else {
  3898. int ip;
  3899. r = NULL;
  3900. for (ip = opos + 1; ip <= opos + 2; ip++) {
  3901. if (argc > ip) {
  3902. int ok = 0;
  3903. if (SCHEME_INTP(argv[ip]))
  3904. ok = SCHEME_INT_VAL(argv[ip]) >= 0;
  3905. else if (SCHEME_BIGNUMP(argv[ip]))
  3906. ok = SCHEME_BIGPOS(argv[ip]);
  3907. else if ((ip == opos + 2) && SCHEME_FALSEP(argv[ip]))
  3908. ok = 1;
  3909. if (!ok)
  3910. scheme_wrong_type(who,
  3911. ((ip == opos + 2)
  3912. ? "non-negative exact integer or #f"
  3913. : "non-negative exact integer"),
  3914. ip, argc, argv);
  3915. }
  3916. }
  3917. if ((argc > opos + 2) && SCHEME_TRUEP(argv[opos + 2])) {
  3918. Scheme_Object *delta;
  3919. if (scheme_bin_lt(argv[opos + 2], argv[opos + 1])) {
  3920. scheme_arg_mismatch(who,
  3921. "ending index is less than the starting index: ",
  3922. argv[opos + 2]);
  3923. }
  3924. delta = scheme_bin_minus(argv[opos + 2], argv[opos + 1]);
  3925. if (SCHEME_BIGNUMP(delta))
  3926. ofinish = -1;
  3927. else
  3928. ofinish = SCHEME_INT_VAL(delta);
  3929. ostart = 0;
  3930. } else {
  3931. ostart = 0;
  3932. ofinish = -1;
  3933. }
  3934. }
  3935. } else {
  3936. r = NULL;
  3937. ostart = 0;
  3938. ofinish = -1;
  3939. }
  3940. c = (Scheme_Converter *)argv[0];
  3941. if (c->closed)
  3942. scheme_arg_mismatch(who, "converter is closed: ", argv[0]);
  3943. instr = ((opos > 1) ? SCHEME_BYTE_STR_VAL(argv[1]) : NULL);
  3944. if (c->kind == mzUTF16_TO_UTF8_KIND) {
  3945. if (istart & 0x1) {
  3946. /* Copy to word-align */
  3947. char *c2;
  3948. c2 = (char *)scheme_malloc_atomic(ifinish - istart);
  3949. memcpy(c2, instr XFORM_OK_PLUS istart, ifinish - istart);
  3950. ifinish = ifinish - istart;
  3951. istart = 0;
  3952. instr = c2;
  3953. }
  3954. status = utf8_encode_x((const unsigned int *)instr, istart >> 1, ifinish >> 1,
  3955. (unsigned char *)r, ostart, ofinish,
  3956. &amt_read, &amt_wrote, 1);
  3957. amt_read -= (istart >> 1);
  3958. if (amt_read) {
  3959. if (!r) {
  3960. /* Need to allocate, then do it again: */
  3961. r = (char *)scheme_malloc_atomic(amt_wrote + 1);
  3962. utf8_encode_x((const unsigned int *)instr, istart >> 1, ifinish >> 1,
  3963. (unsigned char *)r, ostart, ofinish,
  3964. NULL, NULL, 1);
  3965. r[amt_wrote] = 0;
  3966. }
  3967. amt_read <<= 1;
  3968. }
  3969. /* We might get a -1 result because the input has an odd number of
  3970. bytes, and 2nd+next-to-last bytes form an unpaired
  3971. surrogate. In that case, the transformer normally needs one
  3972. more byte: Windows is little-endian, so we need the byte to
  3973. tell whether the surrogate is paired, and for all other
  3974. platforms (where we assume that surrogates are paired), we need
  3975. the byte to generate output. Technically, on a big-endian
  3976. non-Windows machine, we could generate the first byte of UTF-8
  3977. output and keep the byte as state, but we don't. */
  3978. if (status != -1) {
  3979. if (amt_read < ((ifinish - istart) & ~0x1)) {
  3980. /* Must have run out of output space */
  3981. status = 1;
  3982. } else {
  3983. /* Read all of input --- but it wasn't really all if there
  3984. was an odd number of bytes. */
  3985. if ((ifinish - istart) & 0x1)
  3986. status = -1;
  3987. else
  3988. status = 0;
  3989. }
  3990. }
  3991. } else if (c->kind != mzICONV_KIND) {
  3992. /* UTF-8 -> UTF-{8,16} "identity" converter, but maybe permissive */
  3993. if (instr) {
  3994. intptr_t _ostart, _ofinish;
  3995. int utf16;
  3996. if (c->kind == mzUTF8_TO_UTF16_KIND) {
  3997. _ostart = ostart;
  3998. _ofinish = ofinish;
  3999. if (_ostart & 0x1)
  4000. _ostart++;
  4001. _ostart >>= 1;
  4002. if (_ofinish > 0)
  4003. _ofinish >>= 1;
  4004. utf16 = 1;
  4005. } else {
  4006. _ostart = ostart;
  4007. _ofinish = ofinish;
  4008. utf16 = 0;
  4009. }
  4010. status = utf8_decode_x((unsigned char *)instr, istart, ifinish,
  4011. (unsigned int *)r, _ostart, _ofinish,
  4012. &amt_read, &amt_wrote,
  4013. 1, utf16, NULL, 1, c->permissive);
  4014. if (utf16) {
  4015. _ostart <<= 1;
  4016. amt_wrote <<= 1;
  4017. if ((ostart & 0x1) && (amt_wrote > _ostart)) {
  4018. /* Shift down one byte: */
  4019. memmove(r XFORM_OK_PLUS ostart, r XFORM_OK_PLUS _ostart, amt_wrote - _ostart);
  4020. }
  4021. }
  4022. amt_read -= istart;
  4023. amt_wrote -= _ostart;
  4024. if (status == -3) {
  4025. /* r is not NULL; ran out of room */
  4026. status = 1;
  4027. } else {
  4028. if (amt_wrote) {
  4029. if (!r) {
  4030. /* Need to allocate, then do it again: */
  4031. r = (char *)scheme_malloc_atomic(amt_wrote + 1);
  4032. utf8_decode_x((unsigned char *)instr, istart, ifinish,
  4033. (unsigned int *)r, ostart, _ofinish,
  4034. NULL, NULL,
  4035. 1, utf16, NULL, 1, c->permissive);
  4036. r[amt_wrote] = 0;
  4037. }
  4038. } else if (!r)
  4039. r = "";
  4040. if (status > 0)
  4041. status = 0;
  4042. }
  4043. } else {
  4044. r = "";
  4045. status = 0;
  4046. amt_read = 0;
  4047. amt_wrote = 0;
  4048. }
  4049. } else {
  4050. r = do_convert(c->cd, NULL, NULL, 0,
  4051. instr, istart, ifinish-istart,
  4052. r, ostart, ofinish-ostart,
  4053. !r, /* grow? */
  4054. 0,
  4055. (r ? 0 : 1), /* terminator */
  4056. &amt_read, &amt_wrote,
  4057. &status);
  4058. }
  4059. if (status == 0) {
  4060. /* Converted all input without error */
  4061. status_sym = complete_symbol;
  4062. } else if (status == 1) {
  4063. /* Filled output, more input ready */
  4064. status_sym = continues_symbol;
  4065. } else if (status == -1) {
  4066. /* Input ends in the middle of an encoding */
  4067. status_sym = aborts_symbol;
  4068. } else {
  4069. /* Assert: status == -2 */
  4070. /* Input has error (that won't be fixed by
  4071. adding more characters */
  4072. status_sym = error_symbol;
  4073. }
  4074. if (argc <= opos) {
  4075. a[0] = scheme_make_sized_byte_string(r, amt_wrote, 0);
  4076. } else {
  4077. a[0] = scheme_make_integer(amt_wrote);
  4078. }
  4079. if (opos > 1) {
  4080. a[1] = scheme_make_integer(amt_read);
  4081. a[2] = status_sym;
  4082. return scheme_values(3, a);
  4083. } else {
  4084. a[1] = status_sym;
  4085. return scheme_values(2, a);
  4086. }
  4087. }
  4088. static Scheme_Object *byte_string_convert(int argc, Scheme_Object *argv[])
  4089. {
  4090. return convert_one("bytes-convert", 4, argc, argv);
  4091. }
  4092. static Scheme_Object *byte_string_convert_end(int argc, Scheme_Object *argv[])
  4093. {
  4094. return convert_one("bytes-convert-end", 1, argc, argv);
  4095. }
  4096. void scheme_close_converter(Scheme_Object *conv)
  4097. {
  4098. close_converter(conv, NULL);
  4099. }
  4100. static Scheme_Object *byte_string_close_converter(int argc, Scheme_Object **argv)
  4101. {
  4102. if (!SAME_TYPE(SCHEME_TYPE(argv[0]), scheme_string_converter_type))
  4103. scheme_wrong_type("bytes-close-converter", "converter", 0, argc, argv);
  4104. scheme_close_converter(argv[0]);
  4105. return scheme_void;
  4106. }
  4107. static Scheme_Object *
  4108. byte_converter_p(int argc, Scheme_Object *argv[])
  4109. {
  4110. return (SAME_TYPE(SCHEME_TYPE(argv[0]), scheme_string_converter_type)
  4111. ? scheme_true
  4112. : scheme_false);
  4113. }
  4114. /**********************************************************************/
  4115. /* utf8 converter */
  4116. /**********************************************************************/
  4117. static intptr_t utf8_decode_x(const unsigned char *s, intptr_t start, intptr_t end,
  4118. unsigned int *us, intptr_t dstart, intptr_t dend,
  4119. intptr_t *ipos, intptr_t *jpos,
  4120. char compact, char utf16, int *_state,
  4121. int might_continue, int permissive)
  4122. /* Results:
  4123. non-negative => translation complete, = number of produced chars
  4124. -1 => input ended in middle of encoding (only if might_continue)
  4125. -2 => encoding error (only if permissive is 0)
  4126. -3 => not enough output room
  4127. ipos & jpos are filled with ending positions (between [d]start
  4128. and [d]end) before return, unless they are NULL.
  4129. compact => UTF-8 to UTF-8 or UTF-16 --- the latter if utf16
  4130. for Windows for utf16, decode extended UTF-8 that allows surrogates
  4131. _state provides initial state and is filled with ending state;
  4132. when it's not NULL, the us must be NULL
  4133. might_continue => allows -1 result without consuming characters
  4134. permissive is non-zero => use permissive as value for bad byte
  4135. sequences. When generating UTF-8, this must be an ASCII character
  4136. or U+FFFD. */
  4137. {
  4138. intptr_t i, j, oki;
  4139. int failmode = -3, state;
  4140. int init_doki;
  4141. int nextbits, v;
  4142. unsigned int sc;
  4143. # ifdef WINDOWS_UNICODE_SUPPORT
  4144. int pending_surrogate = 0;
  4145. # endif
  4146. if (_state) {
  4147. state = (*_state) & 0x7;
  4148. init_doki = (((*_state) >> 3) & 0x7);
  4149. nextbits = ((((*_state) >> 6) & 0xF) << 2);
  4150. /* Need v to detect 0xD800 through 0xDFFF
  4151. Note that we have 22 bits to work with, which is
  4152. is enough to detect > 0x10FFFF */
  4153. v = ((*_state) >> 10);
  4154. } else {
  4155. state = 0;
  4156. init_doki = 0;
  4157. nextbits = 0;
  4158. v = 0;
  4159. }
  4160. /* In non-permissive mode, a negative result means ill-formed input.
  4161. Permissive mode accepts anything and tries to convert it. In
  4162. that case, the strategy for illegal sequences is to convert
  4163. anything bad to the given "permissive" value. */
  4164. if (end < 0)
  4165. end = strlen((char *)s);
  4166. if (dend < 0)
  4167. dend = 0x7FFFFFFF;
  4168. # define ENCFAIL i = oki; failmode = -2; break
  4169. oki = start;
  4170. j = dstart;
  4171. i = start;
  4172. if (j < dend) {
  4173. while (i < end) {
  4174. sc = s[i];
  4175. if (sc < 0x80) {
  4176. if (state) {
  4177. /* In a sequence, but didn't continue */
  4178. state = 0;
  4179. nextbits = 0;
  4180. if (permissive) {
  4181. v = permissive;
  4182. i = oki;
  4183. j += init_doki;
  4184. } else {
  4185. ENCFAIL;
  4186. }
  4187. } else {
  4188. v = sc;
  4189. }
  4190. } else if ((sc & 0xC0) == 0x80) {
  4191. /* Continues a sequence ... */
  4192. if (state) {
  4193. /* ... and we're in one ... */
  4194. if (!nextbits || (sc & nextbits)) {
  4195. /* and we have required bits. */
  4196. v = (v << 6) + (sc & 0x3F);
  4197. nextbits = 0;
  4198. --state;
  4199. if (state) {
  4200. i++;
  4201. continue;
  4202. }
  4203. /* We finished. One last check: */
  4204. if ((((v >= 0xD800) && (v <= 0xDFFF))
  4205. || (v > 0x10FFFF))
  4206. # ifdef WINDOWS_UNICODE_SUPPORT
  4207. && (!utf16
  4208. /* If UTF-16 for Windows, just apply upper-limit check */
  4209. || (v > 0x10FFFF))
  4210. # endif
  4211. ) {
  4212. /* UTF-16 surrogates or other illegal code units */
  4213. if (permissive) {
  4214. v = permissive;
  4215. j += init_doki;
  4216. i = oki;
  4217. } else {
  4218. ENCFAIL;
  4219. }
  4220. }
  4221. } else {
  4222. /* ... but we're missing required bits. */
  4223. state = 0;
  4224. nextbits = 0;
  4225. if (permissive) {
  4226. v = permissive;
  4227. j += init_doki;
  4228. i = oki;
  4229. } else {
  4230. ENCFAIL;
  4231. }
  4232. }
  4233. } else {
  4234. /* ... but we're not in one */
  4235. if (permissive) {
  4236. v = permissive;
  4237. } else {
  4238. ENCFAIL;
  4239. }
  4240. }
  4241. } else if (state) {
  4242. /* bad: already in a sequence */
  4243. state = 0;
  4244. if (permissive) {
  4245. v = permissive;
  4246. i = oki;
  4247. j += init_doki;
  4248. } else {
  4249. ENCFAIL;
  4250. }
  4251. } else {
  4252. if ((sc & 0xE0) == 0xC0) {
  4253. if (sc & 0x1E) {
  4254. state = 1;
  4255. v = (sc & 0x1F);
  4256. i++;
  4257. continue;
  4258. }
  4259. /* else too small */
  4260. } else if ((sc & 0xF0) == 0xE0) {
  4261. state = 2;
  4262. v = (sc & 0xF);
  4263. if (!v)
  4264. nextbits = 0x20;
  4265. i++;
  4266. continue;
  4267. } else if ((sc & 0xF8) == 0xF0) {
  4268. v = (sc & 0x7);
  4269. if (v <= 4) {
  4270. state = 3;
  4271. if (!v)
  4272. nextbits = 0x30;
  4273. i++;
  4274. continue;
  4275. }
  4276. /* Else will be larger than 0x10FFFF, so fail */
  4277. }
  4278. /* Too small, or 0xFF or 0xFe, or start of a 5- or 6-byte sequence */
  4279. if (permissive) {
  4280. v = permissive;
  4281. } else {
  4282. ENCFAIL;
  4283. }
  4284. }
  4285. /* If we get here, we're supposed to output v */
  4286. if (compact) {
  4287. if (utf16) {
  4288. if (v > 0xFFFF) {
  4289. # ifdef WINDOWS_UNICODE_SUPPORT
  4290. if (pending_surrogate) {
  4291. if (us)
  4292. ((unsigned short *)us)[j] = pending_surrogate;
  4293. j++; /* Accept previously written unpaired surrogate */
  4294. pending_surrogate = 0;
  4295. }
  4296. # endif
  4297. if (j + 1 >= dend)
  4298. break;
  4299. if (us) {
  4300. v -= 0x10000;
  4301. ((unsigned short *)us)[j] = 0xD800 | ((v >> 10) & 0x3FF);
  4302. ((unsigned short *)us)[j+1] = 0xDC00 | (v & 0x3FF);
  4303. }
  4304. j++;
  4305. } else {
  4306. # ifdef WINDOWS_UNICODE_SUPPORT
  4307. /* We allow a surrogate by itself, but don't allow
  4308. a 0xDC00 after a 0xD800, otherwise multiple encodings can
  4309. map to the same thing. */
  4310. if ((v >= 0xD800) && (v <= 0xDFFF)) {
  4311. if (pending_surrogate && ((v & 0xDC00) == 0xDC00)) {
  4312. /* This looks like a surrogate pair, so disallow it. */
  4313. if (permissive) {
  4314. /* We need to fill in 6 permissive substitutions,
  4315. one for each input byte. If we can't put all 6,
  4316. then don't use any input. */
  4317. if (j + 5 >= dend) {
  4318. break;
  4319. } else {
  4320. int p;
  4321. if (us) {
  4322. for (p = 0; p < 5; p++) {
  4323. if (j + p >= dend)
  4324. break;
  4325. ((unsigned short *)us)[j+p] = permissive;
  4326. }
  4327. }
  4328. j += 5;
  4329. v = permissive;
  4330. }
  4331. } else {
  4332. ENCFAIL;
  4333. }
  4334. pending_surrogate = 0;
  4335. } else {
  4336. if (pending_surrogate) {
  4337. if (us)
  4338. ((unsigned short *)us)[j] = pending_surrogate;
  4339. j++; /* Accept previousy written unpaired surrogate */
  4340. pending_surrogate = 0;
  4341. if (j >= dend)
  4342. break;
  4343. }
  4344. if ((v & 0xDC00) == 0xD800)
  4345. pending_surrogate = v;
  4346. else
  4347. pending_surrogate = 0;
  4348. }
  4349. } else {
  4350. if (pending_surrogate) {
  4351. if (us)
  4352. ((unsigned short *)us)[j] = pending_surrogate;
  4353. j++; /* Accept previousy written unpaired surrogate */
  4354. pending_surrogate = 0;
  4355. if (j >= dend)
  4356. break;
  4357. }
  4358. }
  4359. if (pending_surrogate)
  4360. --j; /* don't accept unpaired surrogate, yet */
  4361. else if (us)
  4362. ((unsigned short *)us)[j] = v;
  4363. # else
  4364. if (us)
  4365. ((unsigned short *)us)[j] = v;
  4366. # endif
  4367. }
  4368. } else {
  4369. intptr_t delta;
  4370. delta = (i - oki);
  4371. if (delta) {
  4372. if (j + delta + 1 < dend) {
  4373. if (us)
  4374. memcpy(((char *)us) + j, s + oki, delta + 1);
  4375. j += delta;
  4376. } else
  4377. break;
  4378. } else if (v == 0xFFFD) {
  4379. if (j + 3 < dend) {
  4380. if (us) {
  4381. ((unsigned char *)us)[j] = 0xEF;
  4382. ((unsigned char *)us)[j+1] = 0xBF;
  4383. ((unsigned char *)us)[j+2] = 0xBD;
  4384. }
  4385. j += 2;
  4386. } else
  4387. break;
  4388. } else if (us) {
  4389. ((unsigned char *)us)[j] = v;
  4390. }
  4391. }
  4392. } else if (us) {
  4393. us[j] = v;
  4394. }
  4395. j++;
  4396. i++;
  4397. oki = i;
  4398. init_doki = 0;
  4399. if (j >= dend)
  4400. break;
  4401. }
  4402. }
  4403. if (_state) {
  4404. if (!state)
  4405. *_state = 0;
  4406. else
  4407. *_state = (state
  4408. | (((end - oki) + init_doki) << 3)
  4409. | ((nextbits >> 2) << 6)
  4410. | (v << 10));
  4411. } else if (state) {
  4412. if (might_continue || !permissive) {
  4413. failmode = -1;
  4414. i = end - 1; /* to ensure that failmode is returned */
  4415. } else if (permissive) {
  4416. for (i = oki; i < end; i++) {
  4417. if (j < dend) {
  4418. if (us) {
  4419. if (compact) {
  4420. if (utf16)
  4421. ((unsigned short *)us)[j] = permissive;
  4422. else
  4423. ((unsigned char *)us)[j] = permissive;
  4424. } else
  4425. us[j] = permissive;
  4426. }
  4427. j++;
  4428. } else
  4429. break;
  4430. }
  4431. oki = i;
  4432. }
  4433. }
  4434. # ifdef WINDOWS_UNICODE_SUPPORT
  4435. if (pending_surrogate)
  4436. oki -= 3;
  4437. #endif
  4438. if (ipos)
  4439. *ipos = oki;
  4440. if (jpos)
  4441. *jpos = j;
  4442. if (i < end)
  4443. return failmode;
  4444. # ifdef WINDOWS_UNICODE_SUPPORT
  4445. if (pending_surrogate) {
  4446. /* input must have ended right after surrogate */
  4447. return -1;
  4448. }
  4449. #endif
  4450. return j - dstart;
  4451. }
  4452. intptr_t scheme_utf8_decode(const unsigned char *s, intptr_t start, intptr_t end,
  4453. unsigned int *us, intptr_t dstart, intptr_t dend,
  4454. intptr_t *ipos, char utf16, int permissive)
  4455. {
  4456. return utf8_decode_x(s, start, end, us, dstart, dend,
  4457. ipos, NULL, utf16, utf16, NULL, 0, permissive);
  4458. }
  4459. intptr_t scheme_utf8_decode_as_prefix(const unsigned char *s, intptr_t start, intptr_t end,
  4460. unsigned int *us, intptr_t dstart, intptr_t dend,
  4461. intptr_t *ipos, char utf16, int permissive)
  4462. /* Always returns number of read characters, not error codes. */
  4463. {
  4464. intptr_t opos;
  4465. utf8_decode_x(s, start, end, us, dstart, dend,
  4466. ipos, &opos, utf16, utf16, NULL, 1, permissive);
  4467. return opos - dstart;
  4468. }
  4469. intptr_t scheme_utf8_decode_all(const unsigned char *s, intptr_t len, unsigned int *us, int permissive)
  4470. {
  4471. return utf8_decode_x(s, 0, len, us, 0, -1, NULL, NULL, 0, 0, NULL, 0, permissive);
  4472. }
  4473. intptr_t scheme_utf8_decode_prefix(const unsigned char *s, intptr_t len, unsigned int *us, int permissive)
  4474. /* us != NULL */
  4475. {
  4476. {
  4477. /* Try fast path (all ASCII) */
  4478. intptr_t i;
  4479. for (i = 0; i < len; i++) {
  4480. if (s[i] < 128)
  4481. us[i] = s[i];
  4482. else
  4483. break;
  4484. }
  4485. if (i == len)
  4486. return len;
  4487. }
  4488. return utf8_decode_x(s, 0, len, us, 0, -1, NULL, NULL, 0, 0, NULL, 1, permissive);
  4489. }
  4490. mzchar *scheme_utf8_decode_to_buffer_len(const unsigned char *s, intptr_t len,
  4491. mzchar *buf, intptr_t blen, intptr_t *_ulen)
  4492. {
  4493. intptr_t ulen;
  4494. ulen = utf8_decode_x(s, 0, len, NULL, 0, -1,
  4495. NULL, NULL, 0, 0,
  4496. NULL, 0, 0);
  4497. if (ulen < 0)
  4498. return NULL;
  4499. if (ulen + 1 > blen) {
  4500. buf = (mzchar *)scheme_malloc_atomic((ulen + 1) * sizeof(mzchar));
  4501. }
  4502. utf8_decode_x(s, 0, len, buf, 0, -1,
  4503. NULL, NULL, 0, 0,
  4504. NULL, 0, 0);
  4505. buf[ulen] = 0;
  4506. *_ulen = ulen;
  4507. return buf;
  4508. }
  4509. mzchar *scheme_utf8_decode_to_buffer(const unsigned char *s, intptr_t len,
  4510. mzchar *buf, intptr_t blen)
  4511. {
  4512. intptr_t ulen;
  4513. return scheme_utf8_decode_to_buffer_len(s, len, buf, blen, &ulen);
  4514. }
  4515. intptr_t scheme_utf8_decode_count(const unsigned char *s, intptr_t start, intptr_t end,
  4516. int *_state, int might_continue, int permissive)
  4517. {
  4518. intptr_t pos = 0;
  4519. if (!_state || !*_state) {
  4520. /* Try fast path (all ASCII): */
  4521. intptr_t i;
  4522. for (i = start; i < end; i++) {
  4523. if (s[i] > 127)
  4524. break;
  4525. }
  4526. if (i == end)
  4527. return end - start;
  4528. }
  4529. utf8_decode_x(s, start, end,
  4530. NULL, 0, -1,
  4531. NULL, &pos,
  4532. 0, 0, _state,
  4533. might_continue, permissive);
  4534. return pos;
  4535. }
  4536. static intptr_t utf8_encode_x(const unsigned int *us, intptr_t start, intptr_t end,
  4537. unsigned char *s, intptr_t dstart, intptr_t dend,
  4538. intptr_t *_ipos, intptr_t *_opos, char utf16)
  4539. /* Results:
  4540. -1 => input ended in the middle of an encoding - only when utf16 and _opos
  4541. non-negative => reports number of bytes/code-units produced */
  4542. {
  4543. intptr_t i, j, done = start;
  4544. if (dend < 0)
  4545. dend = 0x7FFFFFFF;
  4546. if (!s) {
  4547. unsigned int wc;
  4548. j = 0;
  4549. for (i = start; i < end; i++) {
  4550. if (utf16) {
  4551. wc = ((unsigned short *)us)[i];
  4552. if ((wc & 0xF800) == 0xD800) {
  4553. /* Unparse surrogates. We assume that the surrogates are
  4554. well formed, unless this is Windows or if we're at the
  4555. end and _opos is 0. */
  4556. # ifdef WINDOWS_UNICODE_SUPPORT
  4557. # define UNPAIRED_MASK 0xFC00
  4558. # else
  4559. # define UNPAIRED_MASK 0xF800
  4560. # endif
  4561. if (((i + 1) == end) && ((wc & UNPAIRED_MASK) == 0xD800) && _opos) {
  4562. /* Ended in the middle of a surrogate pair */
  4563. *_opos = j;
  4564. if (_ipos)
  4565. *_ipos = i;
  4566. return -1;
  4567. }
  4568. # ifdef WINDOWS_UNICODE_SUPPORT
  4569. if ((wc & 0xFC00) != 0xD800) {
  4570. /* Count as one */
  4571. } else if ((i + 1 >= end)
  4572. || (((((unsigned short *)us)[i+1]) & 0xFC00) != 0xDC00)) {
  4573. } else
  4574. # endif
  4575. {
  4576. i++;
  4577. wc = ((wc & 0x3FF) << 10) + ((((unsigned short *)us)[i]) & 0x3FF);
  4578. wc += 0x10000;
  4579. }
  4580. }
  4581. } else {
  4582. wc = us[i];
  4583. }
  4584. if (wc < 0x80) {
  4585. j += 1;
  4586. } else if (wc < 0x800) {
  4587. j += 2;
  4588. } else if (wc < 0x10000) {
  4589. j += 3;
  4590. } else if (wc < 0x200000) {
  4591. j += 4;
  4592. } else if (wc < 0x4000000) {
  4593. j += 5;
  4594. } else {
  4595. j += 6;
  4596. }
  4597. }
  4598. if (_ipos)
  4599. *_ipos = i;
  4600. if (_opos)
  4601. *_opos = j + dstart;
  4602. return j;
  4603. } else {
  4604. unsigned int wc;
  4605. j = dstart;
  4606. for (i = start; i < end; i++) {
  4607. if (utf16) {
  4608. wc = ((unsigned short *)us)[i];
  4609. if ((wc & 0xF800) == 0xD800) {
  4610. /* Unparse surrogates. We assume that the surrogates are
  4611. well formed on non-Windows platforms, but when _opos,
  4612. we detect ending in the middle of an surrogate pair. */
  4613. if (((i + 1) == end) && ((wc & UNPAIRED_MASK) == 0xD800) && _opos) {
  4614. /* Ended in the middle of a surrogate pair */
  4615. *_opos = j;
  4616. if (_ipos)
  4617. *_ipos = i;
  4618. return -1;
  4619. }
  4620. # ifdef WINDOWS_UNICODE_SUPPORT
  4621. if ((wc & 0xFC00) != 0xD800) {
  4622. /* Let the misplaced surrogate through */
  4623. } else if ((i + 1 >= end)
  4624. || (((((unsigned short *)us)[i+1]) & 0xFC00) != 0xDC00)) {
  4625. /* Let the misplaced surrogate through */
  4626. } else
  4627. # endif
  4628. {
  4629. i++;
  4630. wc = ((wc & 0x3FF) << 10) + ((((unsigned short *)us)[i]) & 0x3FF);
  4631. wc += 0x10000;
  4632. }
  4633. }
  4634. } else {
  4635. wc = us[i];
  4636. }
  4637. if (wc < 0x80) {
  4638. if (j + 1 > dend)
  4639. break;
  4640. s[j++] = wc;
  4641. } else if (wc < 0x800) {
  4642. if (j + 2 > dend)
  4643. break;
  4644. s[j++] = 0xC0 | ((wc & 0x7C0) >> 6);
  4645. s[j++] = 0x80 | (wc & 0x3F);
  4646. } else if (wc < 0x10000) {
  4647. if (j + 3 > dend)
  4648. break;
  4649. s[j++] = 0xE0 | ((wc & 0xF000) >> 12);
  4650. s[j++] = 0x80 | ((wc & 0x0FC0) >> 6);
  4651. s[j++] = 0x80 | (wc & 0x3F);
  4652. } else if (wc < 0x200000) {
  4653. if (j + 4 > dend)
  4654. break;
  4655. s[j++] = 0xF0 | ((wc & 0x1C0000) >> 18);
  4656. s[j++] = 0x80 | ((wc & 0x03F000) >> 12);
  4657. s[j++] = 0x80 | ((wc & 0x000FC0) >> 6);
  4658. s[j++] = 0x80 | (wc & 0x3F);
  4659. } else if (wc < 0x4000000) {
  4660. if (j + 5 > dend)
  4661. break;
  4662. s[j++] = 0xF8 | ((wc & 0x3000000) >> 24);
  4663. s[j++] = 0x80 | ((wc & 0x0FC0000) >> 18);
  4664. s[j++] = 0x80 | ((wc & 0x003F000) >> 12);
  4665. s[j++] = 0x80 | ((wc & 0x0000FC0) >> 6);
  4666. s[j++] = 0x80 | (wc & 0x3F);
  4667. } else {
  4668. if (j + 6 > dend)
  4669. break;
  4670. s[j++] = 0xFC | ((wc & 0x40000000) >> 30);
  4671. s[j++] = 0x80 | ((wc & 0x3F000000) >> 24);
  4672. s[j++] = 0x80 | ((wc & 0x00FC0000) >> 18);
  4673. s[j++] = 0x80 | ((wc & 0x0003F000) >> 12);
  4674. s[j++] = 0x80 | ((wc & 0x00000FC0) >> 6);
  4675. s[j++] = 0x80 | (wc & 0x3F);
  4676. }
  4677. done = i;
  4678. }
  4679. if (_ipos)
  4680. *_ipos = done;
  4681. if (_opos)
  4682. *_opos = j;
  4683. return j - dstart;
  4684. }
  4685. }
  4686. intptr_t scheme_utf8_encode(const unsigned int *us, intptr_t start, intptr_t end,
  4687. unsigned char *s, intptr_t dstart,
  4688. char utf16)
  4689. {
  4690. return utf8_encode_x(us, start, end,
  4691. s, dstart, -1,
  4692. NULL, NULL, utf16);
  4693. }
  4694. intptr_t scheme_utf8_encode_all(const unsigned int *us, intptr_t len, unsigned char *s)
  4695. {
  4696. return utf8_encode_x(us, 0, len, s, 0, -1, NULL, NULL, 0 /* utf16 */);
  4697. }
  4698. char *scheme_utf8_encode_to_buffer_len(const mzchar *s, intptr_t len,
  4699. char *buf, intptr_t blen,
  4700. intptr_t *_slen)
  4701. {
  4702. intptr_t slen;
  4703. /* ASCII with len < blen is a common case: */
  4704. if (len < blen) {
  4705. for (slen = 0; slen < len; slen++) {
  4706. if (s[slen] > 127)
  4707. break;
  4708. else
  4709. buf[slen] = s[slen];
  4710. }
  4711. if (slen == len) {
  4712. buf[slen] = 0;
  4713. *_slen = slen;
  4714. return buf;
  4715. }
  4716. }
  4717. slen = utf8_encode_x(s, 0, len, NULL, 0, -1, NULL, NULL, 0);
  4718. if (slen + 1 > blen) {
  4719. buf = (char *)scheme_malloc_atomic(slen + 1);
  4720. }
  4721. utf8_encode_x(s, 0, len, (unsigned char *)buf, 0, -1, NULL, NULL, 0);
  4722. buf[slen] = 0;
  4723. *_slen = slen;
  4724. return buf;
  4725. }
  4726. char *scheme_utf8_encode_to_buffer(const mzchar *s, intptr_t len,
  4727. char *buf, intptr_t blen)
  4728. {
  4729. intptr_t slen;
  4730. return scheme_utf8_encode_to_buffer_len(s, len, buf, blen, &slen);
  4731. }
  4732. unsigned short *scheme_ucs4_to_utf16(const mzchar *text, intptr_t start, intptr_t end,
  4733. unsigned short *buf, intptr_t bufsize,
  4734. intptr_t *ulen, intptr_t term_size)
  4735. {
  4736. mzchar v;
  4737. intptr_t extra, i, j;
  4738. unsigned short *utf16;
  4739. /* Count characters that fall outside UCS-2: */
  4740. for (i = start, extra = 0; i < end; i++) {
  4741. if (text[i] > 0xFFFF)
  4742. extra++;
  4743. }
  4744. if ((end - start) + extra + term_size < bufsize)
  4745. utf16 = buf;
  4746. else
  4747. utf16 = (unsigned short *)scheme_malloc_atomic(sizeof(unsigned short) * ((end - start) + extra + term_size));
  4748. for (i = start, j = 0; i < end; i++) {
  4749. v = text[i];
  4750. if (v > 0xFFFF) {
  4751. utf16[j++] = 0xD800 | ((v >> 10) & 0x3FF);
  4752. utf16[j++] = 0xDC00 | (v & 0x3FF);
  4753. } else
  4754. utf16[j++] = v;
  4755. }
  4756. *ulen = j;
  4757. return utf16;
  4758. }
  4759. mzchar *scheme_utf16_to_ucs4(const unsigned short *text, intptr_t start, intptr_t end,
  4760. mzchar *buf, intptr_t bufsize,
  4761. intptr_t *ulen, intptr_t term_size)
  4762. {
  4763. int wc;
  4764. intptr_t i, j;
  4765. for (i = start, j = 0; i < end; i++) {
  4766. wc = text[i];
  4767. if ((wc & 0xF800) == 0xD800) {
  4768. i++;
  4769. }
  4770. j++;
  4771. }
  4772. if (j + term_size >= bufsize)
  4773. buf = (mzchar *)scheme_malloc_atomic((j + term_size) * sizeof(mzchar));
  4774. for (i = start, j = 0; i < end; i++) {
  4775. wc = text[i];
  4776. if ((wc & 0xF800) == 0xD800) {
  4777. i++;
  4778. wc = ((wc & 0x3FF) << 10) + ((((unsigned short *)text)[i]) & 0x3FF);
  4779. wc += 0x10000;
  4780. }
  4781. buf[j++] = wc;
  4782. }
  4783. *ulen = j;
  4784. return buf;
  4785. }
  4786. /**********************************************************************/
  4787. /* machine type details */
  4788. /**********************************************************************/
  4789. /**************************** MacOS ***********************************/
  4790. #if defined(MACINTOSH_EVENTS) && !defined(OS_X)
  4791. # include <Gestalt.h>
  4792. extern intptr_t scheme_this_ip(void);
  4793. static void machine_details(char *s)
  4794. {
  4795. OSErr err;
  4796. intptr_t lng;
  4797. char sysvers[30];
  4798. char machine_name[256];
  4799. err = Gestalt(gestaltSystemVersion, &lng);
  4800. if (err != noErr) {
  4801. strcpy(sysvers, "<unknown system>");
  4802. } else {
  4803. int i;
  4804. sprintf(sysvers, "%X.%X",
  4805. (lng >> 8) & 0xff,
  4806. lng & 0xff);
  4807. /* remove trailing zeros, put dot before something else */
  4808. i = strlen(sysvers);
  4809. if (i > 1) {
  4810. if (sysvers[i-1] != '.') {
  4811. if (sysvers[i-1] == '0') {
  4812. sysvers[i-1] = 0;
  4813. i--;
  4814. } else {
  4815. sysvers[i] = sysvers[i-1];
  4816. sysvers[i-1] = '.';
  4817. i++;
  4818. sysvers[i] = 0;
  4819. }
  4820. }
  4821. }
  4822. }
  4823. err = Gestalt(gestaltMachineType, &lng);
  4824. if (err != noErr) {
  4825. strcpy(machine_name, "<unknown machine>");
  4826. } else {
  4827. Str255 machine_name_pascal;
  4828. GetIndString(machine_name_pascal, kMachineNameStrID, lng);
  4829. CopyPascalStringToC(machine_name_pascal, machine_name);
  4830. }
  4831. lng = scheme_this_ip();
  4832. sprintf(s, "%s %s %d.%d.%d.%d", sysvers, machine_name,
  4833. ((unsigned char *)&lng)[0],
  4834. ((unsigned char *)&lng)[1],
  4835. ((unsigned char *)&lng)[2],
  4836. ((unsigned char *)&lng)[3]);
  4837. }
  4838. #endif
  4839. /*************************** Windows **********************************/
  4840. #ifdef DOS_FILE_SYSTEM
  4841. # include <windows.h>
  4842. void machine_details(char *buff)
  4843. {
  4844. OSVERSIONINFO info;
  4845. BOOL hasInfo;
  4846. char *p;
  4847. info.dwOSVersionInfoSize = sizeof(info);
  4848. GetVersionEx(&info);
  4849. hasInfo = FALSE;
  4850. p = info.szCSDVersion;
  4851. while (p < info.szCSDVersion + sizeof(info.szCSDVersion) &&
  4852. *p) {
  4853. if (*p != ' ') {
  4854. hasInfo = TRUE;
  4855. break;
  4856. }
  4857. p = p XFORM_OK_PLUS 1;
  4858. }
  4859. sprintf(buff,"Windows %s %ld.%ld (Build %ld)%s%s",
  4860. (info.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS) ?
  4861. "9x" :
  4862. (info.dwPlatformId == VER_PLATFORM_WIN32_NT) ?
  4863. "NT" : "Unknown platform",
  4864. info.dwMajorVersion,info.dwMinorVersion,
  4865. (info.dwPlatformId == VER_PLATFORM_WIN32_NT) ?
  4866. info.dwBuildNumber :
  4867. info.dwBuildNumber & 0xFFFF,
  4868. hasInfo ? " " : "",hasInfo ? info.szCSDVersion : "");
  4869. }
  4870. #endif
  4871. /***************************** OSKit **********************************/
  4872. #ifdef USE_OSKIT_CONSOLE
  4873. void machine_details(char *buff)
  4874. {
  4875. strcpy(buff, "OSKit");
  4876. }
  4877. #endif
  4878. /***************************** Unix ***********************************/
  4879. #if (!defined(MACINTOSH_EVENTS) || defined(OS_X)) && !defined(DOS_FILE_SYSTEM) && !defined(USE_OSKIT_CONSOLE)
  4880. READ_ONLY static char *uname_locations[] = { "/bin/uname",
  4881. "/usr/bin/uname",
  4882. /* The above should cover everything, but
  4883. just in case... */
  4884. "/sbin/uname",
  4885. "/usr/sbin/uname",
  4886. "/usr/local/bin/uname",
  4887. "/usr/local/uname",
  4888. NULL };
  4889. static int try_subproc(Scheme_Object *subprocess_proc, char *prog)
  4890. {
  4891. Scheme_Object *a[5];
  4892. mz_jmp_buf * volatile savebuf, newbuf;
  4893. savebuf = scheme_current_thread->error_buf;
  4894. scheme_current_thread->error_buf = &newbuf;
  4895. if (!scheme_setjmp(newbuf)) {
  4896. a[0] = scheme_false;
  4897. a[1] = scheme_false;
  4898. a[2] = scheme_false;
  4899. a[3] = scheme_make_locale_string(prog);
  4900. a[4] = scheme_make_locale_string("-a");
  4901. _scheme_apply_multi(subprocess_proc, 5, a);
  4902. scheme_current_thread->error_buf = savebuf;
  4903. return 1;
  4904. } else {
  4905. scheme_clear_escape();
  4906. scheme_current_thread->error_buf = savebuf;
  4907. return 0;
  4908. }
  4909. }
  4910. void machine_details(char *buff)
  4911. {
  4912. Scheme_Object *subprocess_proc;
  4913. int i;
  4914. subprocess_proc = scheme_builtin_value("subprocess");
  4915. for (i = 0; uname_locations[i]; i++) {
  4916. if (scheme_file_exists(uname_locations[i])) {
  4917. /* Try running it. */
  4918. if (try_subproc(subprocess_proc, uname_locations[i])) {
  4919. Scheme_Object *sout, *sin, *serr;
  4920. intptr_t c;
  4921. sout = scheme_current_thread->ku.multiple.array[1];
  4922. sin = scheme_current_thread->ku.multiple.array[2];
  4923. serr = scheme_current_thread->ku.multiple.array[3];
  4924. scheme_close_output_port(sin);
  4925. scheme_close_input_port(serr);
  4926. /* Read result: */
  4927. strcpy(buff, "<unknown machine>");
  4928. c = scheme_get_bytes(sout, 1023, buff, 0);
  4929. buff[c] = 0;
  4930. scheme_close_input_port(sout);
  4931. /* Remove trailing whitespace (especially newlines) */
  4932. while (c && portable_isspace(((unsigned char *)buff)[c - 1])) {
  4933. buff[--c] = 0;
  4934. }
  4935. return;
  4936. }
  4937. }
  4938. }
  4939. strcpy(buff, "<unknown machine>");
  4940. }
  4941. #endif
  4942. /**********************************************************************/
  4943. /* Precise GC */
  4944. /**********************************************************************/
  4945. #ifdef MZ_PRECISE_GC
  4946. START_XFORM_SKIP;
  4947. #define MARKS_FOR_STRING_C
  4948. #include "mzmark.c"
  4949. static void register_traversers(void)
  4950. {
  4951. GC_REG_TRAV(scheme_string_converter_type, mark_string_convert);
  4952. }
  4953. END_XFORM_SKIP;
  4954. #endif