📄 ofstring.h
字号:
/* * * Copyright (C) 1997-2005, OFFIS * * This software and supporting documentation were developed by * * Kuratorium OFFIS e.V. * Healthcare Information and Communication Systems * Escherweg 2 * D-26121 Oldenburg, Germany * * THIS SOFTWARE IS MADE AVAILABLE, AS IS, AND OFFIS MAKES NO WARRANTY * REGARDING THE SOFTWARE, ITS PERFORMANCE, ITS MERCHANTABILITY OR * FITNESS FOR ANY PARTICULAR USE, FREEDOM FROM ANY COMPUTER DISEASES OR * ITS CONFORMITY TO ANY SPECIFICATION. THE ENTIRE RISK AS TO QUALITY AND * PERFORMANCE OF THE SOFTWARE IS WITH THE USER. * * Module: ofstd * * Author: Andrew Hewett * * Purpose: A simple string class * * Last Update: $Author: meichel $ * Update Date: $Date: 2005/12/08 16:06:07 $ * CVS/RCS Revision: $Revision: 1.20 $ * Status: $State: Exp $ * * CVS/RCS Log at end of file * */#ifndef OFSTRING_H#define OFSTRING_H#include "dcmtk/config/osconfig.h" /* include OS specific configuration first */#include "dcmtk/ofstd/oftypes.h" /* for OFBool */#include "dcmtk/ofstd/ofcast.h"#ifdef HAVE_STD_STRING/*** Use the ANSI Standard string class*/#include <string>#define OFString std::string#define OFString_npos std::string::npos#else /* not HAVE_STD_STRING *//*** Declare our own string class*/#define INCLUDE_CASSERT#define INCLUDE_CSTRING#define INCLUDE_CSTDLIB#define INCLUDE_LIBC#define INCLUDE_UNISTD#include "dcmtk/ofstd/ofstdinc.h"#include "dcmtk/ofstd/ofstream.h"#include "dcmtk/ofstd/oftypes.h"/*** Error macros*/#define OFSTRING_OUTOFRANGE(cond) assert (!(cond))#define OFSTRING_LENGTHERROR(cond) assert (!(cond))#define OFSTRING_MEMORYALLOCERROR(cond) assert (!(cond))/** OFString_npos is a value larger than any "reasonable" string and is * used to denote "until the end" when a length is required. * Normally string::npos is defined as a static const member * but some C++ compilers are too primitive. */static const size_t OFString_npos = (OFstatic_cast(size_t, -1));/** a simple string class that implements a subset of std::string. * It does not implement iterators or traits and is not optimized for speed. */class OFString{public: /* * The SunOS C++ 2.0.1 does not allow static const members. * We would like to define: * static const size_t npos = ((size_t)-1); * but cannot so an alternative OFString_npos is defined outside * the class (see above). */ /** Default constructor. Constructs an empty string. */ OFString(); /** Constructs a string from the given input string str. The effective * length rlen of the constructed string is the smaller of n and * str.size() - pos, and the string is constructed by copying rlen * characters starting at position pos of the input string str. The * function throws an out-of-range error if pos > str.size(). * @param str string to copy from * @param pos position to start copying from * @param n maximum number of characters to copy */ OFString(const OFString& str, size_t pos = 0, size_t n = OFString_npos); /** This constructor copies n characters starting at s, and constructs a * string object initialized with the corresponding characters. * NOTE: If n > length(s), then junk characters are appended * to the end of the string. i.e. n characters are copied regardless of * the exact length of the array pointed to by the input pointer s. * @param s pointer to an array of char of length n. Must not be NULL. * @param n number of characters in array */ OFString(const char* s, size_t n); /** Constructs a string object from the array pointed to by the input * pointer s. It is assumed that s is not a null pointer. * @param s pointer to a zero-terminated C string. Must not be NULL. */ OFString(const char* s); /** Constructs a string object with the character c repeated rep times. * Reports a length error if rep equals npos. * @param rep number of repetitions * @param c character to construct from */ OFString(size_t rep, char c); /** destructor */ ~OFString(); /** assigns the input string to the current string. * @param rhs string to copy from * @return reference to this object */ OFString& operator=(const OFString& rhs); /** constructs a temporary string from the input s and assigns it to the current string. * @param s pointer to a zero-terminated C string. Must not be NULL. * @return reference to this object */ OFString& operator=(const char* s); /** constructs a temporary string from the input s and assigns it to the current string. * @param s character * @return reference to this object */ OFString& operator=(char s); /** Appends the input string to the current string. * @param rhs string to append from * @return reference to this object */ OFString& operator+=(const OFString& rhs); /** constructs a temporary string from the input s and appends it to the current string. * @param s pointer to a zero-terminated C string. Must not be NULL. * @return reference to this object */ OFString& operator+=(const char* s); /** constructs a temporary string from the input s and appends it to the current string. * @param s character * @return reference to this object */ OFString& operator+=(char s); /** Appends characters from the input string str to the current string * object. At most n characters, starting at position pos of str, are * appended. * The function reports an out-of-range error if pos > str.size(). * @param str string to append from * @param pos position to start copying from * @param n maximum number of characters to copy * @return reference to this object */ OFString& append(const OFString& str, size_t pos = 0, size_t n = OFString_npos); /** constructs a temporary string from the input and appends it to the current string. * @param s pointer to an array of char of length n. Must not be NULL. * @param n number of characters in array * @return reference to this object */ OFString& append(const char* s, size_t n); /** constructs a temporary string from the input and appends it to the current string. * @param s pointer to a zero-terminated C string. Must not be NULL. * @return reference to this object */ OFString& append(const char* s); /** constructs a temporary string from the input and appends it to the current string. * @param rep number of repetitions * @param c character to construct from * @return reference to this object */ OFString& append(size_t rep, char c); /** Assigns characters from the input string str to the current string * object. At most n characters, starting at position pos of str, are * appended. * The function reports an out-of-range error if pos > str.size(). * @param str string to append from * @param pos position to start copying from * @param n maximum number of characters to copy * @return reference to this object */ OFString& assign(const OFString& str, size_t pos = 0, size_t n = OFString_npos); /** constructs a temporary string from the input and assigns it to the current string. * @param s pointer to an array of char of length n. Must not be NULL. * @param n number of characters in array * @return reference to this object */ OFString& assign(const char* s, size_t n); /** constructs a temporary string from the input and assigns it to the current string. * @param s pointer to a zero-terminated C string. Must not be NULL. * @return reference to this object */ OFString& assign(const char* s); /** constructs a temporary string from the input and assigns it to the current string. * @param rep number of repetitions * @param c character to construct from * @return reference to this object */ OFString& assign(size_t rep, char c); /** Inserts at most n characters, starting at position pos2 of the input * string str, into the current string. The characters are inserted * starting at position pos1 in the current string. * The function reports an out-of-range error if pos > str.size(). * @param pos1 position to insert at * @param str string to copy from * @param pos2 position to start copying from * @param n maximum number of characters to copy * @return reference to this object */ OFString& insert(size_t pos1, const OFString& str, size_t pos2 = 0, size_t n = OFString_npos); /** constructs a temporary string from the input and inserts * it into the current string. * @param pos position to insert at * @param s pointer to an array of char of length n. Must not be NULL. * @param n number of characters in array * @return reference to this object */ OFString& insert(size_t pos, const char* s, size_t n); /** constructs a temporary string from the input and inserts * it into the current string. * @param pos position to insert at * @param s pointer to a zero-terminated C string. Must not be NULL. * @return reference to this object */ OFString& insert(size_t pos, const char* s); /** constructs a temporary string from the input and inserts * it into the current string. * @param pos position to insert at * @param rep number of repetitions * @param c character to construct from * @return reference to this object */ OFString& insert(size_t pos, size_t rep, char c); /** Removes up to n characters from the string starting from position pos. * @param pos position to start from * @param n number of characters to remove * @return reference to this object */ OFString& erase (size_t pos = 0, size_t n = OFString_npos); /** replaces a range of characters in the current string * with a range of characters taken from the input string str. The * range to be replaced starts at position pos1 in the current string, * and extends for n1 characters, or up to the end of the string, * whichever comes first. * The range of characters inserted starts at position pos2 of the * input string str, and extends for n2 characters, or up to the end of * the string str, whichever comes first. * @param pos1 position to insert at * @param n1 number of characters to replace * @param str string to copy from * @param pos2 position to start copying from * @param n2 maximum number of characters to copy * @return reference to this object */ OFString& replace(size_t pos1, size_t n1, const OFString& str, size_t pos2 = 0, size_t n2 = OFString_npos); /** constructs a temporary string from the input and replaces the range [pos, n] * in the current string with the constructed string. * @param pos position to replace at * @param n number of characters to be replaced * @param s pointer to an array of char of length n. Must not be NULL. * @param n2 number of characters in array * @return reference to this object */ OFString& replace(size_t pos, size_t n, const char* s, size_t n2); /** constructs a temporary string from the input and replaces the range [pos, n] * in the current string with the constructed string. * @param pos position to replace at * @param n number of characters to be replaced * @param s pointer to a zero-terminated C string. Must not be NULL. * @return reference to this object */ OFString& replace(size_t pos, size_t n, const char* s); /** constructs a temporary string from the input and replaces the range [pos, n] * in the current string with the constructed string. * @param pos position to replace at * @param n number of characters to be replaced * @param rep number of repetitions * @param s character to construct from * @return reference to this object */ OFString& replace(size_t pos, size_t n, size_t rep, char s); /** returns a constant reference to the character at * position pos of the current string. * if pos >= size(), throws out_of_range exception. * @param pos position in string * @return const reference to character in string at pos */ const char& at(size_t pos) const { OFSTRING_OUTOFRANGE (pos >= this->size()); return this->theCString[pos]; } /** returns a non-const reference to the character at * position pos of the current string. * if pos >= size(), throws out_of_range exception. * @param pos position in string * @return non-const reference to character in string at pos */ char& at(size_t pos) { OFSTRING_OUTOFRANGE (pos >= this->size()); return this->theCString[pos]; } /** returns the element at position pos of the current string. Returns * '\0' if pos == size(). * @param pos position in string * @return character in string at pos (by value) */ char operator[] (size_t pos) const { if (pos == this->size()) return '\0'; else { OFSTRING_OUTOFRANGE (pos > this->size()); return this->theCString[pos]; } } /** returns the element at position pos of the current string. * The reference returned is invalid after a subsequent call to any * non-const member function for the object. * @param pos position in string, must be < size(). * @return character in string at pos (by reference) */ char& operator[] (size_t pos) { OFSTRING_OUTOFRANGE (pos >= this->size()); return this->theCString[pos]; } /** returns a pointer to the initial element of an array of length * size()+1 whose first size() elements equal the corresponding elements * of the current string and whose last element is a null character. * @return C string for this string
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -