]> git.donarmstrong.com Git - lilypond.git/blobdiff - flower/string.cc
* lily/grob.cc (Grob): look properties up directly.
[lilypond.git] / flower / string.cc
index 3eaa5c1b099b5f10ef2447a8c6dabbcd1353105e..8f34cf44adc5e7cb51a14e8c4a59b3df0c7d0361 100644 (file)
 /*
+  string.cc - implement String
 
- string.cc - implement String
- (c) 1997 Han-Wen Nienhuys & Jan Nieuwenhuizen
+  (c) 1997--2005 Han-Wen Nienhuys & Jan Nieuwenhuizen
+*/
 
- */
+#ifndef _GNU_SOURCE // we want memmem
+#define _GNU_SOURCE
+#endif
 
-#include <stdlib.h>
-#include <stdio.h>
+#include "string.hh"
 
-#include <assert.h>
-#include <string.h>
+#include <cstdlib>
+#include <cstdio>
+#include <cassert>
+#include <cstring>
+
+#include <iostream>
 
-#include "string.hh"
 #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*
-String::copy_byte_p() const
+Byte *
+String::get_copy_byte () const
 {
-    Byte const* src = strh_.byte_c_l();
-    Byte* dest = new Byte[strh_.length_i() + 1];
-    memcpy( dest, src, strh_.length_i() + 1 );
-    return dest;    
+  Byte const *src = strh_.to_bytes ();
+  Byte *dest = new Byte[strh_.length () + 1];
+  memcpy (dest, src, strh_.length () + 1);
+  return dest;
 }
-void
-String::print_on(ostream& os) const
+
+char *
+String::get_copy_str0 () const
 {
-    if (!strh_.is_binary_bo())
-        os << ch_c_l();
-    else
-       for ( int i = 0; i < length_i(); i++ )
-           os << (Byte)(*this)[ i ];
+  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;
+  strh_ = source.strh_;
+  return *this;
 }
 
-
-String::String(Rational r)
+String::String (Byte const *byte, int len_i)
 {
-    *this = String_convert::rational_str(r);
+  strh_.set (byte, len_i);
 }
 
-String::String (double f, char const* fmt)
+/**
+   @see
+   String_convert::
+*/
+String
+to_string (char c, int n)
 {
-    *this= String_convert::double_str(f,fmt);
+  return String_convert::char_string (c, n);
 }
 
-String::String( char c,  int n )
+String
+to_string (double f, char const *format)
 {
-    *this = String_convert::char_str (c,n);
+  return String_convert::double_string (f, format);
 }
 
-/**
-  @see
-  String_convert::int_str
- */
-String::String(int i, char const * format )
+String
+to_string (int i, char const *format)
 {
-    *this = String_convert::int_str(i,format);
+  return String_convert::int_string (i, format);
 }
 
-String::String (bool b)
+String
+to_string (bool b)
 {
-    *this = (char const* ) (b ? "true" : "false");
+  return String_convert::bool_string (b);
 }
