* Unified ssymbols and msymbols to a single symbol type 'scm_tc7_symbol'.
[bpt/guile.git] / libguile / strings.c
index 733667d..35733e6 100644 (file)
@@ -45,6 +45,8 @@
 \f
 
 #include <stdio.h>
+#include <string.h>
+
 #include "libguile/_scm.h"
 #include "libguile/chars.h"
 
@@ -60,10 +62,16 @@ SCM_DEFINE (scm_string_p, "string?", 1, 0, 0,
            "Returns #t iff OBJ is a string, else returns #f.")
 #define FUNC_NAME s_scm_string_p
 {
-  return SCM_BOOL(SCM_STRINGP (obj));
+  return SCM_BOOL (SCM_STRINGP (obj));
 }
 #undef FUNC_NAME
 
+#if SCM_DEBUG_DEPRECATED == 0
+
+/* The concept of read-only strings will disappear in next release
+ * of Guile.
+ */
+
 SCM_DEFINE (scm_read_only_string_p, "read-only-string?", 1, 0, 0, 
            (SCM x),
            "Return true if OBJ can be read as a string,\n\n"
@@ -81,10 +89,13 @@ SCM_DEFINE (scm_read_only_string_p, "read-only-string?", 1, 0, 0,
 }
 #undef FUNC_NAME
 
+#endif /* DEPRECATED */
+
 SCM_REGISTER_PROC (s_scm_list_to_string, "list->string", 1, 0, 0, scm_string);
 
 SCM_DEFINE (scm_string, "string", 0, 0, 1, 
             (SCM chrs),
+           "@deffnx primitive list->string chrs\n"
            "Returns a newly allocated string composed of the arguments, CHRS.")
 #define FUNC_NAME s_scm_string
 {
@@ -114,27 +125,16 @@ SCM_DEFINE (scm_string, "string", 0, 0, 1,
 #undef FUNC_NAME
 
 SCM 
-scm_makstr (long len, int slots)
+scm_makstr (long len, int dummy)
 {
   SCM s;
-  scm_bits_t * mem;
+  char *mem = (char *) scm_must_malloc (len + 1, "scm_makstr");
 
+  mem[len] = 0;
   SCM_NEWCELL (s);
-  --slots;
-  SCM_REDEFER_INTS;
-  mem = (scm_bits_t *) scm_must_malloc (sizeof (scm_bits_t) * (slots + 1) 
-                                       + len + 1, "scm_makstr");
-  if (slots >= 0)
-    {
-      int x;
-      mem[slots] = (scm_bits_t) mem;
-      for (x = 0; x < slots; ++x)
-       mem[x] = SCM_UNPACK (SCM_BOOL_F);
-    }
-  SCM_SETCHARS (s, (char *) (mem + slots + 1));
+  SCM_SETCHARS (s, mem);
   SCM_SETLENGTH (s, len, scm_tc7_string);
-  SCM_REALLOW_INTS;
-  SCM_CHARS (s)[len] = 0;
+
   return s;
 }
 
@@ -183,9 +183,9 @@ scm_take0str (char *s)
 }
 
 SCM 
-scm_makfromstr (const char *src, scm_sizet len, int slots)
+scm_makfromstr (const char *src, scm_sizet len, int dummy)
 {
-  SCM s = scm_makstr (len, slots);
+  SCM s = scm_makstr (len, 0);
   char *dst = SCM_CHARS (s);
 
   while (len--)
@@ -241,8 +241,8 @@ SCM_DEFINE (scm_string_length, "string-length", 1, 0, 0,
            "Returns the number of characters in STRING")
 #define FUNC_NAME s_scm_string_length
 {
-  SCM_VALIDATE_ROSTRING (1,string);
-  return SCM_MAKINUM (SCM_ROLENGTH (string));
+  SCM_VALIDATE_STRINGORSUBSTR (1, string);
+  return SCM_MAKINUM (SCM_LENGTH (string));
 }
 #undef FUNC_NAME
 
@@ -254,7 +254,7 @@ SCM_DEFINE (scm_string_ref, "string-ref", 2, 0, 0,
 {
   int idx;
 
-  SCM_VALIDATE_ROSTRING (1, str);
+  SCM_VALIDATE_STRINGORSUBSTR (1, str);
   SCM_VALIDATE_INUM_COPY (2, k, idx);
   SCM_ASSERT_RANGE (2, k, idx >= 0 && idx < SCM_ROLENGTH (str));
   return SCM_MAKE_CHAR (SCM_ROUCHARS (str)[idx]);
@@ -276,7 +276,6 @@ SCM_DEFINE (scm_string_set_x, "string-set!", 3, 0, 0,
 #undef FUNC_NAME
 
 
-
 SCM_DEFINE (scm_substring, "substring", 2, 1, 0,
            (SCM str, SCM start, SCM end),
            "Returns a newly allocated string formed from the characters\n"
@@ -286,18 +285,23 @@ SCM_DEFINE (scm_substring, "substring", 2, 1, 0,
             "0 <= START <= END <= (string-length STR).")
 #define FUNC_NAME s_scm_substring
 {
-  long l;
-  SCM_VALIDATE_ROSTRING (1,str);
-  SCM_VALIDATE_INUM (2,start);
-  SCM_VALIDATE_INUM_DEF (3,end,SCM_ROLENGTH(str));
-  SCM_ASSERT_RANGE (2,start,SCM_INUM (start) <= SCM_ROLENGTH (str));
-  SCM_ASSERT_RANGE (2,end,SCM_INUM (end) <= SCM_ROLENGTH (str));
-  l = SCM_INUM (end)-SCM_INUM (start);
-  SCM_ASSERT (l >= 0, SCM_MAKINUM (l), SCM_OUTOFRANGE, FUNC_NAME);
-  return scm_makfromstr (&SCM_ROCHARS (str)[SCM_INUM (start)], (scm_sizet)l, 0);
+  long int from;
+  long int to;
+
+  SCM_VALIDATE_STRINGORSUBSTR (1, str);
+  SCM_VALIDATE_INUM (2, start);
+  SCM_VALIDATE_INUM_DEF (3, end, SCM_ROLENGTH (str));
+
+  from = SCM_INUM (start);
+  SCM_ASSERT_RANGE (2, start, 0 <= from && from <= SCM_ROLENGTH (str));
+  to = SCM_INUM (end);
+  SCM_ASSERT_RANGE (3, end, from <= to && to <= SCM_ROLENGTH (str));
+
+  return scm_makfromstr (&SCM_ROCHARS (str)[from], (scm_sizet) (to - from), 0);
 }
 #undef FUNC_NAME
 
+
 SCM_DEFINE (scm_string_append, "string-append", 0, 0, 1, 
             (SCM args),
            "Returns a newly allocated string whose characters form the\n"
@@ -308,13 +312,13 @@ SCM_DEFINE (scm_string_append, "string-append", 0, 0, 1,
   register long i = 0;
   register SCM l, s;
   register unsigned char *data;
-  for (l = args;SCM_CONSP (l);) {
+
+  SCM_VALIDATE_REST_ARGUMENT (args);
+  for (l = args; !SCM_NULLP (l); l = SCM_CDR (l)) {
     s = SCM_CAR (l);
-    SCM_VALIDATE_ROSTRING (SCM_ARGn,s);
+    SCM_VALIDATE_STRINGORSUBSTR (SCM_ARGn,s);
     i += SCM_ROLENGTH (s);
-    l = SCM_CDR (l);
   }
-  SCM_ASSERT (SCM_NULLP (l), args, SCM_ARGn, FUNC_NAME);
   res = scm_makstr (i, 0);
   data = SCM_UCHARS (res);
   for (l = args;SCM_NIMP (l);l = SCM_CDR (l)) {
@@ -325,6 +329,14 @@ SCM_DEFINE (scm_string_append, "string-append", 0, 0, 1,
 }
 #undef FUNC_NAME
 
+#if SCM_DEBUG_DEPRECATED == 0
+
+/* Explicit shared substrings will disappear from Guile.
+ *
+ * Instead, "normal" strings will be implemented using sharing
+ * internally, combined with a copy-on-write strategy.
+ */
+
 SCM_DEFINE (scm_make_shared_substring, "make-shared-substring", 1, 2, 0,
            (SCM str, SCM frm, SCM to),
            "Return a shared substring of @var{str}.  The semantics are the same as\n"
@@ -375,6 +387,8 @@ SCM_DEFINE (scm_make_shared_substring, "make-shared-substring", 1, 2, 0,
 }
 #undef FUNC_NAME
 
+#endif /* DEPRECATED */
+
 void
 scm_init_strings ()
 {