Logo Search packages:      
Sourcecode: afnix version File versions

String.cpp

// ---------------------------------------------------------------------------
// - String.cpp                                                              -
// - standard object library - string class implementation                   -
// ---------------------------------------------------------------------------
// - This program is free software;  you can redistribute it  and/or  modify -
// - it provided that this copyright notice is kept intact.                  -
// -                                                                         -
// - This program  is  distributed in  the hope  that it will be useful, but -
// - without  any  warranty;  without  even   the   implied    warranty   of -
// - merchantability or fitness for a particular purpose.  In no event shall -
// - the copyright holder be liable for any  direct, indirect, incidental or -
// - special damages arising in any way out of the use of this software.     -
// ---------------------------------------------------------------------------
// - copyright (c) 1999-2007 amaury darsch                                   -
// ---------------------------------------------------------------------------

#include "Ascii.hpp"
#include "Input.hpp"
#include "Stdsid.hxx"
#include "String.hpp"
#include "Strvec.hpp"
#include "Vector.hpp"
#include "Buffer.hpp"
#include "Unicode.hpp"
#include "Reactor.hpp"
#include "Monitor.hpp"
#include "Integer.hpp"
#include "Boolean.hpp"
#include "Runnable.hpp"
#include "Character.hpp"
#include "QuarkZone.hpp"
#include "Exception.hpp"
#include "csys.hpp"
#include "cmem.hpp"

namespace afnix {

  // -------------------------------------------------------------------------
  // - memory section                                                        -
  // -------------------------------------------------------------------------

  // the unique reactor for quark mapping
  static Reactor* p_reactor = nilp;
  static Monitor* p_monitor = nilp;

  // the deallocator for the reactor
  static void dealloc_reactor (void) {
    delete p_reactor;
    delete p_monitor;
    p_reactor = nilp;
    p_monitor = nilp;
  }

  // the reactor allocator
  static inline void alloc_reactor (void) {
    if (p_reactor == nilp) {
      p_monitor = new Monitor;
      p_reactor = new Reactor;
      c_gcleanup (dealloc_reactor);
    }
  }
    
  // -------------------------------------------------------------------------
  // - private section                                                       -
  // -------------------------------------------------------------------------
  
  // 32 bit hash function
  static inline long hash_32 (const t_quad* buf) {
    if (buf == nilp) return 0L;
    t_quad chr = nilq;
    long   hvl = 0;
    long   sht = 17;
    while ((chr = *buf++) != nilq) {
      hvl = hvl ^ (((long) chr) << sht);
      if ((sht = sht - 7) < 0) sht += 24;
    }
    return hvl;
  }

  // 64 bit hash function
  static inline long hash_64 (const t_quad* buf) {
    if (buf == nilp) return 0LL;
    t_quad chr = nilq;
    long   hvl = 0;
    long   sht = 47;
    while ((chr = *buf++) != nilq) {
      hvl = hvl ^ (((long) chr) << sht);
      if ((sht = sht - 7) < 0) sht += 56;
    }
    return hvl;
  }

  // -------------------------------------------------------------------------
  // - class section                                                         -
  // -------------------------------------------------------------------------

  // create an empty string
  
00099   String::String (void) {
    p_sval = nilp;
  }
  
  // create a string from a character
  
00105   String::String (const char c) {
    p_sval = Unicode::strmak (c);
  }
  
   // create a string from an unicode character
  
00111   String::String (const t_quad c) {
    p_sval = Unicode::strmak (c);
  }
 
  // create a string from a c-string
  
00117   String::String (const char* s) {
    p_sval = Unicode::strdup (s);
  }

  // create a string from an unicode string
  
00123   String::String (const t_quad* s) {
    p_sval = Unicode::strdup (s);
  }

  // copy constructor
  
00129   String::String (const String& that) {
    that.rdlock ();
    p_sval = Unicode::strdup (that.p_sval);
    that.unlock ();
  }
  
  // destroy this string
  
00137   String::~String (void) {
    delete [] p_sval;
  }

  // return the class name

00143   String String::repr (void) const {
    return "String";
  }

  // return a literal representation of this string
  
00149   String String::toliteral (void) const {
    rdlock ();
    String result = '"';
    result += p_sval;
    result += '"';
    unlock ();
    return result;
  }

  // return a printable representation of this string
  
00160   String String::tostring (void) const {
    rdlock ();
    String result = *this;
    unlock ();
    return result;
  }

  // return a clone of this string

00169   Object* String::clone (void) const {
    return new String (*this);
  }

  // return the string serial code

00175   t_byte String::serialid (void) const {
    return SERIAL_STRG_ID;
  }

  // serialize this string

00181   void String::wrstream (Output& os) const {
    rdlock ();
    try {
      char* sbuf = encode ();
      os.write (sbuf);
      os.write (nilc);
      delete [] sbuf;
      unlock ();
    } catch (...) {
      unlock ();
      throw;
    }
  }

  // deserialize this string

00197   void String::rdstream (Input& is) {
    wrlock ();
    try {
      Buffer buffer;
      char c = nilc;
      while ((c = is.read ()) != nilc) buffer.add (c);
      *this = buffer.tostring ();
      unlock ();
    } catch (...) {
      unlock ();
      throw;
    }
  }

  // return a quark from this string

00213   long String::toquark (void) const {
    alloc_reactor ();
    p_monitor->enter ();
    long result = p_reactor->intern (*this);
    p_monitor->leave ();
    return result;
  }

  // intern a c-string and return a quark

00223   long String::intern (const char* name) {
    return String(name).toquark();
  }

  // remap a string from a quark

00229   String String::qmap (const long quark) {
    alloc_reactor ();
    p_monitor->enter ();
    String result = p_reactor->qmap (quark);
    p_monitor->leave ();
    return result;
  }

  // return true if the string is nil

00239   bool String::isnil (void) const {
    rdlock ();
    bool result = (p_sval == nilp) || (p_sval[0] == nilq);
    unlock ();
    return result;
  }

  // assign a c-string to this string
  
00248   String& String::operator = (const char* s) {
    wrlock ();
    delete [] p_sval;
    p_sval = Unicode::strdup (s);
    unlock ();
    return *this;
  }
  
  // assign a character to this string
  
00258   String& String::operator = (const char c) {
    wrlock ();
    delete [] p_sval;
    p_sval = Unicode::strmak (c);
    unlock ();
    return *this;
  }

  // assign a unicode string to this string
  
00268   String& String::operator = (const t_quad* s) {
    wrlock ();
    delete [] p_sval;
    p_sval = Unicode::strdup (s);
    unlock ();
    return *this;
  }
  
  // assign a unicode character to this string
  
00278   String& String::operator = (const t_quad c) {
    wrlock ();
    delete [] p_sval;
    p_sval = Unicode::strmak (c);
    unlock ();
    return *this;
  }
  
  // assign a string to this string
  
00288   String& String::operator = (const String& that) {
    // protect the case this == that
    if (this == &that) return *this;
    // assign new string
    wrlock ();
    that.rdlock ();
    delete [] p_sval;
    p_sval = Unicode::strdup (that.p_sval);
    that.unlock ();
    unlock ();
    return *this;
  }

  // get a character at a certain position

00303   t_quad String::operator [] (const long index) const {
    rdlock ();
    // check for good position first
    if ((index < 0) || (index >= length ())) {
      unlock ();
      throw Exception ("index-error", "invalid string index");
    }
    // everything is fine
    t_quad result = p_sval[index];
    unlock ();
    return result;
  }

  // add a string to the current one and return a new one
  
00318   String String::operator + (const String& s) const {
    rdlock   ();
    s.rdlock ();
    try {
      // create a temporary buffer to hold the string
      long    size = length () + s.length () + 1;
      t_quad* sbuf = new t_quad[size];
      // copy and concat the string
      Unicode::strcpy (sbuf,   p_sval);
      Unicode::strcat (sbuf, s.p_sval);
      // create a new string and clean buffer
      String result = sbuf;
      delete [] sbuf;
      s.unlock ();
      unlock   ();
      return result;
    } catch (...) {
      s.unlock ();
      unlock   ();
      throw;
    }
  }
  