-
-String::String( char const* source )
-{   
-    assert(source);    
-    strh_ = source;    
+String
+to_string (long b)
+{
+  return String_convert::long_string (b);
 }
 
-String::String( Byte const* byte_l, int length_i )
-{   
-    strh_.set( byte_l, length_i );    
+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)
+String::append (String s)
 {
-    strh_.append( s.byte_c_l(), s.length_i() );
+  strh_.append (s.to_bytes (), s.length ());
 }
 void
-String::operator +=(String s)
+String::operator += (String s)
 {
-    append(s);
+  append (s);
 }
 
 void
-String::prepend(String s)
+String::prepend (String s)
 {
-    s += *this;
-    *this = s;
+  s += *this;
+  *this = s;
 }
 
 int
-String::length_i() const
+String::length () const
 {
-    return strh_.length_i();
+  return strh_.length ();
 }
 
-Byte const*
-String::byte_c_l() const
+Byte const *
+String::to_bytes () const
 {
-    return strh_.byte_c_l();
+  return strh_.to_bytes ();
 }
 
-char const*
-String::ch_c_l() const
+
+
+Byte *
+String::get_bytes ()
 {
-    return strh_.ch_c_l();
+  return strh_.get_bytes ();
 }
 
-Byte*
-String::byte_l()
+char *
+String::get_str0 ()
 {
-    return strh_.byte_l();
+  return strh_.get_str0 ();
 }
 
-char*
-String::ch_l()
+bool
+String::is_empty () const
 {
-    return strh_.ch_l();
+  return !length ();
 }
-
 /**
-  Do a signed comparison,  analogous to memcmp;
- */
+   Do a signed comparison,  analogous to memcmp;
+*/
 int
-String::compare_i(String const& s1, String const& s2 ) 
+String::compare (String const &s1, String const &s2)
 {
-    Byte const* p1 = s1.byte_c_l();
-    Byte const* p2 = s2.byte_c_l();
-    if ( p1 == p2 )
-       return 0;
-
-    int i1 = s1.length_i();
-    int i2 = s2.length_i();
+  Byte const *p1 = s1.to_bytes ();
+  Byte const *p2 = s2.to_bytes ();
+  if (p1 == p2)
+    return 0;
 
-    int result=  memcmp( p1, p2, i1 <? i2 );
-    return result ? result : i1-i2;
+  /*
+    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_i( char const c ) const
+String::index_last (char const c) const
 {
-    if ( !length_i() ) 
-       return -1;
-
-    char const* me = strh_.ch_c_l();
-    char const* p = memrchr(me, length_i(), c );
-    if ( p )
-       return p - me;
+  if (!length ())
     return -1;
+
+  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_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 );
+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 index_i;
 }
 
 /** 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   -1 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_l();
-    char const* p = (char const *) memchr( me,c,  length_i());
-    if ( p )
-       return p - me;
-    return -1;
+  char const *me = strh_.to_str0 ();
+  char const *p = (char const *) memchr (me, c, length ());
+  if (p)
+    return p - me;
+  return -1;
 }
 
 /**
-  find the substring.
+   find a substring.
 
-  @return
-  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_l();
-    char const* p = (char const *) memmem(
-       me, length_i(), searchfor.ch_c_l(), searchfor.length_i());
-    
-    if ( p )
-       return p - me;
-    else
-       return -1;
+  char const *me = strh_.to_str0 ();
+
+  char const *p = (char const *)
+    memmem (me, length (), searchfor.to_str0 (), searchfor.length ());
+
+  if (p)
+    return p - me;
+
+  return -1;
 }
 
 /** find chars of a set.
 
-  @return
-  the index of the leftmost occurance of an element of #set#
-  */
+@return
+
+the index of the leftmost occurance of an element of #set#.  -1 if
+nothing is found.
+*/
 int
-String::index_any_i( String set ) const
+String::index_any (String set) const
 {
-    int n = length_i();
-    if ( !n )
-       return -1;
+  int n = length ();
+  if (!n)
+    return -1;
 
-    void const * me_l = (void const *) strh_.ch_c_l();
-    for (int i=0; i  < set.length_i(); i++) {
-       char * found=(char*) memchr(me_l, set[i], n  );
-       if (found) {
-           return found - me_l;
+  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;
        }
     }
-    return -1;
+  return -1;
 }
 \f
 String
-String::left_str( int n ) const
+String::left_string (int n) const
 {
-    if (n >= length_i())
-       return *this;
+  if (n >= length ())
+    return *this;
 
-    String retval;     
-    if (n < 1)
-        return retval;
-    
-    retval = *this;
-    retval.strh_.trunc(n);
+  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())
-       return *this;
-    
-    if ( n < 1)
-        return "";
-    
-    return String( strh_.byte_c_l() + length_i() - n, n ); 
-}
+  if (n > length ())
+    return *this;
 
+  if (n < 1)
+    return "";
+
+  return String (strh_.to_bytes () + 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 ) {
-       n += index_i;
-       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 ) ;
+  if (n <= 0)
+    return *this;
+
+  return left_string (index_i) + right_string (length () - index_i - n);
 }
 
-/*
-  proposal: change to "cut()"
- */
 String
-String::mid_str( int index_i, int n ) const
+String::cut_string (int index_i, int n) const
 {
-    if (index_i <0) {
-       n += index_i;
-       index_i=0;
+  if (index_i < 0)
+    {
+      n += index_i;
+      index_i = 0;
     }
-    
-    if ( !length_i() || ( index_i < 0 ) || ( index_i >= length_i() ) || ( n < 1 ) )
-       return String();
 
-    if ( ( n > length_i() ) ||  ( index_i + n > length_i() ) )
-       n = length_i() - index_i;
+  if (!length () || (index_i < 0) || (index_i >= length ()) || (n < 1))
+    return String ();
+
+  if ((n > length ()) || (index_i + n > length ()))
+    n = length () - index_i;
 
-    return String( byte_c_l() + index_i, n );
+  return String (to_bytes () + index_i, n);
 }
 \f
-String
-String::upper_str() const
+
+void
+String::to_upper ()
 {
-    String str = *this;
-    str.to_upper();
-    return str;
+  strnupr (get_str0 (), length ());
 }
+
 void
-String::to_upper()
+String::to_lower ()
 {
-    char *s = (char*)strh_.byte_l();
-    strnupr( s ,length_i());
+  strnlwr (get_str0 (), length ());
 }
 
 void
-String::to_lower()
+String::reverse ()
 {
-    char* s = strh_.ch_l();
-    strnlwr(s,length_i());    
+  memrev (get_bytes (), length ());
 }
 
-
-String 
-String::lower_str() const
+int
+String::to_int () const
 {
-    String str = *this;
-    str.to_lower();
-    return str;
+  return String_convert::dec2int (*this);
 }
-String 
-String::reversed_str() const
+
+double
+String::to_double () const
 {
-    String str = *this;
-    strrev( str.byte_l(), str.length_i() );
-    return str;    
+  return String_convert::dec2double (*this);
 }
 
-int
-String::value_i() const
+#ifdef STREAM_SUPPORT
+ostream &
+operator << (ostream &os, String d)
 {
-    return String_convert::dec2_i( *this );
+  d.print_on (os);
+  return os;
 }
 
-double
-String::value_f() const
+void
+String::print_on (ostream &os) const
 {
-    return String_convert::dec2_f( *this );
+  if (!strh_.is_binary_bo ())
+    os << to_str0 ();
+  else
+    for (int i = 0; i < length (); i++)
+      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;
+}