]> git.donarmstrong.com Git - lilypond.git/blobdiff - flower/string.cc
(Module):
[lilypond.git] / flower / string.cc
index 4ddfcec9c5e9133349a2c66961d195fbd8118566..e210ea21730cfe42e814b2b167fd0eac97b0448a 100644 (file)
 /*
+  string.cc - implement String
 
- string.cc - implement String
- (c)  1997--1999 Han-Wen Nienhuys & Jan Nieuwenhuizen
-
- */
+  (c) 1997--2006 Han-Wen Nienhuys & Jan Nieuwenhuizen
+*/
+#if !STD_STRING
 
 #ifndef _GNU_SOURCE // we want memmem
 #define _GNU_SOURCE
 #endif
 
-#include <stdlib.h>
-#include <stdio.h>
+#include "std-string.hh"
+
+#include <cstdlib>
+#include <cstdio>
+#include <cassert>
+#include <cstring>
 
-#include <assert.h>
-#include <string.h>
-#include <stdarg.h>
+using namespace std;
 
-#include "string.hh"
 #include "libc-extension.hh"
 #include "string-convert.hh"
 
-#ifdef STRING_DEBUG
-void* mymemmove (void* dest, void const* src, size_t n);
-#define memmove mymemmove
-#endif
+/* std::string interface */
 
