NTrace
GPU ray tracing framework
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
String.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2009-2011, NVIDIA Corporation
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  * * Redistributions of source code must retain the above copyright
8  * notice, this list of conditions and the following disclaimer.
9  * * Redistributions in binary form must reproduce the above copyright
10  * notice, this list of conditions and the following disclaimer in the
11  * documentation and/or other materials provided with the distribution.
12  * * Neither the name of NVIDIA Corporation nor the
13  * names of its contributors may be used to endorse or promote products
14  * derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19  * DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
20  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 #pragma once
29 #include "base/Array.hpp"
30 
31 #include <stdarg.h>
32 
33 namespace FW
34 {
35 //------------------------------------------------------------------------
36 
37 class String
38 {
39 public:
40  String (void) {}
41  String (char chr) { set(chr); }
42  String (const char* chars) { set(chars); }
43  String (const char* start, const char* end ) { set(start, end); }
44  String (const String& other) { set(other); }
45  String (S32 value) { setf("%d", value); }
46  String (F64 value) { setf("%g", value); }
47  ~String (void) {}
48 
49  int getLength (void) const { return max(m_chars.getSize() - 1, 0); }
50  char getChar (int idx) const { FW_ASSERT(idx < getLength()); return m_chars[idx]; }
51  const char* getPtr (void) const { return (m_chars.getSize()) ? m_chars.getPtr() : ""; }
52 
53  String& reset (void) { m_chars.reset(); return *this; }
54  String& set (char chr);
55  String& set (const char* chars);
56  String& set (const char* start, const char* end);
57  String& set (const String& other) { m_chars = other.m_chars; return *this; }
58  String& setf (const char* fmt, ...);
59  String& setfv (const char* fmt, va_list args);
60 
61  String substring (int start, int end) const;
62  String substring (int start) const { return substring(start, getLength()); }
63 
64  String trimStart (void) const;
65  String trimEnd (void) const;
66  String trim (void) const;
67 
68  void split (char chr, Array<String>& pieces, bool includeEmpty = false) const;
69 
70  String& clear (void) { m_chars.clear(); }
71  String& append (char chr);
72  String& append (const char* chars);
73  String& append (const String& other);
74  String& appendf (const char* fmt, ...);
75  String& appendfv (const char* fmt, va_list args);
76  String& compact (void) { m_chars.compact(); }
77 
78  int indexOf (char chr) const { return m_chars.indexOf(chr); }
79  int indexOf (char chr, int fromIdx) const { return m_chars.indexOf(chr, fromIdx); }
80  int lastIndexOf (char chr) const { return m_chars.lastIndexOf(chr); }
81  int lastIndexOf (char chr, int fromIdx) const { return m_chars.lastIndexOf(chr, fromIdx); }
82 
83  String toUpper (void) const;
84  String toLower (void) const;
85  bool startsWith (const String& str) const;
86  bool endsWith (const String& str) const;
87 
88  String getFileName (void) const;
89  String getDirName (void) const;
90 
91  char operator[] (int idx) const { return getChar(idx); }
92  String& operator= (const String& other) { set(other); return *this; }
93  String& operator+= (char chr) { append(chr); return *this; }
94  String& operator+= (const String& other) { append(other); return *this; }
95  String operator+ (char chr) const { return String(*this).append(chr); }
96  String operator+ (const String& other) const { return String(*this).append(other); }
97  bool operator== (const char* chars) const { return (strcmp(getPtr(), chars) == 0); }
98  bool operator== (const String& other) const { return (strcmp(getPtr(), other.getPtr()) == 0); }
99  bool operator!= (const char* chars) const { return (strcmp(getPtr(), chars) != 0); }
100  bool operator!= (const String& other) const { return (strcmp(getPtr(), other.getPtr()) != 0); }
101  bool operator< (const char* chars) const { return (strcmp(getPtr(), chars) < 0); }
102  bool operator< (const String& other) const { return (strcmp(getPtr(), other.getPtr()) < 0); }
103  bool operator> (const char* chars) const { return (strcmp(getPtr(), chars) > 0); }
104  bool operator> (const String& other) const { return (strcmp(getPtr(), other.getPtr()) > 0); }
105  bool operator>= (const char* chars) const { return (strcmp(getPtr(), chars) <= 0); }
106  bool operator>= (const String& other) const { return (strcmp(getPtr(), other.getPtr()) <= 0); }
107  bool operator<= (const char* chars) const { return (strcmp(getPtr(), chars) >= 0); }
108  bool operator<= (const String& other) const { return (strcmp(getPtr(), other.getPtr()) >= 0); }
109 
110 private:
111  static int strlen (const char* chars);
112  static int strcmp (const char* a, const char* b);
113 
114 private:
115  Array<char> m_chars;
116 };
117 
118 //------------------------------------------------------------------------
119 
120 String getDateString (void);
121 
122 bool parseSpace (const char*& ptr);
123 bool parseChar (const char*& ptr, char chr);
124 bool parseLiteral (const char*& ptr, const char* str);
125 bool parseInt (const char*& ptr, S32& value);
126 bool parseInt (const char*& ptr, S64& value);
127 bool parseHex (const char*& ptr, U32& value);
128 bool parseFloat (const char*& ptr, F32& value);
129 bool parseVec3f (const char*& ptr, Vec3f& value);
130 
131 //------------------------------------------------------------------------
132 }
String trimEnd(void) const
Definition: String.cpp:120
char getChar(int idx) const
Definition: String.hpp:50
String & append(char chr)
Definition: String.cpp:173
String substring(int start, int end) const
Definition: String.cpp:96
bool endsWith(const String &str) const
Definition: String.cpp:273
const char * getPtr(void) const
Definition: String.hpp:51
String & setf(const char *fmt,...)
Definition: String.cpp:72
String getFileName(void) const
Definition: String.cpp:284
String & appendfv(const char *fmt, va_list args)
Definition: String.cpp:218
int indexOf(char chr) const
Definition: String.hpp:78
bool operator<(const char *chars) const
Definition: String.hpp:101
String(F64 value)
Definition: String.hpp:46
void ** ptr
Definition: DLLImports.cpp:74
bool parseVec3f(const char *&ptr, Vec3f &value)
Definition: String.cpp:503
String(char chr)
Definition: String.hpp:41
String & setfv(const char *fmt, va_list args)
Definition: String.cpp:83
double F64
Definition: Defs.hpp:90
char operator[](int idx) const
Definition: String.hpp:91
int lastIndexOf(char chr, int fromIdx) const
Definition: String.hpp:81
bool operator!=(const char *chars) const
Definition: String.hpp:99
void clear(void)
Definition: Array.hpp:359
String(const char *start, const char *end)
Definition: String.hpp:43
String toLower(void) const
Definition: String.cpp:245
int lastIndexOf(char chr) const
Definition: String.hpp:80
bool operator==(const char *chars) const
Definition: String.hpp:97
String & reset(void)
Definition: String.hpp:53
bool parseLiteral(const char *&ptr, const char *str)
Definition: String.cpp:365
String & operator+=(char chr)
Definition: String.hpp:93
void reset(S size=0)
Definition: Array.hpp:317
String operator+(char chr) const
Definition: String.hpp:95
float F32
Definition: Defs.hpp:89
String(const String &other)
Definition: String.hpp:44
String & set(char chr)
Definition: String.cpp:38
FW_CUDA_FUNC T max(const VectorBase< T, L, S > &v)
Definition: Math.hpp:462
#define FW_ASSERT(X)
Definition: Defs.hpp:67
bool operator>=(const char *chars) const
Definition: String.hpp:105
signed int S32
Definition: Defs.hpp:88
int getLength(void) const
Definition: String.hpp:49
bool operator<=(const char *chars) const
Definition: String.hpp:107
String getDirName(void) const
Definition: String.cpp:292
String trimStart(void) const
Definition: String.cpp:109
bool parseInt(const char *&ptr, S32 &value)
Definition: String.cpp:384
bool operator>(const char *chars) const
Definition: String.hpp:103
String & set(const String &other)
Definition: String.hpp:57
signed __int64 S64
Definition: Defs.hpp:98
unsigned int U32
Definition: Defs.hpp:85
String(const char *chars)
Definition: String.hpp:42
CUdevice int ordinal char int CUdevice dev CUdevprop CUdevice dev CUcontext ctx CUcontext ctx CUcontext pctx CUmodule const void image CUmodule const void fatCubin CUfunction CUmodule const char name void p CUfunction unsigned int bytes CUtexref pTexRef CUtexref CUarray unsigned int Flags CUtexref int CUaddress_mode am CUtexref unsigned int Flags CUaddress_mode CUtexref int dim CUarray_format int CUtexref hTexRef CUfunction unsigned int numbytes CUfunction int float value
Definition: DLLImports.inl:84
String & operator=(const String &other)
Definition: String.hpp:92
String & clear(void)
Definition: String.hpp:70
String trim(void) const
Definition: String.cpp:131
bool parseFloat(const char *&ptr, F32 &value)
Definition: String.cpp:440
String & appendf(const char *fmt,...)
Definition: String.cpp:207
S lastIndexOf(const T &item) const
Definition: Array.hpp:553
~String(void)
Definition: String.hpp:47
bool startsWith(const String &str) const
Definition: String.cpp:261
bool parseSpace(const char *&ptr)
Definition: String.cpp:344
void compact(void)
Definition: Array.hpp:335
void split(char chr, Array< String > &pieces, bool includeEmpty=false) const
Definition: String.cpp:157
String & compact(void)
Definition: String.hpp:76
bool parseHex(const char *&ptr, U32 &value)
Definition: String.cpp:418
String substring(int start) const
Definition: String.hpp:62
int indexOf(char chr, int fromIdx) const
Definition: String.hpp:79
const T * getPtr(S idx=0) const
Definition: Array.hpp:202
String(S32 value)
Definition: String.hpp:45
String getDateString(void)
Definition: String.cpp:323
S getSize(void) const
Definition: Array.hpp:188
bool parseChar(const char *&ptr, char chr)
Definition: String.cpp:354
S indexOf(const T &item, S fromIdx=0) const
Definition: Array.hpp:543
String(void)
Definition: String.hpp:40
String toUpper(void) const
Definition: String.cpp:229