]> git.donarmstrong.com Git - lilypond.git/blobdiff - flower/string.cc
(Module):
[lilypond.git] / flower / string.cc
index 1c23618f39d61d30c622c1efeaae239cc1a6153c..e210ea21730cfe42e814b2b167fd0eac97b0448a 100644 (file)
 
   (c) 1997--2006 Han-Wen Nienhuys & Jan Nieuwenhuizen
 */
+#if !STD_STRING
 
 #ifndef _GNU_SOURCE // we want memmem
 #define _GNU_SOURCE
 #endif
 
-#include "string.hh"
+#include "std-string.hh"
 
 #include <cstdlib>
 #include <cstdio>
 #include <cassert>
 #include <cstring>
 
-#include <iostream>
 using namespace std;
 
 #include "libc-extension.hh"
 #include "string-convert.hh"
 
-/* std::string conversion helpers */
+/* std::string interface */
 
-#if STD_STRING
+namespace std {
 
-#include "std-string.hh"
+String::String (char const *s, int n)
+{
+  strh_.set ((Byte const *)s, n);
+}
 
-String::String (Std_string const &s)
+String::String (String const &s, int pos, ssize n)
 {
-  *this = String (s.c_str ());
+  *this = s.substr (pos, n);
 }
 
-String::operator Std_string () const
+String::String (int n, char c)
 {
-  return Std_string (this->c_str ());
+  *this = String_convert::char_string (c, n);
 }
 
+String &
+String::operator = (String const &source)
+{
+  strh_ = source.strh_;
+  return *this;
+}
+
+String
+String::substr (int pos, ssize n) const
+{
+#if 1
+  if (n == (ssize)-1 || n == (ssize)INT_MAX || n == NPOS)
+    n = length () - pos;
+  return cut_string (pos, n);
+#else
+  if (n == (ssize)-1 || n == (ssize)INT_MAX || n == NPOS)
+    n = length () - pos;
+  if (pos == 0)
+    return left_string (n);
+  else
+    return right_string (length () - pos).left_string (n);
 #endif
+}
 
-/* std::string interface */
+String
+String::insert (ssize pos, String s)
+{
+  *this = substr (0, pos) + s + substr (pos + 1);
+  return *this;
+}
 
-String::String (String const &s, int pos, int n)
+ssize
+String::copy (char *buf, ssize n, ssize pos) const
 {
-  if (n == -1)
-    n = s.length () - pos;
-  if (pos == 0)
-    *this = s.left_string (n);
-  else
-    *this = s.right_string (s.length () - pos).left_string (n);
+  assert (pos == 0);
+  memcpy (buf, strh_.to_bytes (), strh_.length () + 1);
+  return n; // ?
 }
 
-String::String (int n, char c)
+int
+String::compare (String const &s) const
 {
-  *this = String_convert::char_string (c, n);
+  char const *p1 = c_str ();
+  char const *p2 = s.c_str ();
+  if (p1 == p2)
+    return 0;
+
+  /*
+    don't forget the terminating '\0'
+  */
+  int f = min (length (), s.length ());
+  int cmp_length = 1+ f;
+  int i = memcmp (p1, p2, cmp_length);
+  return i;
 }
 
+char const *
+String::data () const
+{
+  return (char const*) to_bytes ();
+}
 
 bool
 String::empty () const
@@ -64,21 +109,31 @@ String::empty () const
 }
 
 int
-String::find (char c) const
+String::find (char c, int pos) const
 {
-  return index (c);
+  String f = right_string (length () - pos);
+  ssize n = f.index (c);
+  if (n != NPOS)
+    return pos + n;
+  return NPOS;
 }
 
 int
-String::find (String &s, int pos) const
+String::find (char const *c, int pos) const
+{
+  return find (String (c), pos);
+}
+
+int
+String::find (String s, int pos) const
 {
   if (!pos)
     return index (s);
   String f = right_string (length () - pos);
-  int n = f.index (s);
-  if (n != -1)
+  ssize n = f.index (s);
+  if (n != NPOS)
     return pos + n;
-  return -1;
+  return NPOS;
 }
 
 int
@@ -90,13 +145,38 @@ String::rfind (char c) const
 String
 String::replace (int pos, int n, String str)
 {
-  return String (*this, 0, pos) + str + String (*this, pos + n);
+  return this->substr (0, pos) + str + this->substr (pos + n);
+}
+
+void
+String::append (String s)
+{
+  strh_.append (s.to_bytes (), s.length ());
+}
+
+void
+String::operator += (String s)
+{
+  append (s);
 }
 
+int
+String::length () const
+{
+  return strh_.length ();
+}
+
+
 
 
 /* String */
 
+int
+String::compare (String const &s1, String const &s2)
+{
+  return s1.compare (s2);
+}
+
 #ifdef STRING_DEBUG
 void *mymemmove (void *dest, void const *src, size_t n);
 #define memmove mymemmove
@@ -119,76 +199,8 @@ String::get_copy_str0 () const
 }
 
 \f
-/*
-  copying, constructing.
-*/
-String &
-String::operator = (String const &source)
-{
-  strh_ = source.strh_;
-  return *this;
-}
-
-String::String (Byte const *byte, int len_i)
-{
-  strh_.set (byte, len_i);
-}
-
-/**
-   @see
-   String_convert::
-*/
-String
-to_string (char c, int n)
-{
-  return String_convert::char_string (c, n);
-}
-
-String
-to_string (double f, char const *format)
-{
-  return String_convert::double_string (f, format);
-}
-
-String
-to_string (int i, char const *format)
-{
-  return String_convert::int_string (i, format);
-}
-
-String
-to_string (bool b)
-{
-  return String_convert::bool_string (b);
-}
-String
-to_string (long b)
-{
-  return String_convert::long_string (b);
-}
-
-String
-to_string (char const *format, ...)
-{
-  va_list args;
-  va_start (args, format);
-  String str = String_convert::vform_string (format, args);
-  va_end (args);
-  return str;
-}
-
-\f
-void
-String::append (String s)
-{
-  strh_.append (s.to_bytes (), s.length ());
-}
-void
-String::operator += (String s)
-{
-  append (s);
-}
 
