string.h

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

H
549
字号

    bool Append (char c);
    // Append the given character to the string. This method is equivalent
    // to using the += operator, but is a little more efficient for single
    // character appends.

    bool AssignWithFormat (const char* format, ...);
    // Create a char buffer whose contents are identical to those produced
    // if the parameters were passed to printf; assign the result to this
    // string. Return TRUE if successful, FALSE if the result was too big
    // (i.e., > 1K).
    
    // --------- Assignments of various kinds
    
    virtual void operator= (const char* strg);

    virtual void operator= (const CL_String& strg);

    virtual void operator= (const CL_Object& strg);
    // Check that the given parameter is really a string (via the
    // inherited {\tt IsA} method), and
    // assign its value to this string.
    
    virtual void operator= (long l);
    // Convert the given long value into a string, and assign it to
    // this string.

    virtual void operator= (double d);
    // Convert the given double value into a string and assign it to
    // this string.

    

    // Concatenations of various kinds
    
    virtual CL_String operator+  (const char* strg) const;
    // Return the string obtained by concatenating the null-terminated
    // parameter strg to this string.

    virtual CL_String operator+  (const CL_String& strg) const;
    // Return the string obtained by concatenating strg to our value.

    virtual CL_String& operator+= (const char* strg);
    // Append the given (null-terminated) string to this string, and then
    // return this string.

    virtual CL_String& operator+= (const CL_String& strg);

    CL_String operator+ (long v) const;
    // Convert the long value into a decimal string and return the result of
    // appending it to ourselves.

    CL_String& operator+= (long v);
    // Append the string representation of the long value to ourselves.
    
    virtual CL_String operator/ (const CL_String& s) const;
    // "Division" operator:
    // Return the string resulting from the removal of the suffix
    // beginning with the first occurrence of the given string.
    // Thus "Alpha Beta Gamma" / "Beta" yields "Alpha ". If the given
    // string does not occur in us, simply return (a copy of) ourselves.

    void operator/= (const CL_String& s)
        { *this = *this / s;};
    

    // --------------------- Storage and restoration -------------------
    
    virtual long StorableFormWidth () const;

    virtual bool ReadFrom (const CL_Stream&);
    // Read and reconstruct ourselves from the binary representation in
    // the given stream. Return TRUE on success, FALSE if failed for any
    // reason, including when a pre-change dependent disallows the change.

    virtual bool WriteTo  (CL_Stream&) const;
    // Write the passive binary representation of this object into the
    // given stream. Return TRUE if successful, FALSE otherwise.

    CL_String AsString () const
        { return *this; };
    // Override the method inherited from {\small\tt CL_Object}.

    void FromStream (istream& stream);
    // Override the method inherited from {\small\tt CL_Object}. The
    // implementation skips any occurrences of the current fill character
    // of the stream, and then collects non-fill characters into this
    // string.

    virtual istream& ReadLine (istream& stream);
    // Read everything upto and including the next end-of-line marker (or
    // end-of-file) on the given stream, and set our value to the result.

    
    // ------------------------ Miscellaneous methods ----------------
    
    virtual bool PadTo (long num_chars, char pad_char = ' ',
                        bool left_justified = TRUE);
    // Pad ourselves (if necessary)  to fill out  to  the given number of
    // characters.   The padding  character is the second parameter, and  the
    // third parameter specifies whether the padding occurs on the right side
    // (i.e.,  left   justification)   or   the   left   side   (i.e.,  right
    // justification).   If  num_chars is smaller than our size, we  remain
    // unchanged. Return TRUE if padding occurred, FALSE if either the string
    // remained unchanged or memory allocation failed.

    CL_String InUpperCase () const;
    // Return this string with all lower-case letters replaced by upper-case
    // ones.

    CL_String InLowerCase () const;
    // Return this string with all upper-case letters replaced by
    // lower-case ones.

    virtual long ToUpper ();
    // Convert all lower-case characters to upper case. Return the number of
    // characters converted. Note that this can be combined with the
    // operator() to operate on substrings; e.g.,
    //               myString (10,3).ToUpper();
    // converts all lower-case characters between positions 10 and 12 to
    // upper-case.

    virtual long ToLower ();
    // Convert all upper-case characters to lower case. Return the number of
    // characters converted.

    virtual bool WordCapitalize ();
    // If this string begins with an alphabet, render the first letter
    // of the first word upper case, and all other letters in that
    // word lower case. Return TRUE if this change was effected, and
    // FALSE otherwise.
    
    //
    // ------------- Basic methods -------------------------
    //

    const char* ClassName() const {return "CL_String";};

    CL_ClassId ClassId() const { return _CL_String_CLASSID;};

    CL_Object* Clone() const {return new CL_String (*this);};

    // -------------------- End public protocol ------------------


    
protected:

    CL_String (const char* s, long len); // Protected constructor

    virtual bool _Init (long size);

    bool _DoInsert (const char*, long);
    // Do insertion without notification
    
    friend CL_Substring;
    friend CL_StringSplitter;
    
    char* _string;
    long  _capacity;
    long  _size;
};





class CL_Substring: public CL_String {

public:

    virtual void operator= (const char* strg)
        {CL_String::operator= (strg);};
    // Ensure that all assignment operators are inherited.
    
    virtual void operator= (const CL_String& strg)
        {CL_String::operator= (strg);};

    virtual void operator= (const CL_Object& strg)
        {CL_String::operator= (strg);};

    virtual void operator= (long l)
        {CL_String::operator= (l);};

    virtual void operator= (double d)
        {CL_String::operator= (d);};
    
    ~CL_Substring ();

protected:
    
    CL_Substring (CL_String& s, long pos, long length);
    // The constructor is protected, so that a Substring cannot be directly
    // constructed.

    CL_Substring (const CL_Substring&);

    bool _Modified (CL_Object&, long);
    // The method called to notify us when our value changes, so that we may
    // modify the real client.

    friend CL_String;
    CL_String& _client; // The string that we are a substring of
    long       _pos;    // Where the substring begins
    long       _len;    // How long the substring was when constructed
};



// Return pointer to char array representation
inline const char* CL_String::AsPtr() const
{
    return _string;
}

inline CL_String::operator const char* () const
{
    return _string;
}



// Return character at given index
inline char CL_String::operator[] (long index) const
{
    assert (index >= 0 && index < _size,
            ("CL_String::operator[]:"
            " invalid index %ld string size %ld", index, Size()));
    return _string[index];
}



inline void CL_String::operator= (const CL_Object& s)
{
    if (CheckClassType (s, "CL_String::op="))
        *this = (const CL_String&) s;
}


inline CL_Substring CL_String::Suffix (long position)
{
    return (*this) (position, Size() - position);
}


inline CL_String operator+ (const char* s1, const CL_String& s2)
{
    return CL_String (s1) + s2;
}

inline bool CL_String::Append (char c)
{
    long pos = Size() - 1;
    return Insert (c, pos);
}


inline CL_String& CL_String::operator+= (long v)
{
    *this = *this + v;
    return *this;
}


inline bool CL_String::CompareWith (const CL_Object& obj,
                                    CL_Object::ComparisonOperator op) const
{
    return ClassId() == obj.ClassId() ?
        CompareWith ((const CL_String&) obj, op) : FALSE;
}

#endif

⌨️ 快捷键说明

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