PageRenderTime 85ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 1ms

/racket/src/racket/src/string.c

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