+#if 0
 void
 String::prepend (String s)
 {
@@ -196,11 +208,8 @@ String::prepend (String s)
   *this = s;
 }
 
-int
-String::length () const
-{
-  return strh_.length ();
-}
+#endif
+
 
 Byte const *
 String::to_bytes () const
@@ -214,81 +223,25 @@ String::get_bytes ()
   return strh_.get_bytes ();
 }
 
-char *
-String::get_str0 ()
-{
-  return strh_.get_str0 ();
-}
-
-#ifndef DISALLOW_OLD_STRING
-bool
-String::is_empty () const
-{
-  return !length ();
-}
-#endif
-
-
-/**
-   Do a signed comparison,  analogous to memcmp;
-*/
-int
-String::compare (String const &s1, String const &s2)
-{
-  Byte const *p1 = s1.to_bytes ();
-  Byte const *p2 = s2.to_bytes ();
-  if (p1 == p2)
-    return 0;
-
-  /*
-    don't forget the terminating '\0'
-  */
-  int f = min (s1.length (), s2.length ());
-  int cmp_length = 1+ f;
-  int i = memcmp (p1, p2, cmp_length);
-  return i;
-}
-
 \f
 int
 String::index_last (char const c) const
 {
   if (!length ())
-    return -1;
+    return NPOS;
 
   char const *me = strh_.c_str ();
   char const *p = (char const *)memrchr ((Byte *)me, length (), c);
   if (p)
     return p - me;
-  return -1;
-}
-
-int
-String::index_last (char const *string) const // UGK!
-{
-  assert (false);              // broken
-  int len = strlen (string); // ugrh
-  if (!length () || !len)
-    return -1;
-
-  int next_i = index (string);
-  if (next_i == -1)
-    return -1;
-
-  int index_i = 0;
-  while (next_i >= 0)
-    {
-      index_i += next_i;
-      next_i = right_string (length () - index_i - len).index (string);
-    }
-  return index_i;
+  return NPOS;
 }
 
 /** find  a character.
 
 @return
 the index of the leftmost character #c# (0 <= return < length ()),
-or   -1 if not found.
+or   NPOS if not found.
 
 ? should return length ()?, as in string.left_string (index (delimiter))
 */
@@ -299,7 +252,7 @@ String::index (char c) const
   char const *p = (char const *) memchr (me, c, length ());
   if (p)
     return p - me;
-  return -1;
+  return NPOS;
 }
 
 /**
@@ -320,14 +273,14 @@ String::index (String searchfor) const
   if (p)
     return p - me;
 
-  return -1;
+  return NPOS;
 }
 
 /** find chars of a set.
 
 @return
 
-the index of the leftmost occurance of an element of #set#.  -1 if
+the index of the leftmost occurance of an element of #set#.  NPOS if
 nothing is found.
 */
 int
@@ -335,7 +288,7 @@ String::index_any (String set) const
 {
   int n = length ();
   if (!n)
-    return -1;
+    return NPOS;
 
   void const *me = (void const *) strh_.c_str ();
   for (int i = 0; i < set.length (); i++)
@@ -344,7 +297,7 @@ String::index_any (String set) const
       if (found)
        return found - (char const *)me;
     }
-  return -1;
+  return NPOS;
 }
 \f
 String
@@ -371,7 +324,7 @@ String::right_string (int n) const
   if (n < 1)
     return "";
 
-  return String (strh_.to_bytes () + length () - n, n);
+  return String (strh_.c_str () + length () - n, n);
 }
 
 String
@@ -403,28 +356,10 @@ String::cut_string (int index_i, int n) const
   if ((n > length ()) || (index_i + n > length ()))
     n = length () - index_i;
 
-  return String (to_bytes () + index_i, n);
+  return String (c_str () + index_i, n);
 }
 \f
 
-void
-String::to_upper ()
-{
-  strnupr (get_str0 (), length ());
-}
-
-void
-String::to_lower ()
-{
-  strnlwr (get_str0 (), length ());
-}
-
-void
-String::reverse ()
-{
-  memrev (get_bytes (), length ());
-}
-
 int
 String::to_int () const
 {
@@ -438,6 +373,8 @@ String::to_double () const
 }
 
 #ifdef STREAM_SUPPORT
+#include <iostream>
+
 ostream &
 operator << (ostream &os, String d)
 {
@@ -461,7 +398,7 @@ String::substitute (String find, String replace)
 {
   int n = find.length ();
   int m = replace.length ();
-  for (int i = index (find), j = 0; i > -1;
+  for (ssize i = index (find), j = 0; i != NPOS;
        i = right_string (length () - j).index (find))
     {
       *this = left_string (i + j)
@@ -475,7 +412,12 @@ String::substitute (String find, String replace)
 String
 String::substitute (char find, char replace)
 {
-  for (int i = index (find); i > - 1; i = index (find))
+  for (ssize i = index (find); i != NPOS; i = index (find))
     (*this)[i] = replace;
   return *this;
 }
+
+}
+
+#endif /* !STD_STRING */
+