  // add a character to the current string and return a new one
  
00343   String String::operator + (const char c) const {
    rdlock   ();
    try {
      // create a temporary buffer to hold the string
      long    size = length () + 2;
      t_quad* sbuf = new t_quad[size];
      // copy the string and set character
      Unicode::strcpy (sbuf, p_sval);
      sbuf[size-2] = (t_quad) c;
      sbuf[size-1] = nilq;
      // create a new string and clean buffer
      String result = sbuf;
      delete [] sbuf;
      unlock ();
      return result;
    } catch (...) {
      unlock ();
      throw;
    }
  }

  // add a unicode character to the current string and return a new one
  
00366   String String::operator + (const t_quad c) const {
    rdlock   ();
    try {
      // create a temporary buffer to hold the string
      long    size = length () + 2;
      t_quad* sbuf = new t_quad[size];
      // copy the string and set character
      Unicode::strcpy (sbuf, p_sval);
      sbuf[size-2] = c;
      sbuf[size-1] = nilq;
      // create a new string and clean buffer
      String result = sbuf;
      delete [] sbuf;
      unlock ();
      return result;
    } catch (...) {
      unlock ();
      throw;
    }
  }

  // add an integer to the current string and return a new one
  
00389   String String::operator + (const long value) const {
    rdlock ();
    try {
      // create a temporary buffer to hold the string
      char* cvalue = Ascii::ltoa (value);
      // create a temporary buffer to hold the string
      long    size = length () + Ascii::strlen (cvalue) + 1;
      t_quad* sbuf = new t_quad[size];
      // copy and concat the string
      Unicode::strcpy (sbuf, p_sval);
      Unicode::strcat (sbuf, cvalue);
      // create a new string and clean buffer
      String result = sbuf;
      delete [] sbuf;
      delete [] cvalue;
      unlock ();
      return result;
    } catch (...) {
      unlock ();
      throw;
    }
  }

  // add a long integer to the current string and return a new one
  
00414   String String::operator + (const t_long value) const {
    rdlock ();
    try {
      // create a temporary buffer to hold the string
      char* cvalue = Ascii::lltoa (value);
      // create a temporary buffer to hold the string
      long    size = length () + Ascii::strlen (cvalue) + 1;
      t_quad* sbuf = new t_quad[size];
      // copy and concat the string
      Unicode::strcpy (sbuf, p_sval);
      Unicode::strcat (sbuf, cvalue);
      // create a new string and clean buffer
      String result = sbuf;
      delete [] sbuf;
      delete [] cvalue;
      unlock ();
      return result;
    } catch (...) {
      unlock ();
      throw;
    }
  }
  
  // add a string to this string
  
00439   String& String::operator += (const String& s) {
    wrlock   ();
    s.rdlock ();
    try {
      // create a temporary buffer to hold the string
      long    size = length () + s.length () + 1;
      t_quad* sbuf = new t_quad[size];
      // copy and concat the string
      Unicode::strcpy (sbuf,   p_sval);
      Unicode::strcat (sbuf, s.p_sval);
      // clean the old string and save the buffer
      delete [] p_sval;
      p_sval = sbuf;
      s.unlock ();
      unlock   ();
      return *this;
    } catch (...) {
      s.unlock ();
      unlock   ();
      throw;
    }
  }

  // add a character to the current string
  
00464   String& String::operator += (const char c) {
    wrlock   ();
    try {
      // create a temporary buffer to hold the string
      long    size = length () + 2;
      t_quad* sbuf = new t_quad[size];
      // copy the string and set character
      Unicode::strcpy (sbuf, p_sval);
      sbuf[size - 2] = (t_quad) c;
      sbuf[size - 1] = nilq;
      // clean the old string and save the buffer
      delete [] p_sval;
      p_sval = sbuf;
      unlock ();
      return *this;
    } catch (...) {
      unlock ();
      throw;
    }
  }

