|
|
|||
Warning, file /include/Geant4/MCGIDI_string.hpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).
0001 /* 0002 # <<BEGIN-copyright>> 0003 # Copyright 2019, Lawrence Livermore National Security, LLC. 0004 # This file is part of the gidiplus package (https://github.com/LLNL/gidiplus). 0005 # gidiplus is licensed under the MIT license (see https://opensource.org/licenses/MIT). 0006 # SPDX-License-Identifier: MIT 0007 # <<END-copyright>> 0008 */ 0009 0010 #ifndef MCGIDI_STRING_HPP 0011 #define MCGIDI_STRING_HPP 0012 0013 /* Modified from Karsten Burger's version 2017 0014 * Made changes to make it more compatible with GPUs. 0015 * Allow for data to be initialized to nullptr. 0016 * 0017 * Modified from public domain software: 0018 * Karsten Burger 2014 0019 * 0020 * Sourceforge project "Simple C++ String Class" 0021 * http://sourceforge.net/projects/simplecstringclass/ 0022 0023 * This a simple C++ string class based on class my_string by 0024 * Christian Stigen Larsen, 2007, http://csl.name/programming/my_string/ 0025 * 0026 * It only uses the C-string functions and is thus independent of the 0027 * standard C++ library. 0028 * 0029 * It is public domain, in the hope, that you find it useful. 0030 * Please note that there is no guarantee of any kind: it is supplied 0031 * without any warranty; without even the implied warranty of 0032 * merchantability or fitness for a particular purpose. 0033 * 0034 * You can probably replace std::string with this one in many 0035 * cases, but a lot of stuff is missing, and I would recommend 0036 * you stick to std::string anyway. 0037 * 0038 * I want to point out that there is nothing fancy about this class. 0039 * It keeps every string in its own buffer, and copies as often as 0040 * needed. 0041 * The data always contains a trailing NUL char. 0042 * 0043 * However, I believe that is a good approach. For instance, it 0044 * uses malloc rather than new, which makes it possible to use 0045 * realloc. On many systems, realloc will try to use up "invisible" 0046 * space that was used by malloc to pad a string for memory alignment. 0047 * That makes it potentially fast for small concatenations. 0048 * 0049 * I don't propose to use this class for anything practical, since 0050 * we already have std::string, but it may be an interesting read 0051 * for C++ novices at the very least. Also, additional functions can 0052 * easily be expanded. 0053 * 0054 * Also I met a case, where I had to avoid std::string because of 0055 * link problems with an application using mixed libraries, especially 0056 * one compiled with an old Intel compiler icc 7. 0057 * 0058 * Bugs/suggestions to info [at) dr-burger ]dot[ com 0059 * or via the Sourceforge project page. 0060 */ 0061 0062 #include <sys/types.h> // size_t 0063 #include <stdexcept> 0064 #include <LUPI_declareMacro.hpp> 0065 0066 /** @brief Simple C++ string class, useful as replacement for 0067 std::string if this cannot be used, or just for fun. 0068 0069 */ 0070 namespace MCGIDI { 0071 0072 class String 0073 { 0074 0075 char* p; ///< The data 0076 size_t allocated_; ///< The allocated memory size (including trailing NUL) 0077 size_t size_; ///< The currently used memory size (excluding trailing NUL) 0078 0079 public: 0080 typedef size_t size_type; 0081 static const size_type npos; 0082 0083 LUPI_HOST_DEVICE String(); 0084 LUPI_HOST_DEVICE ~String(); 0085 LUPI_HOST_DEVICE String(const String&); 0086 LUPI_HOST_DEVICE String(const char*); 0087 0088 LUPI_HOST_DEVICE String& operator=(const char*); 0089 LUPI_HOST_DEVICE String& operator=(const String&); 0090 0091 LUPI_HOST_DEVICE String& operator+=(const String&); 0092 LUPI_HOST_DEVICE String& operator+=(const char*); 0093 LUPI_HOST_DEVICE String& operator+=(char); 0094 LUPI_HOST_DEVICE void push_back(char); 0095 0096 friend String 0097 LUPI_HOST_DEVICE operator+(const String& lhs, const String& rhs); 0098 0099 LUPI_HOST_DEVICE bool operator==(const char*) const; 0100 LUPI_HOST_DEVICE bool operator==(const String&) const; 0101 0102 LUPI_HOST_DEVICE void clear(); // set string to empty string (memory remains reserved) 0103 LUPI_HOST_DEVICE void clearMemory(); // set string to empty string (memory is free'd) 0104 0105 LUPI_HOST_DEVICE size_type size() const { return size_; } ///< size without terminating NUL 0106 LUPI_HOST_DEVICE size_type length() const { return size_; } ///< as size() 0107 0108 // size if fully used 0109 LUPI_HOST_DEVICE size_type capacity() const { return allocated_-1; } 0110 0111 // 8 byte alligned size 0112 LUPI_HOST_DEVICE size_t internalSize() const { 0113 size_t delta = allocated_; 0114 size_t sub = delta % 8; 0115 if (sub != 0) delta += (8-sub); 0116 return delta * sizeof(char); 0117 } 0118 0119 LUPI_HOST_DEVICE bool empty() const { return size_ == 0; } 0120 0121 LUPI_HOST_DEVICE const char* c_str() const { return p; } ///< raw data 0122 0123 /** Reserve internal string memory so that n characters can be put into the 0124 string (plus 1 for the NUL char). If there is already enough memory, 0125 nothing happens, if not, the memory will be realloated to exactly this 0126 amount. 0127 */ 0128 LUPI_HOST_DEVICE void reserve( size_type n, char ** address = nullptr); 0129 0130 /** Resize string. If n is less than the current size, the string will be truncated. 0131 If n is larger, then the memory will be reallocated to exactly this amount, and 0132 the additional characters will be NUL characters. 0133 */ 0134 LUPI_HOST_DEVICE void resize( size_type n, char ** address = nullptr); 0135 0136 /** Resize string. If n is less than the current size, the string will be truncated. 0137 If n is larger, then the memory will be reallocated to exactly this amount, and 0138 the additional characters will be c characters. 0139 */ 0140 LUPI_HOST_DEVICE void resize( size_type n, char c, char ** address = nullptr); 0141 0142 /// swap contents 0143 LUPI_HOST_DEVICE void swap( String& ); 0144 0145 LUPI_HOST_DEVICE String substr(const size_type pos, size_type length) const; 0146 0147 // unchecked access: 0148 LUPI_HOST_DEVICE char& operator[](const size_type i) { return p[i]; } 0149 LUPI_HOST_DEVICE char operator[](const size_type i) const { return p[i]; } 0150 // checked access: 0151 LUPI_HOST_DEVICE char& at(const size_type i); 0152 LUPI_HOST_DEVICE char at(const size_type i) const; 0153 0154 /// erase len characters at position pos 0155 LUPI_HOST_DEVICE String& erase(size_type pos, size_type len); 0156 /// Append n characters of a string 0157 LUPI_HOST_DEVICE String& append(const char* str, size_type n); 0158 0159 LUPI_HOST_DEVICE int compare( size_type pos, size_type len, const String& str ) const; 0160 LUPI_HOST_DEVICE int compare( size_type pos, size_type len, const char* str ) const; 0161 0162 private: 0163 // reallocate the internal memory 0164 LUPI_HOST_DEVICE void my_realloc( size_type n, char ** address = nullptr); 0165 LUPI_HOST_DEVICE char* strdup_never_null(const char* other); 0166 0167 }; 0168 // class 0169 0170 LUPI_HOST_DEVICE bool operator<(const String&, const String&); 0171 0172 } 0173 0174 #endif
| [ Source navigation ] | [ Diff markup ] | [ Identifier search ] | [ general search ] |
|
This page was automatically generated by the 2.3.7 LXR engine. The LXR team |
|