All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
QoreString.h
/* -*- mode: c++; indent-tabs-mode: nil -*- */
/*
QoreString.h
QoreString Class Definition
Qore Programming Language
Copyright 2003 - 2013 David Nichols
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library 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. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef _QORE_QORESTRING_H
#define _QORE_QORESTRING_H
#include <stdarg.h>
#include <string>
class DateTime;
class BinaryNode;
class QoreString {
protected:
struct qore_string_private* priv;
DLLLOCAL void splice_simple(qore_size_t offset, qore_size_t length, QoreString* extract = 0);
DLLLOCAL void splice_simple(qore_size_t offset, qore_size_t length, const char* str, qore_size_t str_len, QoreString* extract = 0);
DLLLOCAL void splice_complex(qore_offset_t offset, ExceptionSink* xsink, QoreString* extract = 0);
DLLLOCAL void splice_complex(qore_offset_t offset, qore_offset_t length, ExceptionSink* xsink, QoreString* extract = 0);
DLLLOCAL void splice_complex(qore_offset_t offset, qore_offset_t length, const QoreString* str, ExceptionSink* xsink, QoreString* extract = 0);
DLLLOCAL int substr_simple(QoreString* str, qore_offset_t offset) const;
DLLLOCAL int substr_simple(QoreString* str, qore_offset_t offset, qore_offset_t length) const;
DLLLOCAL int substr_complex(QoreString* str, qore_offset_t offset, ExceptionSink* xsink) const;
DLLLOCAL int substr_complex(QoreString* str, qore_offset_t offset, qore_offset_t length, ExceptionSink* xsink) const;
// writes a new QoreString with the characters reversed of the "this" QoreString
// assumes the encoding is the same and the length is 0
DLLLOCAL void concat_reverse(QoreString* targ) const;
DLLLOCAL int snprintf(size_t size, const char* fmt, ...);
DLLLOCAL int vsnprintf(size_t size, const char* fmt, va_list args);
DLLLOCAL static int convert_encoding_intern(const char* src, qore_size_t src_len, const QoreEncoding* from, QoreString& targ, const QoreEncoding* to, ExceptionSink* xsink);
public:
DLLEXPORT QoreString();
DLLEXPORT QoreString(bool b);
DLLEXPORT QoreString(const char* str);
DLLEXPORT QoreString(const char* str, const QoreEncoding* new_qorecharset);
DLLEXPORT QoreString(const QoreEncoding* new_qorecharset);
DLLEXPORT QoreString(const char* str, qore_size_t len, const QoreEncoding* new_qorecharset = QCS_DEFAULT);
DLLEXPORT QoreString(const std::string& str, const QoreEncoding* new_encoding = QCS_DEFAULT);
DLLEXPORT QoreString(char c);
DLLEXPORT QoreString(const QoreString& str);
DLLEXPORT QoreString(const QoreString* str);
DLLEXPORT QoreString(const QoreString* str, qore_size_t len);
DLLEXPORT QoreString(int64 i);
DLLEXPORT QoreString(double f);
DLLEXPORT QoreString(const DateTime* date);
DLLEXPORT QoreString(const BinaryNode* bin);
DLLEXPORT QoreString(const BinaryNode* bin, qore_size_t maxlinelen);
DLLEXPORT QoreString(char* nbuf, qore_size_t nlen, qore_size_t nallocated, const QoreEncoding* enc);
DLLEXPORT ~QoreString();
DLLEXPORT qore_size_t length() const;
DLLEXPORT void set(const char* str, const QoreEncoding* new_qorecharset = QCS_DEFAULT);
DLLEXPORT void set(const std::string& str, const QoreEncoding* new_qorecharset = QCS_DEFAULT);
DLLEXPORT void set(const QoreString* str);
DLLEXPORT void set(const QoreString& str);
DLLEXPORT void setEncoding(const QoreEncoding* new_encoding);
DLLEXPORT void concatAndHTMLEncode(const char* str);
DLLEXPORT void concatAndHTMLDecode(const QoreString* str);
DLLEXPORT void concatEscape(const QoreString* str, char c, char esc_char, ExceptionSink* xsink);
DLLEXPORT void concatEscape(const char* str, char c, char esc_char = '\\');
DLLEXPORT void concatAndHTMLEncode(const QoreString* , ExceptionSink* xsink);
DLLEXPORT void concat(const QoreString* str, ExceptionSink* xsink);
DLLEXPORT int concat(const QoreString& str, qore_offset_t pos, ExceptionSink* xsink);
DLLEXPORT int concat(const QoreString& str, qore_offset_t pos, qore_offset_t len, ExceptionSink* xsink);
DLLEXPORT void concat(const QoreString* str, qore_size_t size, ExceptionSink* xsink);
DLLEXPORT void concatBase64(const char* buf, qore_size_t size);
DLLEXPORT void concatBase64(const BinaryNode* bin);
DLLEXPORT void concatBase64(const QoreString* str);
DLLEXPORT void concatBase64(const char* buf, qore_size_t size, qore_size_t maxlinelen);
DLLEXPORT void concatBase64(const BinaryNode* bin, qore_size_t maxlinelen);
DLLEXPORT void concatBase64(const QoreString* str, qore_size_t maxlinelen);
DLLEXPORT BinaryNode* parseBase64(ExceptionSink* xsink) const;
DLLEXPORT QoreString* parseBase64ToString(ExceptionSink* xsink) const;
DLLEXPORT QoreString* parseBase64ToString(const QoreEncoding* enc, ExceptionSink* xsink) const;
DLLEXPORT void concatHex(const char* buf, qore_size_t size);
DLLEXPORT void concatHex(const BinaryNode* bin);
DLLEXPORT void concatHex(const QoreString* str);
DLLEXPORT BinaryNode* parseHex(ExceptionSink* xsink) const;
DLLEXPORT void concat(const DateTime* d);
DLLEXPORT void concatISO8601DateTime(const DateTime* d);
DLLEXPORT void concat(const char* str);
DLLEXPORT void concat(const std::string& str);
DLLEXPORT void concat(const char* str, qore_size_t size);
DLLEXPORT void concat(const char c);
DLLEXPORT int compareSoft(const QoreString* str, ExceptionSink* xsink) const;
DLLEXPORT int compare(const QoreString* str) const;
DLLEXPORT int compare(const char* str) const;
DLLEXPORT void terminate(qore_size_t size);
DLLEXPORT void reserve(qore_size_t size);
DLLEXPORT int sprintf(const char* fmt, ...);
DLLEXPORT int vsprintf(const char* fmt, va_list args);
DLLEXPORT void take(char* str);
DLLEXPORT void take(char* str, const QoreEncoding* enc);
DLLEXPORT void take(char* str, qore_size_t size);
DLLEXPORT void take(char* str, qore_size_t size, const QoreEncoding* enc);
DLLEXPORT void takeAndTerminate(char* str, qore_size_t size);
DLLEXPORT void takeAndTerminate(char* str, qore_size_t size, const QoreEncoding* enc);
DLLEXPORT QoreString* convertEncoding(const QoreEncoding* nccs, ExceptionSink* xsink) const;
DLLEXPORT char* giveBuffer();
DLLEXPORT void clear();
DLLEXPORT void reset();
DLLEXPORT void replaceAll(const char* old_str, const char* new_str);
DLLEXPORT void replace(qore_size_t offset, qore_size_t len, const char* str);
DLLEXPORT void replace(qore_size_t offset, qore_size_t len, const QoreString* str);
DLLEXPORT void replace(qore_size_t offset, qore_size_t len, const QoreString* str, ExceptionSink* xsink);
DLLEXPORT void replaceChar(qore_size_t offset, char c);
DLLEXPORT void splice(qore_offset_t offset, ExceptionSink* xsink);
DLLEXPORT void splice(qore_offset_t offset, qore_offset_t length, ExceptionSink* xsink);
DLLEXPORT void splice(qore_offset_t offset, qore_offset_t length, const AbstractQoreNode* strn, ExceptionSink* xsink);
DLLEXPORT void splice(qore_offset_t offset, qore_offset_t length, const QoreString& str, ExceptionSink* xsink);
DLLEXPORT QoreString* extract(qore_offset_t offset, ExceptionSink* xsink);
DLLEXPORT QoreString* extract(qore_offset_t offset, qore_offset_t length, ExceptionSink* xsink);
DLLEXPORT QoreString* extract(qore_offset_t offset, qore_offset_t length, const AbstractQoreNode* strn, ExceptionSink* xsink);
DLLEXPORT QoreString* substr(qore_offset_t offset, ExceptionSink* xsink) const;
DLLEXPORT QoreString* substr(qore_offset_t offset, qore_offset_t length, ExceptionSink* xsink) const;
DLLEXPORT qore_size_t chomp();
DLLEXPORT const QoreEncoding* getEncoding() const;
DLLEXPORT QoreString* copy() const;
DLLEXPORT void tolwr();
DLLEXPORT void toupr();
DLLEXPORT qore_size_t strlen() const;
DLLEXPORT qore_size_t size() const;
DLLEXPORT qore_size_t capacity() const;
DLLEXPORT const char* getBuffer() const;
DLLEXPORT void allocate(unsigned requested_size);
DLLEXPORT int insertch(char c, qore_size_t pos, unsigned times);
DLLEXPORT int insert(const char* str, qore_size_t pos);
DLLEXPORT void addch(char c, unsigned times);
DLLEXPORT void concatUTF8FromUnicode(unsigned code);
DLLEXPORT int concatUnicode(unsigned code, ExceptionSink* xsink);
DLLEXPORT int concatUnicode(unsigned code);
DLLEXPORT QoreString* reverse() const;
DLLEXPORT void trim_trailing(const char* chars = 0);
DLLEXPORT void trim_leading(const char* chars = 0);
DLLEXPORT void trim(const char* chars = 0);
DLLEXPORT void trim_trailing(char c);
DLLEXPORT void trim_single_trailing(char c);
DLLEXPORT void trim_leading(char c);
DLLEXPORT void trim_single_leading(char c);
DLLEXPORT void trim(char c);
DLLEXPORT unsigned int getUnicodePointFromUTF8(qore_offset_t offset = 0) const;
DLLEXPORT unsigned int getUnicodePoint(qore_offset_t offset, ExceptionSink* xsink) const;
DLLEXPORT void prepend(const char* str);
DLLEXPORT void prepend(const char* str, qore_size_t size);
DLLEXPORT QoreString& operator=(const QoreString& other);
DLLEXPORT QoreString& operator=(const char* other);
DLLEXPORT QoreString& operator=(const std::string& other);
DLLEXPORT bool operator==(const QoreString& other) const;
DLLEXPORT bool operator==(const std::string& other) const;
DLLEXPORT bool operator==(const char* other) const;
DLLEXPORT char operator[](qore_offset_t pos) const;
DLLEXPORT QoreString& operator+=(const char* str);
DLLEXPORT QoreString& operator+=(const std::string& str);
DLLEXPORT bool empty() const;
DLLEXPORT qore_offset_t index(const QoreString& needle, qore_offset_t pos, ExceptionSink* xsink) const;
DLLEXPORT qore_offset_t bindex(const QoreString& needle, qore_offset_t pos) const;
DLLEXPORT qore_offset_t bindex(const char* needle, qore_offset_t pos) const;
DLLEXPORT qore_offset_t bindex(const std::string& needle, qore_offset_t pos) const;
DLLEXPORT qore_offset_t rindex(const QoreString& needle, qore_offset_t pos, ExceptionSink* xsink) const;
DLLEXPORT qore_offset_t brindex(const QoreString& needle, qore_offset_t pos) const;
DLLEXPORT qore_offset_t brindex(const char* needle, qore_offset_t pos) const;
DLLEXPORT qore_offset_t brindex(const std::string& needle, qore_offset_t pos) const;
DLLEXPORT qore_offset_t find(char c, qore_offset_t pos = 0) const;
DLLEXPORT qore_offset_t rfind(char c, qore_offset_t pos = -1) const;
DLLEXPORT bool isDataPrintableAscii() const;
DLLEXPORT bool isDataAscii() const;
DLLEXPORT int64 toBigInt() const;
DLLEXPORT qore_offset_t getByteOffset(qore_size_t i, ExceptionSink* xsink) const;
// concatenates a qorestring without converting encodings - internal only
DLLLOCAL void concat(const QoreString* str);
// private constructor
DLLLOCAL QoreString(struct qore_string_private* p);
};
DLLEXPORT QoreString* checkEncoding(const QoreString* str, const QoreEncoding* enc, ExceptionSink* xsink);
class QoreStringMaker : public QoreString {
private:
DLLLOCAL QoreStringMaker(const QoreStringMaker& str);
DLLLOCAL QoreStringMaker& operator=(const QoreStringMaker&);
public:
DLLEXPORT QoreStringMaker(const char* fmt, ...);
DLLEXPORT QoreStringMaker(const QoreEncoding* enc, const char* fmt, ...);
};
class TempString {
private:
QoreString* str;
TempString(const TempString& );
TempString& operator=(const TempString& );
void* operator new(size_t);
public:
DLLLOCAL TempString() : str(new QoreString) {
}
DLLLOCAL TempString(const QoreEncoding* enc) : str(new QoreString(enc)) {
}
DLLLOCAL TempString(QoreString* s) {
str = s;
}
DLLLOCAL ~TempString() {
delete str;
}
DLLLOCAL QoreString* operator->(){ return str; };
DLLLOCAL QoreString* operator*() { return str; };
DLLLOCAL operator bool() const { return str != 0; }
DLLLOCAL QoreString* release() { QoreString* rv = str; str = 0; return rv; }
};
class TempEncodingHelper {
private:
QoreString* str;
bool temp;
DLLLOCAL TempEncodingHelper(const TempEncodingHelper& );
DLLLOCAL TempEncodingHelper& operator=(const TempEncodingHelper& );
DLLLOCAL void* operator new(size_t);
DLLLOCAL void discard_intern() {
if (temp && str)
delete str;
}
DLLLOCAL void set_intern(const QoreString* s, const QoreEncoding* qe, ExceptionSink* xsink) {
if (s->getEncoding() != qe) {
str = s->convertEncoding(qe, xsink);
temp = true;
}
else {
str = const_cast< QoreString* >(s);
temp = false;
}
}
public:
DLLLOCAL TempEncodingHelper(const QoreString& s, const QoreEncoding* qe, ExceptionSink* xsink) {
set_intern(&s, qe, xsink);
}
DLLLOCAL TempEncodingHelper(const QoreString* s, const QoreEncoding* qe, ExceptionSink* xsink) {
set_intern(s, qe, xsink);
}
DLLLOCAL TempEncodingHelper() : str(0), temp(false) {
}
DLLLOCAL ~TempEncodingHelper() {
discard_intern();
}
DLLLOCAL int set(const QoreString* s, const QoreEncoding* qe, ExceptionSink* xsink) {
discard_intern();
set_intern(s, qe, xsink);
return str != 0;
}
DLLLOCAL bool is_temp() const {
return temp;
}
DLLLOCAL const QoreString* operator->(){ return str; };
DLLLOCAL const QoreString* operator*() { return str; };
DLLLOCAL operator bool() const { return str != 0; }
DLLLOCAL char* giveBuffer() {
if (!str)
return 0;
if (temp)
return str->giveBuffer();
return strdup(str->getBuffer());
}
};
#endif