string.h

来自「开放源码的编译器open watcom 1.6.0版的源代码」· C头文件 代码 · 共 549 行 · 第 1/2 页

H
549
字号


#ifndef _string_h_
#define _string_h_





/*
 *
 *          Copyright (C) 1994, M. A. Sridhar
 *  
 *
 *     This software is Copyright M. A. Sridhar, 1994. You are free
 *     to copy, modify or distribute this software  as you see fit,
 *     and to use  it  for  any  purpose, provided   this copyright
 *     notice and the following   disclaimer are included  with all
 *     copies.
 *
 *                        DISCLAIMER
 *
 *     The author makes no warranties, either expressed or implied,
 *     with respect  to  this  software, its  quality, performance,
 *     merchantability, or fitness for any particular purpose. This
 *     software is distributed  AS IS.  The  user of this  software
 *     assumes all risks  as to its quality  and performance. In no
 *     event shall the author be liable for any direct, indirect or
 *     consequential damages, even if the  author has been  advised
 *     as to the possibility of such damages.
 *
 */



// This class provides the basic functionality for manipulating a
// printable string of characters.



#include "base/object.h"

#ifdef __GNUC__
#pragma interface
#endif


class CL_StringSequence;
class CL_Substring;
class CL_StringSplitter;

class __CLASSTYPE CL_String: public CL_Object {

public:
    //
    // ----------------------------- Constructors -------------------------
    //

    CL_String ();
    // Default: construct the null string.

    CL_String (const char* strg);
    // Construct a string from the given null-terminated char array.

    CL_String (long value, short minwidth = 1, char padChar = ' ');
    // Construct a string containing the digits of 'value', in a field
    // at least minwidth characters wide. Pad, if necessary, with as many of
    // the given padding characters as necessary on the left.

    CL_String (short value, short minwidth = 1, char padChar = ' ');
    // Construct a string containing the digits of 'value', in a field
    // at least minwidth characters wide. Pad, if necessary, with as many of
    // the given padding characters as necessary on the left.


    CL_String (CL_ByteArray& b);
    // Copy b's contents and append a null byte.
    
    CL_String (char c, short count);
    // Produce a string with c  repeated count times.
                
    CL_String (const CL_String& strg);
    // Copy constructor.
    
    
    ~CL_String();
    // Destructor.

    //
    // -------------------------- Conversion -------------------------------
    //

    operator const char* () const;
    // Return pointer to null-terminated char array representation.
    // The caller of this method should not modify the returned char
    // array.

    const char* AsPtr() const;
    // Synonym for {\tt operator const char*}.

    long AsLong() const;
    // Find the longest prefix that is all digits, and return them as
    // a long value.

    double AsDouble () const;
    // Find our longest prefix that looks like a double number, and
    // return it as a double value.
    

    // ----------- Structural manipulations, substrings ------------------

    long Size() const {return _size;} ;
    // Return the size of the string.

    long Length() const { return _size; };
    // Return the size of the string.
    
    char operator[] (long index) const;
    // Return character at given index.


    CL_Substring operator() (long position, long size);
    // Return the substring beginning at the given position and of the
    // given size. Note that this is not a const method, because the
    // returned substring can be assigned to.

    CL_Substring Suffix (long position);
    // Return our suffix beginning at the given position. Note that this
    // is not a const method, because the returned substring can be
    // assigned to.

    // -------------------- Search-related methods ---------------------


    // --- Character searches --
    
    long CharIndex (char c, long pos = 0L) const;
    // Return the index of the first position, at or right of 'pos', that
    // contains the character c. Return -1 if no such position.

    long NCharIndex (char c, long pos = 0L) const;
    // Return the index of the first position, at or right of 'pos', that
    // does not contain the character c. Return -1 if no such position.

         
    long CharIndex (const char* s, long pos = 0L) const;
    // Return the index of the first position, at or right of 'pos', that
    // contains any one of the characters in s. Return -1 if no such position.

    long NCharIndex (const char* s, long pos = 0L) const;
    // Return the index of the first position, at or right of 'pos', that
    // contains none of the characters in s. Return -1 if no such position.

    
    // ---- String searches ---

    
    bool IsPrefixOf (const char* p) const;
    // Are we a prefix of the given string?