-// return array, alloced with new.
-Byte*
-String::copy_byte_p () const
+namespace std {
+
+String::String (char const *s, int n)
 {
-  Byte const* src = strh_.byte_C ();
-  Byte* dest = new Byte[strh_.length_i () + 1];
-  memcpy (dest, src, strh_.length_i () + 1);
-  return dest;    
+  strh_.set ((Byte const *)s, n);
 }
 
-char*
-String::copy_ch_p () const
+String::String (String const &s, int pos, ssize n)
 {
-  return (char*)copy_byte_p ();
+  *this = s.substr (pos, n);
 }
 
-void
-String::print_on (ostream& os) const
+String::String (int n, char c)
 {
-  if (!strh_.is_binary_bo ())
-    os << ch_C ();
-  else
-    for (int i = 0; i < length_i (); i++)
-      os << (Byte) (*this)[ i ];
+  *this = String_convert::char_string (c, n);
 }
-\f
-/*
-  copying, constructing.
- */
-String&
-String::operator = (String const&source)
+
+String &
+String::operator = (String const &source)
 {
   strh_ = source.strh_;
   return *this;
 }
 
-String::String (char const* source)
-{   
-  assert (source);    
-  strh_ = source;    
+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
 }
 
-String::String (Byte const* byte_l, int length_i)
-{   
-  strh_.set (byte_l, length_i);    
+String
+String::insert (ssize pos, String s)
+{
+  *this = substr (0, pos) + s + substr (pos + 1);
+  return *this;
 }
 
-/**
-  @see
-  String_convert::
- */
-String
-to_str (char c, int n)
+ssize
+String::copy (char *buf, ssize n, ssize pos) const
 {
-  return String_convert::char_str (c, n);
+  assert (pos == 0);
+  memcpy (buf, strh_.to_bytes (), strh_.length () + 1);
+  return n; // ?
 }
 
-String
-to_str (double f, char const* format)
+int
+String::compare (String const &s) const
+{
+  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 String_convert::double_str (f, format);
+  return (char const*) to_bytes ();
 }
 
-String
-to_str (int i, char const * format)
+bool
+String::empty () const
 {
-  return String_convert::int_str (i, format);
+  return !length ();
 }
 
-String
-to_str (bool b)
+int
+String::find (char c, int pos) const
 {
-  return String_convert::bool_str (b);
+  String f = right_string (length () - pos);
+  ssize n = f.index (c);
+  if (n != NPOS)
+    return pos + n;
+  return NPOS;
 }
 
-String 
-to_str (char const* format, ... )
+int
+String::find (char const *c, int pos) const
 {
-  va_list args;
-  va_start (args, format);
-  String str = String_convert::vform_str (format, args);
-  va_end (args);
-  return str;
+  return find (String (c), pos);
+}
+
+int
+String::find (String s, int pos) const
+{
+  if (!pos)
+    return index (s);
+  String f = right_string (length () - pos);
+  ssize n = f.index (s);
+  if (n != NPOS)
+    return pos + n;
+  return NPOS;
+}
+
+int
+String::rfind (char c) const
+{
+  return index_last (c);
+}
+
+String
+String::replace (int pos, int n, String str)
+{
+  return this->substr (0, pos) + str + this->substr (pos + n);
 }
 
-\f
 void
 String::append (String s)
 {
-  strh_.append (s.byte_C (), s.length_i ());
+  strh_.append (s.to_bytes (), s.length ());
 }
+
 void
 String::operator += (String s)
 {
   append (s);
 }
 
-void
-String::prepend (String s)
+int
+String::length () const
 {
-  s += *this;
-  *this = s;
+  return strh_.length ();
 }
 
+
+
+
+/* String */
+
 int
-String::length_i () const
+String::compare (String const &s1, String const &s2)
 {
-  return strh_.length_i ();
+  return s1.compare (s2);
 }
 
-Byte const*
-String::byte_C () const
-{
-  return strh_.byte_C ();
-}
+#ifdef STRING_DEBUG
+void *mymemmove (void *dest, void const *src, size_t n);
+#define memmove mymemmove
+#endif
 
-char const*
-String::ch_C () const
+// return array, alloced with new.
+Byte *
+String::get_copy_byte () const
 {
-  return strh_.ch_C ();
+  Byte const *src = strh_.to_bytes ();
+  Byte *dest = new Byte[strh_.length () + 1];
+  memcpy (dest, src, strh_.length () + 1);
+  return dest;
 }
 
-Byte*
-String::byte_l ()
+char *
+String::get_copy_str0 () const
 {
-  return strh_.byte_l ();
+  return (char *)get_copy_byte ();
 }
 
-char*
-String::ch_l ()
+\f
+
+#if 0
+void
+String::prepend (String s)
 {
-  return strh_.ch_l ();
+  s += *this;
+  *this = s;
 }
 
-bool 
-String::empty_b () const
+#endif
+
+
+Byte const *
+String::to_bytes () const
 {
-  return !length_i ();
+  return strh_.to_bytes ();
 }
-/**
-  Do a signed comparison,  analogous to memcmp;
- */
-int
-String::compare_i (String const& s1, String const& s2) 
-{
-  Byte const* p1 = s1.byte_C ();
-  Byte const* p2 = s2.byte_C ();
-  if (p1 == p2)
-    return 0;
 
-  /*
-    don't forget the terminating '\0'
-   */
-  int f = (s1.length_i () <? s2.length_i ());
-  int cmp_length = 1+ f;
-  int i = memcmp (p1, p2, cmp_length);
-  return i;
+Byte *
+String::get_bytes ()
+{
+  return strh_.get_bytes ();
 }
 
 \f
 int
-String::index_last_i (char const c) const
+String::index_last (char const c) const
 {
-  if (!length_i ()) 
-    return -1;
+  if (!length ())
+    return NPOS;
 
-  char const* me = strh_.ch_C ();
-  char const* p = (char const*)memrchr ( (Byte*)me, length_i (), c);
+  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_i (char const* string) const // UGK!
-{
-  assert (false);              // broken
-  int length = strlen (string); // ugrh
-  if (!length_i () || !length) 
-    return -1;
-  
-  int next_i = index_i (string);
-  if (next_i == -1)
-    return -1;
-  
-  int index_i = 0;
-  while (next_i >= 0) 
-    {
-      index_i += next_i;
-      next_i = right_str (length_i () - index_i - length).index_i (string );
-    }
-  return index_i;
+  return NPOS;
 }
 
 /** find  a character.
 
-  @return
-  the index of the leftmost character #c# (0 <= return < length_i ()),
-  or   -1 if not found. 
+@return
+the index of the leftmost character #c# (0 <= return < length ()),
+or   NPOS if not found.
 
-  ? should return length_i ()?, as in string.left_str (index_i (delimiter))
+? should return length ()?, as in string.left_string (index (delimiter))
 */
 int
-String::index_i (char c) const
+String::index (char c) const
 {
-  char const* me = strh_.ch_C ();
-  char const* p = (char const *) memchr (me,c,  length_i ());
+  char const *me = strh_.c_str ();
+  char const *p = (char const *) memchr (me, c, length ());
   if (p)
     return p - me;
-  return -1;
+  return NPOS;
 }
 
 /**
-  find a substring.
+   find a substring.
 
-  @return
-1  index of leftmost occurrence of #searchfor#
- */
+   @return
+   index of leftmost occurrence of #searchfor#
+*/
 int
-String::index_i (String searchfor) const
+String::index (String searchfor) const
 {
-  char const* me = strh_.ch_C ();
+  char const *me = strh_.c_str ();
+
+  char const *p
+    = (char const *) memmem (me, length (),
+                            searchfor.c_str (), searchfor.length ());
 
-  char const* p =     (char const *) 
-    memmem (me, length_i (), searchfor.ch_C (), searchfor.length_i ());
-  
   if (p)
     return p - me;
-  else
-    return -1;
+
+  return NPOS;
 }
 
 /** find chars of a set.
 
-  @return
-
-  the index of the leftmost occurance of an element of #set#.  -1 if
-  nothing is found.
-
+@return
 
+the index of the leftmost occurance of an element of #set#.  NPOS if
+nothing is found.
 */
 int
-String::index_any_i (String set) const
+String::index_any (String set) const
 {
-  int n = length_i ();
+  int n = length ();
   if (!n)
-    return -1;
+    return NPOS;
 
-  void const * me_l = (void const *) strh_.ch_C ();
-  for (int i=0; i  < set.length_i (); i++) 
+  void const *me = (void const *) strh_.c_str ();
+  for (int i = 0; i < set.length (); i++)
     {
-      char * found= (char*) memchr (me_l, set[i], n );
-      if (found) 
-       {
-         return found - me_l;
-       }
+      char *found = (char *) memchr (me, set[i], n);
+      if (found)
+       return found - (char const *)me;
     }
-  return -1;
+  return NPOS;
 }
 \f
 String
-String::left_str (int n) const
+String::left_string (int n) const
 {
-  if (n >= length_i ())
+  if (n >= length ())
     return *this;
 
-  String retval;       
+  String retval;
   if (n < 1)
     return retval;
-  
+
   retval = *this;
   retval.strh_.trunc (n);
   return retval;
 }
 
 String
-String::right_str (int n) const
+String::right_string (int n) const
 {
-  if (n > length_i ())
+  if (n > length ())
     return *this;
-  
+
   if (n < 1)
     return "";
-  
-  return String (strh_.byte_C () + length_i () - n, n); 
-}
 
+  return String (strh_.c_str () + length () - n, n);
+}
 
 String
-String::nomid_str (int index_i, int n) const
+String::nomid_string (int index_i, int n) const
 {
-  if (index_i < 0) 
+  if (index_i < 0)
     {
       n += index_i;
       index_i = 0;
     }
   if (n <= 0)
     return *this;
-  
-  return
-    left_str (index_i)   +
-    right_str (length_i () - index_i - n) ;
+
+  return left_string (index_i) + right_string (length () - index_i - n);
 }
 
 String
-String::cut_str (int index_i, int n) const
+String::cut_string (int index_i, int n) const
 {
-  if (index_i <0) 
+  if (index_i < 0)
     {
       n += index_i;
-      index_i=0;
+      index_i = 0;
     }
-  
-  if (!length_i () || (index_i < 0) || (index_i >= length_i () ) || (n < 1 ) )
+
+  if (!length () || (index_i < 0) || (index_i >= length ()) || (n < 1))
     return String ();
 
-  if ( (n > length_i ()) ||  (index_i + n > length_i () ) )
-    n = length_i () - index_i;
+  if ((n > length ()) || (index_i + n > length ()))
+    n = length () - index_i;
 
-  return String (byte_C () + index_i, n);
+  return String (c_str () + index_i, n);
 }
 \f
-String
-String::upper_str () const
-{
-  String str = *this;
-  str.to_upper ();
-  return str;
-}
-void
-String::to_upper ()
+
+int
+String::to_int () const
 {
-  char *s = (char*)strh_.byte_l ();
-  strnupr (s ,length_i ());
+  return String_convert::dec2int (*this);
 }
 
-void
-String::to_lower ()
+double
+String::to_double () const
 {
-  char* s = strh_.ch_l ();
-  strnlwr (s,length_i ());    
+  return String_convert::dec2double (*this);
 }
 
+#ifdef STREAM_SUPPORT
+#include <iostream>
 
-String 
-String::lower_str () const
+ostream &
+operator << (ostream &os, String d)
 {
-  String str = *this;
-  str.to_lower ();
-  return str;
+  d.print_on (os);
+  return os;
 }
-String 
-String::reversed_str () const
+
+void
+String::print_on (ostream &os) const
 {
-  String str = *this;
-  strrev (str.byte_l (), str.length_i ());
-  return str;    
+  if (!strh_.is_binary_bo ())
+    os << c_str ();
+  else
+    for (int i = 0; i < length (); i++)
+      os << (Byte) (*this)[ i ];
 }
+#endif
 
-int
-String::value_i () const
+String
+String::substitute (String find, String replace)
 {
-  return String_convert::dec2_i (*this);
+  int n = find.length ();
+  int m = replace.length ();
+  for (ssize i = index (find), j = 0; i != NPOS;
+       i = right_string (length () - j).index (find))
+    {
+      *this = left_string (i + j)
+       + replace
+       + right_string (length () - j - i - n);
+      j += i + m;
+    }
+  return *this;
 }
 
-double
-String::value_f () const
+String
+String::substitute (char find, char replace)
 {
-  return String_convert::dec2_f (*this);
+  for (ssize i = index (find); i != NPOS; i = index (find))
+    (*this)[i] = replace;
+  return *this;
+}
+
 }
 
+#endif /* !STD_STRING */
+