📄 l3_str.h
字号:
#ifndef _INCLUDED_L3_STR_H#define _INCLUDED_L3_STR_H// Copyright (C) Krzysztof Bosak, 1998-12-25...1999-11-12.// All rights reserved.// kbosak@box43.pl// http://www.kbosak.prv.pl// *TextString*#include <string.h>#include <stdlib.h>#include <stdio.h>#include "l3_port.h"///////////////////////////////////////////////////////////////////////////////*// Unused, but possible:#include <string>#ifdef WIN32typedef std::string TextString;#elseclass TextString: public std::string{public: inline TextString() { } inline TextString(const char * const ptr) : std::string(ptr) { } inline char& operator[](int i) { assert(i>=0); assert(i<static_cast<int>(length())); return at(i); } inline const char & operator[](int i) const { assert(i>=0); assert(i<static_cast<int>(length())); return at(i); }};#endif*///////////////////////////////////////////////////////////////////////////////#include "l3_array.h"class TextString{ // Small subset of std::string, without reference counting (often faster) // and with strict access control. basearray_s<char> _string;public: inline TextString(): _string(1) // EXCEPTION NEUTRAL { _string[0]='\0'; } inline TextString(const char * const str) // EXCEPTION NEUTRAL // Implicit. : _string(static_cast<int>(strlen(str)+1)) { // Makes sense to use explicit here: str+=56+"abc"; // Should be explicit because of l3_arrayheap assert(str!=NULL); if(str!=NULL) { strcpy(&_string[0], str); } else { _string.setsize(1); _string[0]=0; } } inline void resize(int requested_size) // EXCEPTION NEUTRAL { _string.resize(requested_size); } /* inline int size() const // NOTHROW { return length(); } */ inline int length() const // NOTHROW { return static_cast<int>(strlen(&_string[0])); } inline int capacity() const // NOTHROW { return static_cast<int>(_string.size()); } inline const char * c_str() const // NOTHROW { return &_string[0]; }
inline char * str() const // NOTHROW
{
return (char*)(&_string[0]);
}
inline const char * data() const // NOTHROW { return &_string[0]; } inline char& operator[](int i) // EXCEPTION NEUTRAL { assert(i>=0); assert(i < static_cast<int>(strlen(&_string[0]))); return _string[i]; } inline char operator[](int i) const // EXCEPTION NEUTRAL { assert(i>=0); assert(i < static_cast<int>(strlen(&_string[0]))); return _string[i]; } TextString& operator<<(const TextString& s2) // EXCEPTION NEUTRAL
{
this->operator+=(s2);
return (*this);
}
TextString& operator<<(const int s2) // EXCEPTION NEUTRAL
{
this->operator+=(s2);
return (*this);
}
TextString& operator<<(const char* s2) // EXCEPTION NEUTRAL
{
this->operator+=(static_cast<TextString>(s2));
return (*this);
}
TextString& operator<<(const char s2) // EXCEPTION NEUTRAL
{
char st2[2];
st2[0]=s2;
st2[1]='\0';
this->operator+=(static_cast<TextString>(st2));
return (*this);
}
friend bool operator<(const TextString &a, const TextString &b) // NOTHROW { return strcmp(&a._string[0], &b._string[0])>0; } friend bool operator>(const TextString &a, const TextString &b) // NOTHROW { return strcmp(&a._string[0], &b._string[0])<0; } friend bool operator<=(const TextString &a, const TextString &b) // NOTHROW { return strcmp(&a._string[0], &b._string[0])>=0; } friend bool operator>=(const TextString &a, const TextString &b) // NOTHROW { return strcmp(&a._string[0], &b._string[0])<=0; } friend bool operator==(const TextString &a, const TextString &b) // NOTHROW { return strcmp(&a._string[0], &b._string[0])==0; } friend bool operator==(const TextString &a, const char * const b) // NOTHROW { assert(b!=NULL); return strcmp(&a._string[0], b)==0; } friend bool operator!=(const TextString &a, const TextString &b) // NOTHROW { return strcmp(&a._string[0], &b._string[0])!=0; } friend bool operator!=(const TextString &a, const char * const b) // NOTHROW { assert(b!=NULL); return strcmp(&a._string[0], b)!=0; } TextString& operator+=(const TextString& s); // EXCEPTION NEUTRAL inline TextString& operator+=(int number) // EXCEPTION NEUTRAL { char buffer[36]; buffer[35]='\0'; sprintf(buffer, "%d", number); assert(buffer[35]=='\0'); buffer[35]='\0'; return operator+=(TextString(&buffer[0]));// OK to return by reference. } inline TextString& operator+=(double number) // EXCEPTION NEUTRAL { char buffer[36]; buffer[35]='\0'; sprintf(buffer, "%f", number); assert(buffer[35]=='\0'); buffer[35]='\0'; return operator+=(TextString(&buffer[0]));// OK to return by reference. } friend const TextString operator+(const TextString& s1, const TextString& s2) // EXCEPTION NEUTRAL { TextString result(s1); result.operator+=(s2); return result; } friend const TextString operator+(const TextString& s1, int number) // EXCEPTION NEUTRAL { TextString result(s1); result.operator+=(number); return result; } friend const TextString operator+(const TextString& s1, double number) // EXCEPTION NEUTRAL { TextString result(s1); result.operator+=(number); return result; } friend ostream& operator<<(ostream& str, const TextString& text) { return str<<text.c_str(); }};#endif // _INCLUDED_L3_STR_H
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -