AGXDynamics  2.31.1.0
String.h
Go to the documentation of this file.
1 /*
2 Copyright 2007-2021. Algoryx Simulation AB.
3 
4 All AGX source code, intellectual property, documentation, sample code,
5 tutorials, scene files and technical white papers, are copyrighted, proprietary
6 and confidential material of Algoryx Simulation AB. You may not download, read,
7 store, distribute, publish, copy or otherwise disseminate, use or expose this
8 material unless having a written signed agreement with Algoryx Simulation AB, or having been
9 advised so by Algoryx Simulation AB for a time limited evaluation, or having purchased a
10 valid commercial license from Algoryx Simulation AB.
11 
12 Algoryx Simulation AB disclaims all responsibilities for loss or damage caused
13 from using this software, unless otherwise stated in written agreements with
14 Algoryx Simulation AB.
15 */
16 
17 
18 #ifndef AGX_STRING_H
19 #define AGX_STRING_H
20 
21 
22 #include <agx/Integer.h>
23 #include <agx/Real.h>
24 #include <agx/Vector.h>
25 
26 #include <string>
27 
28 
29 namespace agx
30 {
31  template<typename T = std::string> class AgXString;
32 
33 #ifndef SWIG
35 #else
37 #endif
38 
39  template<typename T>
40  class AgXString : public T
41  {
42  public:
43  typedef std::string::size_type size_type;
44  typedef std::string::iterator iterator;
45  typedef std::string::const_iterator const_iterator;
46 
47  public:
51  AgXString();
52 
56  AgXString( const AgXString& str );
57 
61  AgXString( const std::string& str );
62 
66  AgXString( const char* str );
67 
73  AgXString( size_type length, char ch );
74 
81  AgXString( const char* str, size_type length );
82 
89  AgXString( const char* str, size_type index, size_type length );
90 
96  AgXString( const_iterator begin, const_iterator end );
97 
101  AgXString operator + ( const AgXString& other ) const;
102 
106  AgXString operator + ( const char* other ) const;
107 
111  AgXString operator + ( const char elem ) const;
112 
116  AgXString copy() const;
117 
121  AgXString substr( size_type index ) const;
122 
126  AgXString substr( size_type index, size_type length ) const;
127 
132  static AgXString format( const char* format, ... );
133  static AgXString formatVA( const char* format, va_list ap);
134 
135 
139  bool operator== (const std::string& other) const;
140 
144  bool operator== (const char* other) const;
145 
149  bool operator!= (const std::string& other) const;
150 
154  bool operator!= (const char* other) const;
155 
156  /* Pystring extensions */
158  // Copyright (c) 2008, Sony Pictures Imageworks
159  // All rights reserved.
160  //
161  // Redistribution and use in source and binary forms, with or without
162  // modification, are permitted provided that the following conditions are
163  // met:
164  //
165  // Redistributions of source code must retain the above copyright notice,
166  // this list of conditions and the following disclaimer.
167  // Redistributions in binary form must reproduce the above copyright
168  // notice, this list of conditions and the following disclaimer in the
169  // documentation and/or other materials provided with the distribution.
170  // Neither the name of the organization Sony Pictures Imageworks nor the
171  // names of its contributors
172  // may be used to endorse or promote products derived from this software
173  // without specific prior written permission.
174  // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
175  // "AS
176  // IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
177  // TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
178  // PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
179  // OWNER
180  // OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
181  // SPECIAL,
182  // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
183  // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
184  // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
185  // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
186  // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
187  // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
189 
190 #define MAX_32BIT_INT 2147483647
191 
195  AgXString capitalize() const;
196 
200  AgXString center(int width) const;
201 
206  int count(const AgXString& substr, int start = 0, int end = MAX_32BIT_INT) const;
207 
212  bool endswith(const AgXString& suffix, int start = 0, int end = MAX_32BIT_INT) const;
213  bool endswithCI(const AgXString& suffix, int start = 0, int end = MAX_32BIT_INT) const;
214 
219  AgXString expandtabs(int tabsize = 8) const;
220 
226  bool contains(const AgXString& sub, size_t start = 0) const;
227 
231  int index(const AgXString& sub, int start = 0, int end = MAX_32BIT_INT) const;
232 
237  bool isalnum() const;
238 
243  bool isalpha() const;
244 
249  bool isdigit() const;
250 
255  bool islower() const;
256 
261  bool isspace() const;
262 
268  bool istitle() const;
269 
274  bool isupper() const;
275 
280  AgXString ljust(int width) const;
281 
285  AgXString lower() const;
286 
293  AgXString lstrip(const AgXString& chars = "") const;
294 
299  AgXString stripLeadingString(const AgXString& toBeRemoved) const;
300 
307  void partition(const AgXString& sep, StringVector& result ) const;
308 
313  AgXString replace(const AgXString& oldstr, const AgXString& newstr, int count = -1) const;
314 
320  size_t find(const AgXString& sub, int start = 0, int end = MAX_32BIT_INT ) const;
321 
327  size_t rfind(const AgXString& sub, int start = 0, int end = MAX_32BIT_INT ) const;
328 
333  AgXString rjust(int width) const;
334 
341  void rpartition(const AgXString& sep, StringVector& result ) const;
342 
348  AgXString rstrip(const AgXString& chars = "") const;
349 
355  void split(StringVector& result, const AgXString& sep = "", int maxsplit = -1) const;
356 
364  void rsplit(StringVector& result, const AgXString& sep = "", int maxsplit = -1) const;
365 
370  void splitlines(StringVector& result, bool keepends = false ) const;
371 
377  bool startswith(const AgXString& prefix, int start = 0, int end = MAX_32BIT_INT ) const;
378  bool startswithCI(const AgXString& prefix, int start = 0, int end = MAX_32BIT_INT ) const;
379 
385  AgXString strip(const AgXString& chars = "") const;
386 
390  AgXString swapcase() const;
391 
396  AgXString title() const;
397 
403  AgXString translate(const AgXString& table, const AgXString& deletechars = "") const;
404 
408  AgXString upper() const;
409 
414  AgXString zfill(int width) const;
415 
419  AgXString slice(int start = 0, int end = MAX_32BIT_INT) const;
420  };
421 
422 #ifndef SWIG
424 #endif
425 
426  // Utility function to print bit patterns
427  // NOTE: Ignores endianess, handles data as a block of bytes
428  template <typename T>
429  agx::String printBin(const T& val);
430 
431  template <typename T>
432  agx::String printHex(const T& val);
433 
434  agx::String AGXCORE_EXPORT printBin(const void* data, UInt numBytes);
435  agx::String AGXCORE_EXPORT printHex(const void* data, UInt numBytes);
436 
437 
438  // Specializations for basic types, handles endianess
443 
448 
449 } // namespace agx
450 
451 #include <external/pystring/pystring.h>
452 
453 namespace agx
454 {
455 
456  template<typename T>
457  inline AgXString<T> AgXString<T>::format(const char* format, ...)
458  {
459  va_list ap;
460  va_start(ap, format);
461  AgXString<T> result = AgXString<T>::formatVA(format, ap);
462  va_end(ap);
463  return result;
464  }
465 
466  template<typename T>
467  inline AgXString<T> AgXString<T>::formatVA(const char* format, va_list ap)
468  {
469  char* buf = 0;
470 
471 #ifdef _MSC_VER
472  int len = _vscprintf( format, ap ) + 1;
473  buf = (char*) malloc( len * sizeof(char) );
474  if (buf)
475  vsprintf_s( buf, len, format, ap );
476 #else
477  int ret = vasprintf(&buf, format, ap);
478  agxVerifyN(ret >= 0, "Could not allocate memory for formatted string: \'%s\'", format);
479 #endif
480 
481  agxAssert(buf);
482  if (buf) {
483  AgXString<T> result(buf);
484  free(buf);
485 
486  return result;
487  }
488  else
489  return AgXString<T>(); // Fall back.
490  }
491 
492  // Implementation
493 
494  template <typename T>
495  inline AgXString<T>::AgXString() : T()
496  {
497 
498  }
499 
500  template <typename T>
502  : T( str )
503 
504  {
505  }
506 
507  template <typename T>
508  inline AgXString<T>::AgXString( const std::string& str )
509  : T( str )
510  {
511  }
512 
513  template <typename T>
514  inline AgXString<T>::AgXString( const char* str )
515  : T( str )
516  {
517  }
518 
519  template <typename T>
521  : T( length, ch )
522  {
523  }
524 
525  template <typename T>
526  inline AgXString<T>::AgXString( const char* str, size_type length )
527  : T( str, length )
528 
529  {
530  }
531 
532  template <typename T>
533  inline AgXString<T>::AgXString( const char* str, size_type index, size_type length )
534  : T( str, index, length )
535 
536  {
537  }
538 
539  template <typename T>
541  : T( begin, end )
542  {
543  }
544 
545  template <typename T>
547  {
548  AgXString ret = *this;
549  ret += other;
550  return ret;
551  }
552 
553  template <typename T>
554  inline AgXString<T> AgXString<T>::operator + ( const char* other ) const
555  {
556  AgXString ret = *this;
557  ret += other;
558  return ret;
559  }
560 
561  template <typename T>
562  inline AgXString<T> AgXString<T>::operator + ( const char elem ) const
563  {
564  AgXString ret = *this;
565  ret += elem;
566  return ret;
567  }
568 
569  template <typename T>
571  {
572  return AgXString( *this );
573  }
574 
575  template <typename T>
577  {
578  return AgXString( this->c_str(), index, this->size() );
579  }
580 
581  template <typename T>
583  {
584  return AgXString( this->c_str(), index, length );
585  }
586 
587  template <typename T>
588  AGX_FORCE_INLINE bool AgXString<T>::operator== (const std::string& other) const
589  {
590  return *(std::string*)this == other;
591  }
592 
593  template <typename T>
594  AGX_FORCE_INLINE bool AgXString<T>::operator== (const char* other) const
595  {
596  return *(std::string*)this == other;
597  }
598 
599  template <typename T>
600  AGX_FORCE_INLINE bool AgXString<T>::operator!= (const std::string& other) const
601  {
602  return *(std::string*)this != other;
603  }
604 
605  template <typename T>
606  AGX_FORCE_INLINE bool AgXString<T>::operator!= (const char* other) const
607  {
608  return *(std::string*)this != other;
609  }
610 
611 
612 
613  template<typename T>
614  AgXString<T> operator + ( const char* str1, const AgXString<T>& str2 )
615  {
616  return AgXString<T>( str1 ) + str2;
617  }
618 
619  template<typename T>
620  AgXString<T> operator + ( const char elem, const AgXString<T>& str )
621  {
622  return AgXString<T>( 1, elem ) + str;
623  }
624 
625  template<typename T>
626  inline bool AgXString<T>::contains(const AgXString<T>& sub, size_t start) const
627  {
628  return this->find(sub, int(start)) != AgXString<T>::npos;
629  }
630 
631 
632  template<typename T>
634  {
635  return pystring::capitalize(*this);
636  }
637 
638  template<typename T>
640  {
641  return pystring::center(*this, width);
642  }
643 
644  template<typename T>
645  int AgXString<T>::count(const AgXString<T>& substr, int start, int end) const
646  {
647  return pystring::count(*this, substr, start, end);
648  }
649 
650  template<typename T>
651  bool AgXString<T>::endswith(const AgXString<T>& suffix, int start, int end) const
652  {
653  return pystring::endswith(*this, suffix, start, end);
654  }
655 
656  template<typename T>
657  bool AgXString<T>::endswithCI(const AgXString<T>& suffix, int start, int end) const
658  {
659  return pystring::endswithCI(*this, suffix, start, end);
660  }
661 
662  template<typename T>
664  {
665  return pystring::expandtabs(*this, tabsize);
666  }
667 
668  /*
669  int AgXString<T>::find(const AgXString<T>& sub, int start, int end) const
670  {
671  return pystring::find(*this, sub, start, end);
672  }
673  */
674 
675  template<typename T>
676  int AgXString<T>::index(const AgXString<T>& sub, int start, int end) const
677  {
678  return pystring::index(*this, sub, start, end);
679  }
680 
681  template<typename T>
683  {
684  return pystring::isalnum(*this);
685  }
686 
687  template<typename T>
689  {
690  return pystring::isalpha(*this);
691  }
692 
693  template<typename T>
695  {
696  return pystring::isdigit(*this);
697  }
698 
699  template<typename T>
701  {
702  return pystring::islower(*this);
703  }
704 
705  template<typename T>
707  {
708  return pystring::isspace(*this);
709  }
710 
711  template<typename T>
713  {
714  return pystring::istitle(*this);
715  }
716 
717  template<typename T>
719  {
720  return pystring::isupper(*this);
721  }
722 
723 
724  template<typename T>
726  {
727  return pystring::ljust(*this, width);
728  }
729 
730  template<typename T>
732  {
733  return pystring::lower(*this);
734  }
735 
736 
737  template<typename T>
739  {
740  std::size_t start = this->find(toBeRemoved); // Find the string to be removed
741  if (start == 0 ) {// Did we find it, and its from the start of the string?
742  if (T::length() > toBeRemoved.size()) {
743  return this->substr(toBeRemoved.size(), T::length() ); // Remove it.
744  } else
745  return "";
746  }
747  return *this;
748  }
749 
750 
751  template<typename T>
753  {
754  return pystring::lstrip(*this, chars);
755  }
756 
757  template<typename T>
758  void AgXString<T>::partition(const AgXString<T>& sep, StringVector& result ) const
759  {
760  return pystring::partition(*this, sep, result);
761  }
762 
763  template<typename T>
764  AgXString<T> AgXString<T>::replace(const AgXString<T>& oldstr, const AgXString<T>& newstr, int count) const
765  {
766  return pystring::replace(*this, oldstr, newstr, count);
767  }
768 
769  template<typename T>
770  size_t AgXString<T>::find(const AgXString<T>& sub, int start, int end) const
771  {
772  int index = pystring::find(*this, sub, start, end);
773  return index < 0 ? AgXString<T>::npos : (size_t)index;
774  }
775 
776  template<typename T>
777  size_t AgXString<T>::rfind(const AgXString<T>& sub, int start, int end) const
778  {
779  int index = pystring::rfind(*this, sub, start, end);
780  return index < 0 ? AgXString<T>::npos : (size_t)index;
781  }
782 
783 
784  template<typename T>
786  {
787  return pystring::rjust(*this, width);
788  }
789 
790  template<typename T>
791  void AgXString<T>::rpartition(const AgXString<T>& sep, StringVector& result ) const
792  {
793  return pystring::rpartition(*this, sep, result);
794  }
795 
796  template<typename T>
798  {
799  return pystring::rstrip(*this, chars);
800  }
801 
802  template<typename T>
803  void AgXString<T>::split(StringVector& result, const AgXString<T>& sep, int maxsplit) const
804  {
805  return pystring::split(*this, result, sep, maxsplit);
806  }
807 
808  template<typename T>
809  void AgXString<T>::rsplit(StringVector& result, const AgXString<T>& sep, int maxsplit) const
810  {
811  return pystring::rsplit(*this, result, sep, maxsplit);
812  }
813 
814  template<typename T>
815  void AgXString<T>::splitlines(StringVector& result, bool keepends) const
816  {
817  return pystring::splitlines(*this, result, keepends);
818  }
819 
820  template<typename T>
821  bool AgXString<T>::startswith(const AgXString<T>& prefix, int start, int end) const
822  {
823  return pystring::startswith(*this, prefix, start, end);
824  }
825 
826  template<typename T>
827  bool AgXString<T>::startswithCI(const AgXString<T>& prefix, int start, int end) const
828  {
829  return pystring::startswithCI(*this, prefix, start, end);
830  }
831 
832  template<typename T>
834  {
835  if (this->size() == 0)
836  return *this;
837 
838  return pystring::strip(*this, chars);
839  }
840 
841  template<typename T>
843  {
844  return pystring::swapcase(*this);
845  }
846 
847  template<typename T>
849  {
850  return pystring::title(*this);
851  }
852 
853  template<typename T>
854  AgXString<T> AgXString<T>::translate(const AgXString<T>& table, const AgXString<T>& deletechars) const
855  {
856  return pystring::translate(*this, table, deletechars);
857  }
858 
859  template<typename T>
861  {
862  return pystring::upper(*this);
863  }
864 
865  template<typename T>
867  {
868  return pystring::zfill(*this, width);
869  }
870 
871  template<typename T>
872  AgXString<T> AgXString<T>::slice(int start, int end) const
873  {
874  return pystring::slice(*this, start, end);
875  }
876 
877 
878  template <typename T>
879  agx::String printBin(const T& val)
880  {
881  return printBin(&val, sizeof(T));
882  }
883 
884  template <typename T>
885  agx::String printHex(const T& val)
886  {
887  return printHex(&val, sizeof(T));
888  }
889 
890 }
891 
892 namespace std
893 {
895  {
896  std::swap(static_cast<std::string&>(lhs), static_cast<std::string&>(rhs));
897  }
898 }
899 
900 
901 #endif /* _AGX_STRING_H_ */
#define MAX_32BIT_INT
Definition: String.h:190
void partition(const AgXString &sep, StringVector &result) const
Split the string around first occurrence of sep.
Definition: String.h:758
AgXString capitalize() const
Definition: String.h:633
float Real32
Definition: Real.h:44
void rpartition(const AgXString &sep, StringVector &result) const
Split the string around last occurrence of sep.
Definition: String.h:791
double Real64
Definition: Real.h:45
bool operator==(const std::string &other) const
Definition: String.h:588
bool istitle() const
Definition: String.h:712
AgXString strip(const AgXString &chars="") const
Definition: String.h:833
AgXString expandtabs(int tabsize=8) const
Definition: String.h:663
bool isalpha() const
Definition: String.h:688
bool islower() const
Definition: String.h:700
#define agxVerifyN(expr, format,...)
Definition: debug.h:133
bool isalnum() const
Definition: String.h:682
AgXString replace(const AgXString &oldstr, const AgXString &newstr, int count=-1) const
Definition: String.h:764
agx::String AGXCORE_EXPORT printHex(Real64 val)
bool operator==(T val, InvalidIndexStruct)
Definition: agx.h:191
std::string::iterator iterator
Definition: String.h:44
STL namespace.
AgXString translate(const AgXString &table, const AgXString &deletechars="") const
Definition: String.h:854
AgXString< std::string > String
Definition: String.h:423
size_t rfind(const AgXString &sub, int start=0, int end=MAX_32BIT_INT) const
Definition: String.h:777
GLenum GLuint GLenum GLsizei length
Definition: gl.h:5539
AgXString upper() const
Definition: String.h:860
int index(const AgXString &sub, int start=0, int end=MAX_32BIT_INT) const
Synonym of find right now.
Definition: String.h:676
void swap(agx::Name &lhs, agx::Name &rhs)
Definition: Name.h:323
#define AGX_FORCE_INLINE
Definition: Object.h:63
AgXString stripLeadingString(const AgXString &toBeRemoved) const
If this string begins with toBeRemoved, it will be removed and returned.
Definition: String.h:738
size_t find(const AgXString &sub, int start=0, int end=MAX_32BIT_INT) const
Definition: String.h:770
void swap(agx::String &lhs, agx::String &rhs)
Definition: String.h:894
AgXString operator+(const AgXString &other) const
Operator +, AgXString b = thisAgXString + other.
Definition: String.h:546
Vector< AgXString< std::string > > StringVector
Definition: String.h:31
Templated vector class.
Definition: Vector.h:52
AgXString slice(int start=0, int end=MAX_32BIT_INT) const
function matching python&#39;s slice functionality.
Definition: String.h:872
agx::String printBin(const T &val)
Definition: String.h:879
std::string::size_type size_type
Definition: String.h:43
uint32_t UInt
Definition: Integer.h:34
std::string::const_iterator const_iterator
Definition: String.h:45
AgXString swapcase() const
Definition: String.h:842
Namespace agx contains the dynamics/math part of the AGX Dynamics API.
bool endswithCI(const AgXString &suffix, int start=0, int end=MAX_32BIT_INT) const
Definition: String.h:657
AgXString substr(size_type index) const
Definition: String.h:576
LinearProbingHashSetImplementation< KeyT, HashT >::iterator begin(LinearProbingHashSetImplementation< KeyT, HashT > &set)
AgXString zfill(int width) const
Definition: String.h:866
AgXString rstrip(const AgXString &chars="") const
Definition: String.h:797
int count(const AgXString &substr, int start=0, int end=MAX_32BIT_INT) const
Definition: String.h:645
AgXString lower() const
Definition: String.h:731
#define agxAssert(expr)
Definition: debug.h:143
bool isupper() const
Definition: String.h:718
AgXString center(int width) const
Definition: String.h:639
bool endswith(const AgXString &suffix, int start=0, int end=MAX_32BIT_INT) const
Definition: String.h:651
agx::String AGXCORE_EXPORT printBin(Real64 val)
uint64_t UInt64
Definition: Integer.h:41
bool isspace() const
Definition: String.h:706
static AgXString formatVA(const char *format, va_list ap)
Definition: String.h:467
LinearProbingHashSetImplementation< KeyT, HashT >::iterator end(LinearProbingHashSetImplementation< KeyT, HashT > &set)
bool operator!=(T val, InvalidIndexStruct)
Definition: agx.h:197
AgXString lstrip(const AgXString &chars="") const
Definition: String.h:752
void splitlines(StringVector &result, bool keepends=false) const
Definition: String.h:815
AgXString rjust(int width) const
Definition: String.h:785
AgXString< T > operator+(const char elem, const AgXString< T > &str)
Definition: String.h:620
AgXString title() const
Definition: String.h:848
void split(StringVector &result, const AgXString &sep="", int maxsplit=-1) const
Fills the "result" list with the words in the string, using sep as the delimiter string.
Definition: String.h:803
uint32_t UInt32
Definition: Integer.h:40
bool operator!=(const std::string &other) const
Definition: String.h:600
agx::String printHex(const T &val)
Definition: String.h:885
#define AGXCORE_EXPORT
bool contains(const AgXString &sub, size_t start=0) const
Definition: String.h:626
static AgXString format(const char *format,...)
C printf formatting of a string.
Definition: String.h:457
AgXString ljust(int width) const
Definition: String.h:725
AgXString()
Default constructor.
Definition: String.h:495
bool startswith(const AgXString &prefix, int start=0, int end=MAX_32BIT_INT) const
Definition: String.h:821
AgXString copy() const
Copy method, if implementation of this class is changed.
Definition: String.h:570
bool startswithCI(const AgXString &prefix, int start=0, int end=MAX_32BIT_INT) const
Definition: String.h:827
bool isdigit() const
Definition: String.h:694
void rsplit(StringVector &result, const AgXString &sep="", int maxsplit=-1) const
Fills the "result" list with the words in the string, using sep as the delimiter string.
Definition: String.h:809