]> git.donarmstrong.com Git - lilypond.git/blobdiff - flower/string.cc
* The grand 2005-2006 replace.
[lilypond.git] / flower / string.cc
index e4191300fe5500c283414adb0a74b079d757b18b..4d7fd9019bc096bafb9030334b934743293c158d 100644 (file)
@@ -1,10 +1,8 @@
 /*
+  string.cc - implement String
 
- string.cc - implement String
-  (c) 1997--2004 Han-Wen Nienhuys & Jan Nieuwenhuizen
-
- */
+  (c) 1997--2006 Han-Wen Nienhuys & Jan Nieuwenhuizen
+*/
 
 #ifndef _GNU_SOURCE // we want memmem
 #define _GNU_SOURCE
 #include <cstring>
 
 #include <iostream>
+using namespace std;
 
 #include "libc-extension.hh"
 #include "string-convert.hh"
 
 #ifdef STRING_DEBUG
-void* mymemmove (void* dest, void const* src, size_t n);
+void *mymemmove (void *dest, void const *src, size_t n);
 #define memmove mymemmove
 #endif
 
 // return array, alloced with new.
-Byte*
+Byte *
 String::get_copy_byte () const
 {
-  Byte constsrc = strh_.to_bytes ();
-  Bytedest = new Byte[strh_.length () + 1];
+  Byte const *src = strh_.to_bytes ();
+  Byte *dest = new Byte[strh_.length () + 1];
   memcpy (dest, src, strh_.length () + 1);
-  return dest;    
+  return dest;
 }
 
-char*
+char *
 String::get_copy_str0 () const
 {
-  return (char*)get_copy_byte ();
+  return (char *)get_copy_byte ();
 }
 
 \f
 /*
   copying, constructing.
- */
-String&
-String::operator = (String const&source)
+*/
+String &
+String::operator = (String const &source)
 {
   strh_ = source.strh_;
   return *this;
 }
 
-
-String::String (Byte const* byte, int len_i)
-{   
+String::String (Byte const *byte, int len_i)
+{
   strh_.set (byte, len_i);
 }
 
 /**
-  @see
-  String_convert::
- */
+   @see
+   String_convert::
+*/
 String
 to_string (char c, int n)
 {
@@ -71,13 +69,13 @@ to_string (char c, int n)
 }
 
 String
-to_string (double f, char constformat)
+to_string (double f, char const *format)
 {
   return String_convert::double_string (f, format);
 }
 
 String
-to_string (int i, char const * format)
+to_string (int i, char const *format)
 {
   return String_convert::int_string (i, format);
 }
@@ -93,8 +91,8 @@ to_string (long b)
   return String_convert::long_string (b);
 }
 
-String 
-to_string (char const* format, ... )
+String
+to_string (char const *format, ...)
 {
   va_list args;
   va_start (args, format);
@@ -128,50 +126,44 @@ String::length () const
   return strh_.length ();
 }
 
-Byte const*
+Byte const *
 String::to_bytes () const
 {
   return strh_.to_bytes ();
 }
 
-char const*
-String::to_str0 () const
-{
-  return strh_.to_str0 ();
-}
-
-Byte*
+Byte *
 String::get_bytes ()
 {
   return strh_.get_bytes ();
 }
 
-char*
+char *
 String::get_str0 ()
 {
   return strh_.get_str0 ();
 }
 
-bool 
+bool
 String::is_empty () const
 {
   return !length ();
 }
 /**
-  Do a signed comparison,  analogous to memcmp;
- */
+   Do a signed comparison,  analogous to memcmp;
+*/
 int