  // add a character to the current string
  
00487   String& String::operator += (const t_quad c) {
    wrlock   ();
    try {
      // create a temporary buffer to hold the string
      long    size = length () + 2;
      t_quad* sbuf = new t_quad[size];
      // copy the string and set character
      Unicode::strcpy (sbuf, p_sval);
      sbuf[size - 2] = c;
      sbuf[size - 1] = nilq;
      // clean the old string and save the buffer
      delete [] p_sval;
      p_sval = sbuf;
      unlock ();
      return *this;
    } catch (...) {
      unlock ();
      throw;
    }
  }

  // add an integer to this string
  
00510   String& String::operator += (const long value) {
    wrlock ();
    try {
      // create a temporary buffer to hold the string
      char* cvalue = Ascii::ltoa (value);
      // create a temporary buffer to hold the string
      long    size = length () + Ascii::strlen (cvalue) + 1;
      t_quad* sbuf = new t_quad[size];
      // copy and concat the string
      Unicode::strcpy (sbuf, p_sval);
      Unicode::strcat (sbuf, cvalue);
      // clean the old string and save the buffer
      delete [] p_sval;
      delete [] cvalue;
      p_sval = sbuf;
      unlock ();
      return *this;
    } catch (...) {
      unlock ();
      throw;
    }
  }

  // add a long integer to this string
  
00535   String& String::operator += (const t_long value) {
    rdlock ();
    try {
      // create a temporary buffer to hold the string
      char* cvalue = Ascii::lltoa (value);
      // create a temporary buffer to hold the string
      long    size = length () + Ascii::strlen (cvalue) + 1;
      t_quad* sbuf = new t_quad[size];
      // copy and concat the string
      Unicode::strcpy (sbuf, p_sval);
      Unicode::strcat (sbuf, cvalue);
      // clean the old string and save the buffer
      delete [] p_sval;
      delete [] cvalue;
      p_sval = sbuf;
      unlock ();
      return *this;
    } catch (...) {
      unlock ();
      throw;
    }
  }

  // compare a string with another one
  
00560   bool String::operator == (const String& s) const {
    rdlock   ();
    s.rdlock ();
    bool result = Unicode::strcmp (p_sval, s.p_sval);
    s.unlock ();
    unlock   ();
    return result;
  }

  // compare a string with a c-string
  
00571   bool String::operator == (const char* s) const {
    rdlock   ();
    bool result = Unicode::strcmp (p_sval, s);
    unlock   ();
    return result;
  }
  
  // compare a string with another one
  
00580   bool String::operator != (const String& s) const {
    rdlock   ();
    s.rdlock ();
    bool result = Unicode::strcmp (p_sval, s.p_sval);
    s.unlock ();
    unlock   ();
    return !result;
  }
  
  // compare a string with a c-string
  
00591   bool String::operator != (const char* s) const {
    rdlock   ();
    bool result = Unicode::strcmp (p_sval, s);
    unlock   ();
    return !result;
  }

  // compare a string with another one
  
00600   bool String::operator < (const String& s) const {
    rdlock   ();
    s.rdlock ();
    bool result = Unicode::strlth (p_sval, s.p_sval);
    s.unlock ();
    unlock   ();
    return result;
  }

  // compare a string with a c-string
  
00611   bool String::operator < (const char* s) const {
    rdlock   ();
    bool result = Unicode::strlth (p_sval, s);
    unlock   ();
    return result;
  }

  // compare a string with another one
  
00620   bool String::operator <= (const String& s) const {
    rdlock   ();
    s.rdlock ();
    bool result = Unicode::strleq (p_sval, s.p_sval);
    s.unlock ();
    unlock   ();
    return result;
  }

  // compare a string with a c-string
  
00631   bool String::operator <= (const char* s) const {
    rdlock   ();
    bool result = Unicode::strleq (p_sval, s);
    unlock   ();
    return result;
  }

  // compare a string with another one
  
00640   bool String::operator > (const String& s) const {
    rdlock   ();
    s.rdlock ();
    bool result = Unicode::strleq (p_sval, s.p_sval);
    s.unlock ();
    unlock   ();
    return !result;
  }