    long Index (const char* s, long pos = 0, long n = 1) const;
    // Return the left end of the n-th occurrence of the string s in
    // our string; begin the search for s at position pos. Return -1 if 
    // no more than n-1 such occurrences of s were found.

    long Replace (const char* s1, const char* s2, long n = 1, long pos = 0);
    // Scan our string beginning at position pos, and replace the
    // first n occurrences of s1 in our string with s2. 
    // Return the number of successful replacements that occurred.
    // Note that if s2 is NULL or points to a null string, the
    // occurrences of s1 are removed. If s1 is null, no replacements
    // will be made.
    //    This method returns 0 if memory allocation fails.

    long ReplaceAll (const char* s1, const char* s2, long pos = 0);
    // Beginning at position pos, replace all occurrences of s1 with
    // s2, as in Replace. Return the number of replacements that took
    // place.

    

    // ------------------------ Decomposition ----------------------------
    //
    
    CL_String Field (long n, const char field_seps[] = " \t") const;
    // View our string as being made up of fields, separated by one or
    // more characters from the null-terminated string field_seps. Return
    // the n-th field. Fields are indexed from 1, i.e., n == 1 returns the
    // leftmost field. If n is 0, the entire string is returned. (This
    // function is similar to the functionality in awk.)

    CL_StringSequence Split (const char field_seps[] = " \t") const;
    // View our string as being made up of fields, separated by one or
    // more characters from the null-terminated string field_seps. Return
    // the sequence of fields in us.
    
    long Split (CL_String fld[], long n, const char field_seps[] = " \t")
        const;
    // View our string as being made up of fields, separated by one or
    // more characters from the string field_seps. Return the value of
    // fld as follows:
    //      fld[0..n-2]    contains the first n-1 fields, if there are
    //                     that many fields
    //      fld[n-1]       contains the remaining part of the string
    // Return the actual number of cells used in fld as the function
    // value.
    // Note that fld is an array of CL_String objects provided by the
    // caller, and must have at least n CL_Strings in it.

    
    //
    // ------------------------ Comparison -----------------------------------
    
    bool operator<  (const CL_Object& strg) const;

    bool operator<= (const CL_Object& strg) const;

    bool operator>  (const CL_Object& strg) const;

    bool operator>= (const CL_Object& strg) const;

    bool operator== (const CL_Object& strg) const;

    bool operator!= (const CL_Object& strg) const;

    

    virtual bool operator<  (const char* strg) const;

    virtual bool operator<= (const char* strg) const;

    virtual bool operator>  (const char* strg) const;

    virtual bool operator>= (const char* strg) const;

    virtual bool operator== (const char* strg) const;

    virtual bool operator!= (const char* strg) const;

    short Compare (const CL_Object& obj) const;
    // Compare returns -1 if we are less than the given string, 0 if
    // the two are equal, and +1 if we are greater. The method issues a
    // runtime error message if the given object does not have the class id
    // of a string.
    
    short Compare (const CL_String& s) const;
    // Compare returns -1 if we are less than the given string, 0 if
    // the two are equal, and +1 if we are greater.

    bool CompareWith (const CL_String& obj,
                      CL_Object::ComparisonOperator op) const;

    bool CompareWith (const CL_Object& obj,
                      CL_Object::ComparisonOperator op) const;
    // Overrides CL_Object's CompareWith method. Checks that obj's class id
    // is the same as ours and then invokes Compare with obj cast down to
    // CL_String. (Returns FALSE if class id does not match.)



    // --------------------------String modification ----------------------

    virtual bool Insert (char c, long position = -1);
    // Insert a character into the string, immediately to the right of the
    // given position, thus expanding the string. The default position of
    // -1 specifies insertion at the left end of the string. 
    // Return TRUE if successful, FALSE if either an invalid position is
    // specified, a pre-change dependent refused change, or else memory
    // allocation failure occurred.

    virtual bool Insert (const char* p, long position = -1);
    // Insert a null-terminated character string into ourselves. Note that the
    // effect is equivalent to assigning to the null substring at the
    // given position; i.e., {\tt s.Insert (p, pos)} is the same as {\tt s
    // (position+1, 0) = p;}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?