-String::compare (String const& s1, String const& s2) 
+String::compare (String const &s1, String const &s2)
 {
-  Byte constp1 = s1.to_bytes ();
-  Byte constp2 = s2.to_bytes ();
+  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 = (s1.length () <? s2.length ());
+  */
+  int f = min (s1.length (), s2.length ());
   int cmp_length = 1+ f;
   int i = memcmp (p1, p2, cmp_length);
   return i;
@@ -181,83 +173,82 @@ String::compare (String const& s1, String const& s2)
 int
 String::index_last (char const c) const
 {
-  if (!length ()) 
+  if (!length ())
     return -1;
 
-  char constme = strh_.to_str0 ();
-  char const* p = (char const*)memrchr ((Byte*)me, length (), c);
+  char const *me = strh_.to_str0 ();
+  char const *p = (char const *)memrchr ((Byte *)me, length (), c);
   if (p)
     return p - me;
   return -1;
 }
 
 int
-String::index_last (char conststring) const // UGK!
+String::index_last (char const *string) const // UGK!
 {
   assert (false);              // broken
   int len = strlen (string); // ugrh
-  if (!length () || !len) 
+  if (!length () || !len)
     return -1;
-  
+
   int next_i = index (string);
   if (next_i == -1)
     return -1;
-  
+
   int index_i = 0;
-  while (next_i >= 0) 
+  while (next_i >= 0)
     {
       index_i += next_i;
-      next_i = right_string (length () - index_i - len).index (string );
+      next_i = right_string (length () - index_i - len).index (string);
     }
   return index_i;
 }
 
 /** find  a character.
 
-  @return
-  the index of the leftmost character #c# (0 <= return < length ()),
-  or   -1 if not found. 
+@return
+the index of the leftmost character #c# (0 <= return < length ()),
+or   -1 if not found.
 
-  ? should return length ()?, as in string.left_string (index (delimiter))
+? should return length ()?, as in string.left_string (index (delimiter))
 */
 int
 String::index (char c) const
 {
-  char constme = strh_.to_str0 ();
-  char const* p = (char const *) memchr (me,c,  length ());
+  char const *me = strh_.to_str0 ();
+  char const *p = (char const *) memchr (me, c, length ());
   if (p)
     return p - me;
   return -1;
 }
 
 /**
-  find a substring.
+   find a substring.
 
-  @return
-1  index of leftmost occurrence of #searchfor#
- */
+   @return
+   index of leftmost occurrence of #searchfor#
+*/
 int
 String::index (String searchfor) const
 {
-  char const* me = strh_.to_str0 ();
+  char const *me = strh_.to_str0 ();
+
+  char const *p
+    = (char const *) memmem (me, length (),
+                            searchfor.to_str0 (), searchfor.length ());
 
-  char const* p = (char const *) 
-    memmem (me, length (), searchfor.to_str0 (), searchfor.length ());
-  
   if (p)
     return p - me;
-  else
-    return -1;
+
+  return -1;
 }
 
 /** 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#.  -1 if
+nothing is found.
 */
 int
 String::index_any (String set) const
@@ -266,14 +257,12 @@ String::index_any (String set) const
   if (!n)
     return -1;
 
-  void const * me = (void const *) strh_.to_str0 ();
-  for (int i=0; i  < set.length (); i++) 
+  void const *me = (void const *) strh_.to_str0 ();
+  for (int i = 0; i < set.length (); i++)
     {
-      char * found= (char*) memchr (me, set[i], n );
-      if (found) 
-       {
-         return found - (char const*)me;
-       }
+      char *found = (char *) memchr (me, set[i], n);
+      if (found)
+       return found - (char const *)me;
     }
   return -1;
 }
@@ -284,10 +273,10 @@ String::left_string (int n) const
   if (n >= length ())
     return *this;
 
-  String retval;       
+  String retval;
   if (n < 1)
     return retval;
-  
+
   retval = *this;
   retval.strh_.trunc (n);
   return retval;
@@ -298,83 +287,62 @@ String::right_string (int n) const
 {
   if (n > length ())
     return *this;
-  
+
   if (n < 1)
     return "";
-  
-  return String (strh_.to_bytes () + length () - n, n); 
-}
 
+  return String (strh_.to_bytes () + length () - n, n);
+}
 
 String
 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_string (index_i)   +
-    right_string (length () - index_i - n) ;
+
+  return left_string (index_i) + right_string (length () - index_i - n);
 }
 
 String
 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 () || (index_i < 0) || (index_i >= length () ) || (n < 1 ) )
+
+  if (!length () || (index_i < 0) || (index_i >= length ()) || (n < 1))
     return String ();
 
-  if ((n > length ()) || (index_i + n > length () ) )
+  if ((n > length ()) || (index_i + n > length ()))
     n = length () - index_i;
 
   return String (to_bytes () + index_i, n);
 }
 \f
-String
-String::upper_string () const
-{
-  String str = *this;
-  str.to_upper ();
-  return str;
-}
+
 void
 String::to_upper ()
 {
-  char *s = (char*)strh_.get_bytes ();
-  strnupr (s ,length ());
+  strnupr (get_str0 (), length ());
 }
 
 void
 String::to_lower ()
 {
-  char* s = strh_.get_str0 ();
-  strnlwr (s,length ());    
+  strnlwr (get_str0 (), length ());
 }
 
-
-String 
-String::lower_string () const
-{
-  String str = *this;
-  str.to_lower ();
-  return str;
-}
-String 
-String::reversed_string () const
+void
+String::reverse ()
 {
-  String str = *this;
-  strrev (str.get_bytes (), str.length ());
-  return str;    
+  memrev (get_bytes (), length ());
 }
 
 int
@@ -391,15 +359,14 @@ String::to_double () const
 
 #ifdef STREAM_SUPPORT
 ostream &
-operator << (ostreamos, String d)
+operator << (ostream &os, String d)
 {
   d.print_on (os);
   return os;
 }
 
-
 void
-String::print_on (ostreamos) const
+String::print_on (ostream &os) const
 {
   if (!strh_.is_binary_bo ())
     os << to_str0 ();
@@ -408,3 +375,27 @@ String::print_on (ostream& os) const
       os << (Byte) (*this)[ i ];
 }
 #endif
+
+String
+String::substitute (String find, String replace)
+{
+  int n = find.length ();
+  int m = replace.length ();
+  for (int i = index (find), j = 0; i > -1;
+       i = right_string (length () - j).index (find))
+    {
+      *this = left_string (i + j)
+       + replace
+       + right_string (length () - j - i - n);
+      j += i + m;
+    }
+  return *this;
+}
+
+String
+String::substitute (char find, char replace)
+{
+  for (int i = index (find); i > - 1; i = index (find))
+    (*this)[i] = replace;
+  return *this;
+}