  // compare a string with a c-string
  
00651   bool String::operator > (const char* s) const {
    rdlock   ();
    bool result = Unicode::strleq (p_sval, s);
    unlock   ();
    return !result;
  }

  // compare a string with another one
  
00660   bool String::operator >= (const String& s) const {
    rdlock   ();
    s.rdlock ();
    bool result = Unicode::strlth (p_sval, s.p_sval);
    s.unlock ();
    unlock   ();
    return !result;
  }

  // compare a string with a c-string
  
00671   bool String::operator >= (const char* s) const {
    rdlock   ();
    bool result = Unicode::strlth (p_sval, s);
    unlock   ();
    return !result;
  }

  // return the length of this string
  
00680   long String::length (void) const {
    rdlock ();
    long result = Unicode::strlen (p_sval);
    unlock ();
    return result;
  }
  
  // return a c-string representation or nilp for this string

00689   char* String::tochar (void) const {
    rdlock ();
    try {
      char* result = Ascii::strdup (p_sval);
      unlock ();
      return result;
    } catch (...) {
      unlock ();
      throw;
    }
  }
  
  // return an unicode string representation or nilp for this string

00703   t_quad* String::toquad (void) const {
    rdlock ();
    try {
      t_quad* result = Unicode::strdup (p_sval);
      unlock ();
      return result;
    } catch (...) {
      unlock ();
      throw;
    }
  }

  // return the first character

00717   t_quad String::first (void) const {
    rdlock ();
    try {
      t_quad result = isnil () ? nilq : p_sval[0];
      unlock ();
      return result;
    } catch (...) {
      unlock ();
      throw;
    }
  }

  // return the last character

00731   t_quad String::last (void) const {
    rdlock ();
    try {
      long len = length ();
      t_quad result = (len == 0) ? nilq : p_sval[len-1];
      unlock ();
      return result;
    } catch (...) {
      unlock ();
      throw;
    }
  }

  // encode a string in character form

00746   char* String::encode (void) const {
    rdlock ();
    try {
      char* result = Unicode::encode (p_sval);
      unlock ();
      return result;
    } catch (...) {
      unlock ();
      throw;
    }
  }

  // decode a string in character form

00760   void String::decode (const char* s) {
    wrlock ();
    try {
      t_quad* sval = Unicode::decode (s);
      delete [] p_sval;
      p_sval = sval;
      unlock ();
    } catch (...) {
      unlock ();
      throw;
    }
  }

  // remove leading blank from this string
  
00775   String String::stripl (void) const {
    rdlock ();
    try {
      t_quad* buffer = Unicode::rmlead (p_sval);
      String  result = buffer;
      delete [] buffer;
      unlock ();
      return result;
    } catch (...) {
      unlock ();
      throw;
    }
  }
  
  // remove trailing blank from this string
  
00791   String String::stripr (void) const {
    rdlock ();
    try {
      t_quad* buffer = Unicode::rmtrail (p_sval);
      String  result = buffer;
      delete [] buffer;
      unlock ();
      return result;
    } catch (...) {
      unlock ();
      throw;
    }
  }
  
  // remove leading and trailing blank from this string
  
00807   String String::strip (void) const {
    rdlock ();
    try {
      t_quad* lbuffer = Unicode::rmlead  (p_sval);
      t_quad* rbuffer = Unicode::rmtrail (lbuffer);
      String  result  = rbuffer;
      delete [] lbuffer;
      delete [] rbuffer;
      unlock ();
      return result;
    } catch (...) {
      unlock ();
      throw;
    }
  }

  // convert the string to upper case
  
00825   String String::toupper (void) const {
    rdlock ();
    try {
      t_quad* buffer = Unicode::toupper (p_sval);
      String  result = buffer;
      delete [] buffer;
      unlock ();
      return result;
    } catch (...) {
      unlock ();
      throw;
    }
  }

  // convert the string to lower case
  
00841   String String::tolower (void) const {
    rdlock ();
    try {
      t_quad* buffer = Unicode::tolower (p_sval);
      String  result = buffer;
      delete [] buffer;
      unlock ();
      return result;
    } catch (...) {
      unlock ();
      throw;
    }
  }

