⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cstring.h

📁 Shorthand是一个强大的脚本语言
💻 H
字号:
#ifndef __cstring_h
#define __cstring_h
/////////////////////////////////////////////////////////////////////////////
// $Header: /shorthand/src/cstring.h 7     2/14/03 4:20a Arm $
//---------------------------------------------------------------------------
// This file is part of "libAndrix" library - a collection of classes
// and functions developed by Andrei Remenchuk.
//---------------------------------------------------------------------------
// While you may own complete copyright on the project with which you have
// received this file, the author reserves the right to use code contained
// in this very file for any purposes, including publishing and usage in
// any free or commercial software.
//
// You may re-distribute this file or re-use it in your own free or
// commercial software provided that this text is included in the file.
// If you change this file you must include clear notice stating that
// you changed this file and the date of change.
//
// This statement doesn't apply to other files that are part of the same
// package unless otherwise noted.
//---------------------------------------------------------------------------
// (c) 1998-2002 Andrei Remenchuk <andrei@remenchuk.com>
//---------------------------------------------------------------------------
// cstring.h - string class declaration
/////////////////////////////////////////////////////////////////////////////

#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include "array.h"

/**
 * Powerfull string class which replaces standard string and CString.
 */
class string
{
private:
    // string length
    int m_length;

    // character buffer capacity
    int m_capacity;
    
    // buffer capacity increment
    int m_increment;

    // character buffer
    char* m_buffer;


private:
    
    // makes sure buffer has required capacity
    void ensure_capacity(int n);

    // internal common construction
    void ctor();

    // internal helper function used in split() methods
    const char* set_from_quoted(const char* s, char stopchar, bool stop_on_space);

public:
    string();
    string(const char* s, int length = -1);
    string(const string& s);
    string* clone() const;
    ~string();

    char* provide_window(int length);

    // replaces string contents by formatted text
    void printf(const char* format, ...);
    
    // replaces string contents by formatted text
    void vprintf(const char* format, va_list args);

    // appends buffer to this string
    // if length is given, only that much characters are copied
    void append(const char* s, int length = -1);
    
    // formats text and appends it to this string
    void appendf(const char* format, ...);

    // sets string contents from char buffer
    // if length is given, only that much characters are copied
    void set(const char *s, int length = -1);

    // pastes a piece of another string 
    void paste(const char* s, int start, int end);
    
    // appends one character to this string
    void append(char ch);

    // clears string content; string becomes empty
    void clear();
    
    // reports whether or not string string contains any of the 
    // specified characters
    static bool haschars(const char* s, const char* chars);

    // returns NULL if string is empty, and char* value if not
    const char* nv() const; 

    // returns string length
    inline int length() const { return m_length; }
    
    // truncates string length (if within range)
    int set_length(int n);

    // returns true if string is empty
    inline bool is_empty() const { return m_length == 0; }

    // returns true if string is empty
    static bool is_empty(const char* s) { return s == NULL || *s == '\0'; }

    // returns const pointer to null-terminated string buffer 
    const char* cstr() const { return m_buffer; }

    void set_at(int index, char ch);

    char char_at(int index) const;
    
    char last() const;

    char first() const;

    // removes all trailing whitespace characters 
    void rtrim();

    // removes all leading whitespace characters 
    void ltrim();

    // removes all leading and trailing  whitespace characters 
    void trim();

    // converts string to lowercase
    void lowercase();

    // converts string to lowercase
    void uppercase();

    // URL-decodes this string into another string
    void urldecode(string& target) const;

    // URL-encodes this string into another string
    void urlencode(string& target) const;

    // URL-encodes any string into another string
    static void urlencode(const char* source, string& target);

    // assigns this string from another buffer
    const string& operator = (const char* s);

    // assigns this string from another string object
    const string& operator = (const string& s);

    // casts string object to (const char*)
    inline operator const char* () const { return m_buffer; }

    // casts string object to (char*)
    //inline operator char* () { return m_buffer; }
    
    // reports whether or not this string is equal to another string (ignoring case)
    bool ieq(const char* s) const;

    // reports whether or not this string is equal to another string 
    bool operator == (const char* s) const;
    
    // reports whether or not this string is not equal to another string 
    bool operator != (const char* s) const;

    // removes last character
    const char* chop();

    // keeps removing last character while if it is one of specified characters,
    // until there is at least one character left in the string
    const char* chop(const char* pat);
    
    // splits this string into fields (delimited by whitespace characters)
    int split(array<string, true>& list);

    // splits supplies buffer into fields (delimited by any of specified characters)
    static int split(const char* s, array<string, true>& list, const char* separators);

    // splits supplies buffer into fields (delimited by whitespace characters)
    static int split(const char* s, array<string, true>& list);
    
    // splits this string into fields according to the pattern
    int split(const char* pattern, ...);

    // splits this string into fields according to the pattern
    static int split(const char* s, const char* pattern, va_list args);

    // splits this string into fields according to the pattern
    static int split(const char* s, const char* pattern, ...);

    // used to iterate through all lines within this string
    const char* next_line(void** position, bool skip_leading_spaces = true);

    // returns number pattern that matches this string;
    // patterns are given as "pattern1\0pattern2\0\pattern3\0....\0\0"
    int index(const char* pattern, int default_index = -1, bool case_sensitive = false);

    bool begins_with(const char* s, bool case_sensitive = false) const;

    bool ends_with(const char* s, bool case_sensitive = false) const;

    bool contains(const char* s, bool case_sensitive = false) const;
    
    // removes speficied prefix from the string (if it begins with it)
    bool remove_prefix(const char* prefix, bool case_sensitive = false);

    // parses RFC-822 e-mail address 
    bool parse_email(string& name, string& address);
    
    // parses RFC-822 e-mail address 
    static bool parse_email(const char* spec, string& name, string& address);

    // saves string contents to file
    int save_to_file(const char* file_name);

    // loads string contents from file
    bool load_from_file(const char* file_name);

    // replaces a range of characters within this string by another string
    // return value is true if offset and length were valid
    bool replace_range(int offset, int length, const char* replacement);

    // replaces every occurance of substring within this string
    // return value is number of replacements actually performed
    int replace_all(const char* pattern, const char* replacement);

    // replaces every occurance of the character by another character
    int replace_char(char pattern, char replacement);

    int translate(char ch1, char ch2);

    int strpos(char ch, int start_index = 0) const;

    int strpos(const char* str, int start_index = 0) const;

};

typedef array<string, true> string_array;

int vawidth(const char* format, va_list* args);

inline char* strend(char* s) { return s + strlen(s); }

inline const char* strend(const char* s) { return s + strlen(s); }




#endif // __cstring_h

⌨️ 快捷键说明

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