Fix Char_str().

This commit is contained in:
Jorge Acereda 2020-05-09 12:21:14 +02:00
parent 0fb9f9a56e
commit 83e8c1a874
4 changed files with 25 additions and 6 deletions

View File

@ -23,7 +23,7 @@ void String_string_MINUS_set_BANG_(String *s, int i, char ch) {
void String_string_MINUS_set_MINUS_at_BANG_(String *into, int i,
const String *src) {
char *dest = (*into) + i;
int lsrc = strlen(*src);
size_t lsrc = strlen(*src);
/* given a string and indices
*
* 0 1 2 3 4 5 6 7 8 9
@ -166,9 +166,13 @@ String Bool_format(const String *str, bool b) {
return buffer;
}
String Char_str(char c) {
String buffer = CARP_MALLOC(2);
sprintf(buffer, "%c", c);
String Char_str(int c) {
char buf[16];
int sz = snprintf(buf, sizeof(buf), "%lc", c);
size_t nsz = sz + 1;
String buffer = CARP_MALLOC(nsz);
assert(sz > 0);
memcpy(buffer, buf, nsz);
return buffer;
}

View File

@ -8,6 +8,7 @@ typedef SSIZE_T ssize_t;
#include <unistd.h>
#endif
#include <inttypes.h>
#include <locale.h>
typedef char *String;
typedef char *Pattern;

View File

@ -110,7 +110,7 @@ toC toCMode (Binder meta root) = emitterSrc (execState (visit startingIndent roo
'\t' -> "'\\t'"
'\n' -> "'\\n'"
'\\' -> "'\\\\'"
x -> ['\'', x, '\'']
x -> ['U', '\'', x, '\'']
Closure elem _ -> visit indent elem
Sym _ _ -> visitSymbol indent xobj
(Defn _) -> error (show (DontVisitObj xobj))
@ -915,7 +915,9 @@ wrapInInitFunction :: Bool -> String -> String
wrapInInitFunction with_core src =
"void carp_init_globals(int argc, char** argv) {\n" ++
(if with_core
then " System_args.len = argc;\n System_args.data = argv;\n"
then
" System_args.len = argc;\n System_args.data = argv;\n" ++
" setlocale(LC_CTYPE, \"\");\n"
else "")
++ src ++
"}"

View File

@ -4,6 +4,18 @@
(use Test)
(deftest test
(assert-equal test
"a"
&(str \a)
"str works with ASCII")
(assert-equal test
"ñ"
&(str \ñ)
"str works with Latin1")
(assert-equal test
"😲"
&(str \😲)
"str works with Emoji")
(assert-true test
(= \a \a)
"char = works as expected I")