  // return the hashid for this string
  
00857   long String::hashid (void) const {
    rdlock ();
    long result = (sizeof (long) == 8) ? hash_64 (p_sval) : hash_32 (p_sval);
    unlock ();
    return (result > 0) ? result : -result;
  }

  // return the right substring starting at a certain index

00866   String String::rsubstr (const long index) const {
    rdlock ();
    String result;
    long len = length ();
    if ((len == 0) || (index >= len-1)) {
      unlock ();
      return result;
    }
    t_quad* sptr = p_sval + index;
    result = sptr;
    unlock ();
    return result;
  }

  // return the left substring of a string

00882   String String::lsubstr (const long index) const {
    rdlock ();
    String result;
    long len = length ();
    if ((len == 0) || (index > len)) {
      unlock ();
      return result;
    }
    t_quad* sptr = Unicode::strdup (p_sval);
    sptr[index] = nilq;
    result = sptr;
    delete [] sptr;
    unlock ();
    return result;
  }

  // return a substring of a string

00900   String String::substr (const long lidx, const long ridx) const {
    rdlock ();
    long len = length ();
    if ((lidx >= ridx) || (lidx < 0) || (lidx >= len) || (ridx <0) ||
      (ridx >  len)  || (len == 0)) {
      unlock ();
      throw Exception ("index-error", "invalid index for sub-string");
    }
    // create th result string
    String result;
    t_quad* buf = Unicode::strdup (p_sval);
    t_quad* sub = buf + lidx;
    buf[ridx] = nilq;
    result = sub;
    delete [] buf;
    unlock ();
    return result;
  }

  // fill this string with a character until a given size is reached
  
00921   String String::lfill (const char c, const long size) const {
    rdlock ();
    // do nothing if size exceed
    long len = size - length ();
    if (len <= 0) {
      String result = *this;
      unlock ();
      return result;
    }
    // fill the string first
    String result;
    for (long i = 0; i < len; i++) result = result + c;
    result += *this;
    unlock ();
    return result;
  }

  // fill this string with a character until a given size is reached
  
00940   String String::lfill (const t_quad c, const long size) const {
    rdlock ();
    // do nothing if size exceed
    long len = size - length ();
    if (len <= 0) {
      String result = *this;
      unlock ();
      return result;
    }
    // fill the string first
    String result;
    for (long i = 0; i < len; i++) result = result + c;
    result += *this;
    unlock ();
    return result;
  }

  // fill this string with a character until a given size is reached
  
00959   String String::rfill (const char c, const long size) const {
    rdlock ();
    // do nothing if size exceed
    long len = size - length ();
    if (len <= 0) {
      String result = *this;
      unlock ();
      return result;
    }
    // fill the string first
    String result = *this;
    for (long i = 0; i < len; i++) result = result + c;
    unlock ();
    return result;
  }

  // fill this string with a character until a given size is reached
  
00977   String String::rfill (const t_quad c, const long size) const {
    rdlock ();
    // do nothing if size exceed
    long len = size - length ();
    if (len <= 0) {
      String result = *this;
      unlock ();
      return result;
    }
    // fill the string first
    String result = *this;
    for (long i = 0; i < len; i++) result = result + c;
    unlock ();
    return result;
  }

  // split a string with blank and tab

00995   Vector* String::split (void) const {
    rdlock ();
    try {
     Vector* result = split ("");
     unlock ();
     return result;
    } catch (...) {
      unlock ();
      throw;
    }
  }

  // split a string according to a string break

01009   Vector* String::split (const String& sbrk) const {
    rdlock ();
    Vector* result = new Vector;
    try {
      Strvec vec = Strvec::split (*this, sbrk);
      long len = vec.length ();
      for (long i = 0; i < len; i++) {
      String data = vec.get (i);
      result->append (new String (data));
      }
    } catch (...) {
      delete result;
      unlock ();
      throw;
    }
    return result;
  }

