VR-Link API Documentation for DIS
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
vlString.h
Go to the documentation of this file.
1 /****************************************************************************
2  * Copyright (c) 1992-2026 MAK Technologies, Inc
3  * All rights reserved.
4  ****************************************************************************/
5 
9 
10 #pragma once
11 
12 #include <vlutil/vlMachineTypes.h>
13 #include <vector>
14 #include <iosfwd>
15 #include <cstddef>
16 #include <cstdint>
17 
18 #if _MSC_VER >= 1920 //VC16 2019
19 #include <xstring>
20 #endif
21 
22 #if __has_include(<compare>)
23 # if defined(__cpp_lib_three_way_comparison) && __cpp_lib_three_way_comparison >= 201907
24 # include <compare>
25 # define DtSPACESHIP_OPERATOR_IS_SUPPORTED 1
26 # endif
27 #endif
28 
29 #ifdef DtUSE_UTILITIES_NAMESPACE
30 namespace DtUSE_UTILITIES_NAMESPACE
31 {
32 #endif
33 
39  {
40  public:
41 
43  DtString();
44 
46  DtString( const char* str );
47 
49  DtString( const char* str, int length );
50 
52  DtString( const std::string& str );
53 
55  DtString( const DtString& orig );
56 
63 
64  DtString( double val );
65  DtString( int val );
66  DtString( long val );
67  DtString( char val );
68  DtString( unsigned int val );
69  DtString( unsigned long val );
70 
72 
74  virtual ~DtString();
75 
78 
79  virtual DtString& operator = ( const char* orig );
80  virtual DtString& operator = ( const DtString& orig );
81 
83 
86  [[deprecated("Use c_str() instead.")]] virtual operator const char*() const;
87 
89  virtual DtString* clone() const;
90 
93 
94  friend DT_DLL_VLUTIL bool operator < ( const DtString& str1, const DtString& str2 );
95  friend DT_DLL_VLUTIL bool operator > ( const DtString& str1, const DtString& str2 );
96  friend DT_DLL_VLUTIL bool operator == ( const DtString& str1, const DtString& str2 );
97  friend DT_DLL_VLUTIL bool operator != ( const DtString& str1, const DtString& str2 );
98  friend DT_DLL_VLUTIL bool operator <= ( const DtString& str1, const DtString& str2 );
99  friend DT_DLL_VLUTIL bool operator >= ( const DtString& str1, const DtString& str2 );
100 
101  virtual bool operator < ( const char* str ) const;
102  virtual bool operator > ( const char* str ) const;
103  virtual bool operator == ( const char* str ) const;
104  virtual bool operator != ( const char* str ) const;
105  virtual bool operator <= ( const char* str ) const;
106  virtual bool operator >= ( const char* str ) const;
107 
109 
113 
114  virtual bool caseCompare(const char* str) const;
115  virtual bool caseCompare(const DtString& str) const;
116 
118 
121 
122  friend DT_DLL_VLUTIL DtString operator + ( const char* str1, const DtString& str2 );
123  friend DT_DLL_VLUTIL DtString operator + ( const DtString& str1, const DtString& str2 );
124 
125  virtual DtString operator + ( const char* str ) const;
126  virtual DtString& operator += ( const char* str );
127  virtual DtString& operator += ( const DtString& str );
128 
130 
133 
134  virtual char& operator [] ( int index );
135  virtual const char& operator [] ( int index ) const;
136 
138 
145 
146  virtual bool snipFront( const char ch, bool toLastCh = false );
147  virtual bool snipBack( const char ch, bool fromFirstCh = false );
149 
151 
152  virtual bool chompFront();
153  virtual bool chompBack();
155 
158  virtual void tokenize( const char delimiter, std::vector<DtString>& tokens ) const;
159 
163  virtual int toInt( bool* ok = 0 ) const;
164 
168  virtual unsigned int toUInt( bool* ok = 0 ) const;
169 
173  virtual float toFloat( bool* ok = 0 ) const;
174 
178  virtual double toDouble( bool* ok = 0 ) const;
179 
183  virtual bool toBoolean( bool* ok = 0 ) const;
184 
186  virtual void toUpper();
187 
189  virtual void toLower();
190 
194  virtual int findChar( const char ch );
195 
199  virtual int findLastChar( const char ch );
200 
204  virtual int findString( const DtString& string ) const;
205 
209  virtual int findLastString( const DtString& string );
210 
211  bool snipFrontToLast( const char ch )
212  {
213  return snipFront( ch, true );
214  }
215 
216  bool snipBackToFirst( const char ch )
217  {
218  return snipBack( ch, true );
219  }
220 
222  virtual int count( const char ch ) const;
223 
226  virtual int length() const;
227 
229  virtual int size() const;
230 
232  virtual std::string stdString() const;
233 
236  virtual const char* string() const;
237 
239  virtual const char* c_str() const;
240 
242  virtual bool isEmpty() const;
243 
246  virtual DtString section(const char sep, int start, int end = -1) const;
247 
248  public:
249 
251  static const DtString& nullString();
252 
255  static const DtString& nullId();
256 
257  protected:
258 
259  friend DT_DLL_VLUTIL std::ostream& operator << ( std::ostream& os, const DtString& str );
260  friend DT_DLL_VLUTIL std::istream& operator >> ( std::istream& is, DtString& str );
261 
266  void clear();
267 
270  void setString( const char* str );
271 
272  protected:
273 
274  enum
275  {
276  theDefaultBufferSize = 64
277  };
278 
279  char myDefaultBuffer[theDefaultBufferSize];
280  char* myString;
281  bool myStringAlloced;
283  unsigned int myBufSize;
284 
285  static const int theMaxInsertionStringSize;
286 
287  };
288 
289 
290  inline bool DtString::isEmpty() const
291  {
292  return (myString[0] == '\0');
293  }
294 
295 #if DtSPACESHIP_OPERATOR_IS_SUPPORTED
296  // Note: Since DtString implicitly casts to const char*, this operator is necessary to prevent
297  // the pointer version of <=> from being used which will compare memory addresses instead of string contents.
298  inline std::strong_ordering operator<=> (const DtString& s1, const DtString& s2)
299  {
300  if (s1 == s2)
301  {
302  return std::strong_ordering::equal;
303  }
304  if (s1 < s2)
305  {
306  return std::strong_ordering::less;
307  }
308  return std::strong_ordering::greater;
309  }
310 #endif
311 
312 #ifdef DtUSE_UTILITIES_NAMESPACE
313 }
314 #endif
315 
316 // In the RTI, the hash struct must use the namespace type which has to be done outside of the namespace
317 #ifdef DtUSE_UTILITIES_NAMESPACE
318 #define DtSTRING_HASH_TYPE MAKRti::DtString
319 #else
320 #define DtSTRING_HASH_TYPE DtString
321 #endif
322 
323 template<>
324 struct std::hash<DtSTRING_HASH_TYPE>
325 {
326  std::size_t operator()(const DtSTRING_HASH_TYPE& s) const noexcept
327  {
328  // FNV-1a 64-bit hash over the characters in DtString
329  const char* data = s.c_str();
330  const std::size_t length = static_cast<std::size_t>(s.size());
331 
332  std::uint64_t hash = 1469598103934665603ull; // offset basis
333  const std::uint64_t prime = 1099511628211ull; // FNV prime
334 
335  for (std::size_t i = 0; i < length; ++i)
336  {
337  hash ^= static_cast<unsigned char>(data[i]);
338  hash *= prime;
339  }
340 
341  return static_cast<std::size_t>(hash);
342  }
343 };
std::size_t operator()(const DtSTRING_HASH_TYPE &s) const noexcept
Definition: vlString.h:326
const bool operator==(const DtU128 &lhs, const DtU128 &rhs)
virtual bool isEmpty() const
returns true if the string is empty, where empty is size() == 0
Definition: vlString.h:290
bool snipFrontToLast(const char ch)
Definition: vlString.h:211
DtVector operator+(const DtVector &v1, const DtVector &v2)
implements v1 + v2
std::ostream & operator<<(std::ostream &os, const Dt3dChord &chord)
std::istream & operator>>(std::istream &is, Dt3dExtent &extent)
bool snipBackToFirst(const char ch)
Definition: vlString.h:216
char * myString
Use this buffer if possible.
Definition: vlString.h:280
unsigned int myBufSize
Does myString need to be deleted when done?
Definition: vlString.h:283
const bool operator!=(const DtU128 &lhs, const DtU128 &rhs)
#define DtSTRING_HASH_TYPE
Definition: vlString.h:320
Instances of DtString are used to represent strings.
Definition: vlString.h:38
const bool operator<(const DtU128 &lhs, const DtU128 &rhs)
This file contains typedefs for machine dependant types.
const bool operator>(const DtU128 &lhs, const DtU128 &rhs)
static const int theMaxInsertionStringSize
current buffer size
Definition: vlString.h:285
#define DT_DLL_VLUTIL
Definition: vlMachineTypes.h:121

Document ID: Generated on Mon Sep 28 00:09:32 EDT 2026 from SVN revision 295353
Copyright © 1992-2025 MAK Technologies. All Rights Reserved (www.mak.com)