core: Prepare for M2-Planet: string.c.

* src/string.c: Rewrite C constructs not supported by M2-Planet.
This commit is contained in:
Jan Nieuwenhuizen 2019-10-21 19:47:18 +02:00
parent 50e145e109
commit 0b29b305e7
No known key found for this signature in database
GPG Key ID: F3C1A0D9C1D65273
1 changed files with 25 additions and 14 deletions

View File

@ -52,11 +52,13 @@ list_to_cstring (SCM list, size_t *size)
{ {
if (i > MAX_STRING) if (i > MAX_STRING)
assert_max_string (i, "list_to_string", g_buf); assert_max_string (i, "list_to_string", g_buf);
g_buf[i++] = VALUE (car (list)); g_buf[i] = VALUE (car (list));
i = i + 1;
list = cdr (list); list = cdr (list);
} }
g_buf[i] = 0; g_buf[i] = 0;
*size = i; size[0] = i;
return g_buf; return g_buf;
} }
@ -73,11 +75,17 @@ make_bytes (char const *s, size_t length)
SCM x = alloc (size); SCM x = alloc (size);
TYPE (x) = TBYTES; TYPE (x) = TBYTES;
LENGTH (x) = length; LENGTH (x) = length;
char *p = (char *) &g_cells[x].cdr; #if __M2_PLANET__
if (!length) char *p = &g_cells[x];
*(char *) p = 0; p = p + 2 * sizeof (SCM);
#else
char *p = &CDR (x);
#endif
if (length == 0)
p[0] = 0;
else else
memcpy (p, s, length + 1); memcpy (p, s, length + 1);
return x; return x;
} }
@ -85,7 +93,7 @@ SCM
make_string (char const *s, size_t length) make_string (char const *s, size_t length)
{ {
if (length > MAX_STRING) if (length > MAX_STRING)
assert_max_string (length, "make_string", (char *) s); assert_max_string (length, "make_string", s);
SCM x = make_cell__ (TSTRING, length, 0); SCM x = make_cell__ (TSTRING, length, 0);
SCM v = make_bytes (s, length); SCM v = make_bytes (s, length);
CDR (x) = v; CDR (x) = v;
@ -93,7 +101,7 @@ make_string (char const *s, size_t length)
} }
SCM SCM
string_equal_p (SCM a, SCM b) ///((name . "string=?")) string_equal_p (SCM a, SCM b) /*:((name . "string=?")) */
{ {
if (!((TYPE (a) == TSTRING && TYPE (b) == TSTRING) || (TYPE (a) == TKEYWORD || TYPE (b) == TKEYWORD))) if (!((TYPE (a) == TSTRING && TYPE (b) == TSTRING) || (TYPE (a) == TKEYWORD || TYPE (b) == TKEYWORD)))
{ {
@ -113,9 +121,10 @@ string_equal_p (SCM a, SCM b) ///((name . "string=?"))
} }
if (a == b if (a == b
|| STRING (a) == STRING (b) || STRING (a) == STRING (b)
|| (!LENGTH (a) && !LENGTH (b)) || (LENGTH (a) == 0 && LENGTH (b) == 0)
|| (LENGTH (a) == LENGTH (b) && !memcmp (CSTRING (a), CSTRING (b), LENGTH (a)))) || (LENGTH (a) == LENGTH (b) && !memcmp (CSTRING (a), CSTRING (b), LENGTH (a))))
return cell_t; return cell_t;
return cell_f; return cell_f;
} }
@ -158,8 +167,9 @@ SCM
bytes_to_list (char const *s, size_t i) bytes_to_list (char const *s, size_t i)
{ {
SCM p = cell_nil; SCM p = cell_nil;
while (i--) while (i != 0)
{ {
i = i - 1;
int c = (0x100 + s[i]) % 0x100; int c = (0x100 + s[i]) % 0x100;
p = cons (MAKE_CHAR (c), p); p = cons (MAKE_CHAR (c), p);
} }
@ -194,7 +204,7 @@ list_to_string (SCM list)
} }
SCM SCM
read_string (SCM port) ///((arity . n)) read_string (SCM port) /*:((arity . n)) */
{ {
int fd = __stdin; int fd = __stdin;
if (TYPE (port) == TPAIR && TYPE (car (port)) == TNUMBER) if (TYPE (port) == TPAIR && TYPE (car (port)) == TNUMBER)
@ -205,7 +215,8 @@ read_string (SCM port) ///((arity . n))
{ {
if (i > MAX_STRING) if (i > MAX_STRING)
assert_max_string (i, "read_string", g_buf); assert_max_string (i, "read_string", g_buf);
g_buf[i++] = c; g_buf[i] = c;
i = i + 1;
c = readchar (); c = readchar ();
} }
g_buf[i] = 0; g_buf[i] = 0;
@ -214,7 +225,7 @@ read_string (SCM port) ///((arity . n))
} }
SCM SCM
string_append (SCM x) ///((arity . n)) string_append (SCM x) /*:((arity . n)) */
{ {
char *p = g_buf; char *p = g_buf;
g_buf[0] = 0; g_buf[0] = 0;
@ -224,8 +235,8 @@ string_append (SCM x) ///((arity . n))
SCM string = CAR (x); SCM string = CAR (x);
assert (TYPE (string) == TSTRING); assert (TYPE (string) == TSTRING);
memcpy (p, CSTRING (string), LENGTH (string) + 1); memcpy (p, CSTRING (string), LENGTH (string) + 1);
p += LENGTH (string); p = p + LENGTH (string);
size += LENGTH (string); size = size + LENGTH (string);
if (size > MAX_STRING) if (size > MAX_STRING)
assert_max_string (size, "string_append", g_buf); assert_max_string (size, "string_append", g_buf);
x = CDR (x); x = CDR (x);