  // accumulate strings between a control character

01029   Vector* String::extract (const t_quad cbrk) const {
    rdlock ();
    Vector* result = new Vector;
    long len = length ();
    for (long i = 0; i < len; i++) {
      t_quad c = p_sval[i];
      if (c == cbrk) {
      i++;
      String buf;
      while ((c = p_sval[i]) != cbrk) {
        buf += c ;
        i++;
        if (i == len) {
          delete result;
          unlock ();
          throw Exception ("extract-error", "unterminated string", *this);
        }
      }
      result->append (new String (buf));
      }
    }
    unlock ();
    return result;
  }

  // -------------------------------------------------------------------------
  // - object section                                                        -
  // -------------------------------------------------------------------------

  // the quark zone
  static const long QUARK_ZONE_LENGTH = 25;
  static QuarkZone  zone (QUARK_ZONE_LENGTH);

  // the supported quark for this string class
  static const long QUARK_ADD       = zone.intern ("+");
  static const long QUARK_EQL       = zone.intern ("==");
  static const long QUARK_NEQ       = zone.intern ("!=");
  static const long QUARK_LTH       = zone.intern ("<");
  static const long QUARK_LEQ       = zone.intern ("<=");
  static const long QUARK_GTH       = zone.intern (">");
  static const long QUARK_GEQ       = zone.intern (">=");
  static const long QUARK_AEQ       = zone.intern ("+=");
  static const long QUARK_GET       = zone.intern ("get");
  static const long QUARK_NILP      = zone.intern ("nil-p");
  static const long QUARK_SPLIT     = zone.intern ("split");
  static const long QUARK_STRIP     = zone.intern ("strip");
  static const long QUARK_LENGTH    = zone.intern ("length");
  static const long QUARK_HASHID    = zone.intern ("hashid");
  static const long QUARK_STRIPL    = zone.intern ("strip-left");
  static const long QUARK_STRIPR    = zone.intern ("strip-right");
  static const long QUARK_SUBSTR    = zone.intern ("substr");
  static const long QUARK_EXTRACT   = zone.intern ("extract");
  static const long QUARK_TOUPPER   = zone.intern ("to-upper");
  static const long QUARK_TOLOWER   = zone.intern ("to-lower");
  static const long QUARK_SUBLEFT   = zone.intern ("sub-left");
  static const long QUARK_SUBRIGHT  = zone.intern ("sub-right");
  static const long QUARK_FILLLEFT  = zone.intern ("fill-left");
  static const long QUARK_FILLRIGHT = zone.intern ("fill-right");

  // create a new object in a generic way

01090   Object* String::mknew (Vector* argv) {
    if ((argv == nilp) || (argv->length () == 0)) return new String;
    if (argv->length () != 1) 
      throw Exception ("argument-error", 
                   "too many argument with string constructor");
    // try to map the string argument
    Object* obj = argv->get (0);
    if (obj == nilp) return new String;

    // try a literal object
    Literal* lval = dynamic_cast <Literal*> (obj);
    if (lval != nilp) return new String (lval->tostring ());

    // illegal object
    throw Exception ("type-error", "illegal object with string constructor",
                 obj->repr ());
  }

  // return true if the given quark is defined

01110   bool String::isquark (const long quark, const bool hflg) const {
    rdlock ();
    if (zone.exists (quark) == true) {
      unlock ();
      return true;
    }
    bool result = hflg ? Literal::isquark (quark, hflg) : false;
    unlock ();
    return result;
  }

  // operate this object with another object

01123   Object* String::oper (t_oper type, Object* object) {
    Literal* lobj = dynamic_cast <Literal*> (object);
    String*  sobj = dynamic_cast <String*>    (object);

    switch (type) {
    case Object::ADD:
      if (lobj != nilp) return new String (*this + lobj->tostring ());
      break;  
    case Object::EQL:
      if (sobj != nilp) return new Boolean (*this == *sobj);
      break;
    case Object::NEQ:
      if (sobj != nilp) return new Boolean (*this != *sobj);
      break;
    case Object::LTH:
      if (sobj != nilp) return new Boolean (*this < *sobj);
      break;
    case Object::LEQ:
      if (sobj != nilp) return new Boolean (*this <= *sobj);
      break;
    case Object::GTH:
      if (sobj != nilp) return new Boolean (*this > *sobj);
      break;
    case Object::GEQ:
      if (sobj != nilp) return new Boolean (*this >= *sobj);
      break;
    default:
      throw Exception ("operator-error", "unsupported string operator");
    }
    throw Exception ("type-error", "invalid operand with string",
                 Object::repr (object));
  }

  // set an object to this object

01158   Object* String::vdef (Runnable* robj, Nameset* nset, Object* object) {
    Literal* lobj = dynamic_cast <Literal*> (object);
    if (lobj != nilp) {
      *this = lobj->tostring ();
      return this;
    }
    throw Exception ("type-error", "invalid object with string vdef",
                 Object::repr (object));
  }

  // apply this object with a set of arguments and a quark

01170   Object* String::apply (Runnable* robj, Nameset* nset, long quark,
                   Vector* argv) {
    // get the arguments length
    long argc = (argv == nilp) ? 0 : argv->length ();

    // dispatch 0 argument
    if (argc == 0) {
      if (quark == QUARK_NILP)    return new Boolean (isnil   ());
      if (quark == QUARK_LENGTH)  return new Integer (length  ());
      if (quark == QUARK_STRIPL)  return new String  (stripl  ());
      if (quark == QUARK_STRIPR)  return new String  (stripr  ());
      if (quark == QUARK_STRIP)   return new String  (strip   ());
      if (quark == QUARK_TOUPPER) return new String  (toupper ());
      if (quark == QUARK_TOLOWER) return new String  (tolower ());
      if (quark == QUARK_HASHID)  return new Integer (hashid  ());
      if (quark == QUARK_SPLIT) {
      Object* result = split ();
      robj->post (result);
      return result;
      }
    }
    // dispatch 1 argument
    if (argc == 1) {
      if (quark == QUARK_ADD)   return oper (Object::ADD, argv->get (0));
      if (quark == QUARK_EQL)   return oper (Object::EQL, argv->get (0));
      if (quark == QUARK_NEQ)   return oper (Object::NEQ, argv->get (0));
      if (quark == QUARK_SPLIT) {
      Object* result = split (argv->getstring (0));
      robj->post (result);
      return result;
      }
      if (quark == QUARK_AEQ) {
      Object*   obj = argv->get (0);
      Literal* lobj = dynamic_cast <Literal*> (obj);
      if (lobj == nilp) {
        throw Exception ("type-error", "invalid object with operator +=",
                     Object::repr (obj));
      }
      String val = lobj->tostring ();
      *this = *this + val;
      robj->post (this);
      return this;
      }
      if (quark == QUARK_GET) {
      t_long val = argv->getint (0);
      char c = (*this)[val];
      return new Character (c);
      }
      if (quark == QUARK_EXTRACT) {
      t_quad cbrk = argv->getchar (0);
      Object* result = extract (cbrk);
      robj->post (result);
      return result;
      }
      if (quark == QUARK_SUBRIGHT) {
      t_long val = argv->getint (0);
      String result = rsubstr (val);
      return new String (result);
      }
      if (quark == QUARK_SUBLEFT) {
      t_long val = argv->getint (0);
      String result = lsubstr (val);
      return new String (result);
      }
    }
    // dispatch 2 arguments
    if (argc == 2) {
      if (quark == QUARK_FILLLEFT) {
      t_quad fill   = argv->getchar (0);
      t_long size   = argv->getint  (1);
      String result = lfill (fill, size);
      return new String (result);
      }
      if (quark == QUARK_FILLRIGHT) {
      t_quad fill   = argv->getchar (0);
      t_long size   = argv->getint  (1);
      String result = rfill (fill, size);
      return new String (result);
      }
      if (quark == QUARK_SUBSTR) {
      t_long lidx   = argv->getint (0);
      t_long ridx   = argv->getint (1);
      String result = substr (lidx, ridx);
      return new String (result);
      }
    }
    // call the literal method
    return Literal::apply (robj, nset, quark, argv);
  }
}

Generated by  Doxygen 1.6.0   Back to index