AGX Dynamics  2.35.0.1
String.h
Go to the documentation of this file.
1 /*
2 Copyright 2007-2022. 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:
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 
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 
122 
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 
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 
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 
434 
435  // Utility function to print bit patterns
436  // NOTE: Ignores endianess, handles data as a block of bytes
437  template <typename T>
438  agx::String printBin(const T& val);
439 
440  template <typename T>
441  agx::String printHex(const T& val);
442 
443  agx::String AGXCORE_EXPORT printBin(const void* data, UInt numBytes);
444  agx::String AGXCORE_EXPORT printHex(const void* data, UInt numBytes);
445 
446 
447  // Specializations for basic types, handles endianess
452 
457 
458 } // namespace agx
459 
460 
461 #include <external/pystring/pystring.h>
462 
463 namespace agx
464 {
465 
466  template<typename T>
467  inline AgXString<T> AgXString<T>::format(const char* format, ...)
468  {
469  va_list ap;
470  va_start(ap, format);
471  AgXString<T> result = AgXString<T>::formatVA(format, ap);
472  va_end(ap);
473  return result;
474  }
475 
476  template<typename T>
477  inline AgXString<T> AgXString<T>::formatVA(const char* format, va_list ap)
478  {
479  char* buf = 0;
480 
481 #ifdef _MSC_VER
482  int len = _vscprintf( format, ap ) + 1;
483  buf = (char*) malloc( len * sizeof(char) );
484  if (buf)
485  vsprintf_s( buf, len, format, ap );
486 #else
487  int ret = vasprintf(&buf, format, ap);
488  agxVerifyN(ret >= 0, "Could not allocate memory for formatted string: \'%s\'", format);
489 #endif
490 
491  agxAssert(buf);
492  if (buf) {
493  AgXString<T> result(buf);
494  free(buf);
495 
496  return result;
497  }
498  else
499  return AgXString<T>(); // Fall back.
500  }
501 
502  // Implementation
503 
504  template <typename T>
505  inline AgXString<T>::AgXString() : T()
506  {
507 
508  }
509 
510  template <typename T>
512  : T( str )
513 
514  {
515  }
516 
517  template <typename T>
518  inline AgXString<T>::AgXString( const std::string& str )
519  : T( str )
520  {
521  }
522 
523  template <typename T>
524  inline AgXString<T>::AgXString( const char* str )
525  : T( str )
526  {
527  }
528 
529  template <typename T>
530  inline AgXString<T>::AgXString( size_type length, char ch )
531  : T( length, ch )
532  {
533  }
534 
535  template <typename T>
536  inline AgXString<T>::AgXString( const char* str, size_type length )
537  : T( str, length )
538 
539  {
540  }
541 
542  template <typename T>
543  inline AgXString<T>::AgXString( const char* str, size_type index, size_type length )
544  : T( str, index, length )
545 
546  {
547  }
548 
549  template <typename T>
551  : T( begin, end )
552  {
553  }
554 
555  template <typename T>
557  {
558  AgXString ret = *this;
559  ret += other;
560  return ret;
561  }
562 
563  template <typename T>
564  inline AgXString<T> AgXString<T>::operator + ( const char* other ) const
565  {
566  AgXString ret = *this;
567  ret += other;
568  return ret;
569  }
570 
571  template <typename T>
572  inline AgXString<T> AgXString<T>::operator + ( const char elem ) const
573  {
574  AgXString ret = *this;
575  ret += elem;
576  return ret;
577  }
578 
579  template <typename T>
581  {
582  return AgXString( *this );
583  }
584 
585  template <typename T>
587  {
588  return AgXString( this->c_str(), index, this->size() );
589  }
590 
591  template <typename T>
592  inline AgXString<T> AgXString<T>::substr( size_type index, size_type length ) const
593  {
594  return AgXString( this->c_str(), index, length );
595  }
596 
597  template <typename T>
598  AGX_FORCE_INLINE bool AgXString<T>::operator== (const std::string& other) const
599  {
600  return *(std::string*)this == other;
601  }
602 
603  template <typename T>
604  AGX_FORCE_INLINE bool AgXString<T>::operator== (const char* other) const
605  {
606  return *(std::string*)this == other;
607  }
608 
609  template <typename T>
610  AGX_FORCE_INLINE bool AgXString<T>::operator!= (const std::string& other) const
611  {
612  return *(std::string*)this != other;
613  }
614 
615  template <typename T>
616  AGX_FORCE_INLINE bool AgXString<T>::operator!= (const char* other) const
617  {
618  return *(std::string*)this != other;
619  }
620 
621 
622 
623  template<typename T>
624  AgXString<T> operator + ( const char* str1, const AgXString<T>& str2 )
625  {
626  return AgXString<T>( str1 ) + str2;
627  }
628 
629  template<typename T>
630  AgXString<T> operator + ( const char elem, const AgXString<T>& str )
631  {
632  return AgXString<T>( 1, elem ) + str;
633  }
634 
635  template<typename T>
636  inline bool AgXString<T>::contains(const AgXString<T>& sub, size_t start) const
637  {
638  return this->find(sub, int(start)) != AgXString<T>::npos;
639  }
640 
641 
642  template<typename T>
644  {
645  return pystring::capitalize(*this);
646  }
647 
648  template<typename T>
650  {
651  return pystring::center(*this, width);
652  }
653 
654  template<typename T>
655  int AgXString<T>::count(const AgXString<T>& substr, int start, int end) const
656  {
657  return pystring::count(*this, substr, start, end);
658  }
659 
660  template<typename T>
661  bool AgXString<T>::endswith(const AgXString<T>& suffix, int start, int end) const
662  {
663  return pystring::endswith(*this, suffix, start, end);
664  }
665 
666  template<typename T>
667  bool AgXString<T>::endswithCI(const AgXString<T>& suffix, int start, int end) const
668  {
669  return pystring::endswithCI(*this, suffix, start, end);
670  }
671 
672  template<typename T>
674  {
675  return pystring::expandtabs(*this, tabsize);
676  }
677 
678  /*
679  int AgXString<T>::find(const AgXString<T>& sub, int start, int end) const
680  {
681  return pystring::find(*this, sub, start, end);
682  }
683  */
684 
685  template<typename T>
686  int AgXString<T>::index(const AgXString<T>& sub, int start, int end) const
687  {
688  return pystring::index(*this, sub, start, end);
689  }
690 
691  template<typename T>
693  {
694  return pystring::isalnum(*this);
695  }
696 
697  template<typename T>
699  {
700  return pystring::isalpha(*this);
701  }
702 
703  template<typename T>
705  {
706  return pystring::isdigit(*this);
707  }
708 
709  template<typename T>
711  {
712  return pystring::islower(*this);
713  }
714 
715  template<typename T>
717  {
718  return pystring::isspace(*this);
719  }
720 
721  template<typename T>
723  {
724  return pystring::istitle(*this);
725  }
726 
727  template<typename T>
729  {
730  return pystring::isupper(*this);
731  }
732 
733 
734  template<typename T>
736  {
737  return pystring::ljust(*this, width);
738  }
739 
740  template<typename T>
742  {
743  return pystring::lower(*this);
744  }
745 
746 
747  template<typename T>
749  {
750  std::size_t start = this->find(toBeRemoved); // Find the string to be removed
751  if (start == 0 ) {// Did we find it, and its from the start of the string?
752  if (T::length() > toBeRemoved.size()) {
753  return this->substr(toBeRemoved.size(), T::length() ); // Remove it.
754  } else
755  return "";
756  }
757  return *this;
758  }
759 
760 
761  template<typename T>
763  {
764  return pystring::lstrip(*this, chars);
765  }
766 
767  template<typename T>
768  void AgXString<T>::partition(const AgXString<T>& sep, StringVector& result ) const
769  {
770  return pystring::partition(*this, sep, result);
771  }
772 
773  template<typename T>
774  AgXString<T> AgXString<T>::replace(const AgXString<T>& oldstr, const AgXString<T>& newstr, int count) const
775  {
776  return pystring::replace(*this, oldstr, newstr, count);
777  }
778 
779  template<typename T>
780  size_t AgXString<T>::find(const AgXString<T>& sub, int start, int end) const
781  {
782  int index = pystring::find(*this, sub, start, end);
783  return index < 0 ? AgXString<T>::npos : (size_t)index;
784  }
785 
786  template<typename T>
787  size_t AgXString<T>::rfind(const AgXString<T>& sub, int start, int end) const
788  {
789  int index = pystring::rfind(*this, sub, start, end);
790  return index < 0 ? AgXString<T>::npos : (size_t)index;
791  }
792 
793 
794  template<typename T>
796  {
797  return pystring::rjust(*this, width);
798  }
799 
800  template<typename T>
801  void AgXString<T>::rpartition(const AgXString<T>& sep, StringVector& result ) const
802  {
803  return pystring::rpartition(*this, sep, result);
804  }
805 
806  template<typename T>
808  {
809  return pystring::rstrip(*this, chars);
810  }
811 
812  template<typename T>
813  void AgXString<T>::split(StringVector& result, const AgXString<T>& sep, int maxsplit) const
814  {
815  return pystring::split(*this, result, sep, maxsplit);
816  }
817 
818  template<typename T>
819  void AgXString<T>::rsplit(StringVector& result, const AgXString<T>& sep, int maxsplit) const
820  {
821  return pystring::rsplit(*this, result, sep, maxsplit);
822  }
823 
824  template<typename T>
825  void AgXString<T>::splitlines(StringVector& result, bool keepends) const
826  {
827  return pystring::splitlines(*this, result, keepends);
828  }
829 
830  template<typename T>
831  bool AgXString<T>::startswith(const AgXString<T>& prefix, int start, int end) const
832  {
833  return pystring::startswith(*this, prefix, start, end);
834  }
835 
836  template<typename T>
837  bool AgXString<T>::startswithCI(const AgXString<T>& prefix, int start, int end) const
838  {
839  return pystring::startswithCI(*this, prefix, start, end);
840  }
841 
842  template<typename T>
844  {
845  if (this->size() == 0)
846  return *this;
847 
848  return pystring::strip(*this, chars);
849  }
850 
851  template<typename T>
853  {
854  return pystring::swapcase(*this);
855  }
856 
857  template<typename T>
859  {
860  return pystring::title(*this);
861  }
862 
863  template<typename T>
864  AgXString<T> AgXString<T>::translate(const AgXString<T>& table, const AgXString<T>& deletechars) const
865  {
866  return pystring::translate(*this, table, deletechars);
867  }
868 
869  template<typename T>
871  {
872  return pystring::upper(*this);
873  }
874 
875  template<typename T>
877  {
878  return pystring::zfill(*this, width);
879  }
880 
881  template<typename T>
882  AgXString<T> AgXString<T>::slice(int start, int end) const
883  {
884  return pystring::slice(*this, start, end);
885  }
886 
887 
888  template <typename T>
889  agx::String printBin(const T& val)
890  {
891  return printBin(&val, sizeof(T));
892  }
893 
894  template <typename T>
895  agx::String printHex(const T& val)
896  {
897  return printHex(&val, sizeof(T));
898  }
899 
900 }
901 
902 namespace std
903 {
905  {
906  std::swap(static_cast<std::string&>(lhs), static_cast<std::string&>(rhs));
907  }
908 }
909 
910 
911 #endif /* _AGX_STRING_H_ */
void swap(agx::Name &lhs, agx::Name &rhs)
Definition: Name.h:323
#define MAX_32BIT_INT
Definition: String.h:190
#define AGXCORE_EXPORT
AgXString center(int width) const
Definition: String.h:649
AgXString swapcase() const
Definition: String.h:852
AgXString(const_iterator begin, const_iterator end)
Construct sub-string given iterators.
Definition: String.h:550
void splitlines(StringVector &result, bool keepends=false) const
Definition: String.h:825
AgXString zfill(int width) const
Definition: String.h:876
bool istitle() const
Definition: String.h:722
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:819
AgXString(const std::string &str)
Create a AgXString from stl string.
Definition: String.h:518
bool isdigit() const
Definition: String.h:704
AgXString(const AgXString &str)
Copy constructors.
Definition: String.h:511
AgXString upper() const
Definition: String.h:870
bool operator!=(const std::string &other) const
Definition: String.h:610
bool contains(const AgXString &sub, size_t start=0) const
Definition: String.h:636
AgXString strip(const AgXString &chars="") const
Definition: String.h:843
AgXString rstrip(const AgXString &chars="") const
Definition: String.h:807
AgXString copy() const
Copy method, if implementation of this class is changed.
Definition: String.h:580
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:813
int index(const AgXString &sub, int start=0, int end=MAX_32BIT_INT) const
Synonym of find right now.
Definition: String.h:686
AgXString replace(const AgXString &oldstr, const AgXString &newstr, int count=-1) const
Definition: String.h:774
AgXString substr(size_type index) const
Definition: String.h:586
bool isspace() const
Definition: String.h:716
AgXString stripLeadingString(const AgXString &toBeRemoved) const
If this string begins with toBeRemoved, it will be removed and returned.
Definition: String.h:748
AgXString title() const
Definition: String.h:858
AgXString(const char *str, size_type index, size_type length)
Construct sub-string of str from index and length characters forward.
Definition: String.h:543
static AgXString formatVA(const char *format, va_list ap)
Definition: String.h:477
bool endswith(const AgXString &suffix, int start=0, int end=MAX_32BIT_INT) const
Definition: String.h:661
AgXString(const char *str)
Element type pointer constructor.
Definition: String.h:524
AgXString translate(const AgXString &table, const AgXString &deletechars="") const
Definition: String.h:864
bool startswithCI(const AgXString &prefix, int start=0, int end=MAX_32BIT_INT) const
Definition: String.h:837
AgXString rjust(int width) const
Definition: String.h:795
std::string::size_type size_type
Definition: String.h:43
std::string::iterator iterator
Definition: String.h:44
bool operator==(const std::string &other) const
Definition: String.h:598
AgXString capitalize() const
Definition: String.h:643
bool endswithCI(const AgXString &suffix, int start=0, int end=MAX_32BIT_INT) const
Definition: String.h:667
AgXString slice(int start=0, int end=MAX_32BIT_INT) const
function matching python's slice functionality.
Definition: String.h:882
AgXString lstrip(const AgXString &chars="") const
Definition: String.h:762
AgXString substr(size_type index, size_type length) const
Definition: String.h:592
size_t find(const AgXString &sub, int start=0, int end=MAX_32BIT_INT) const
Definition: String.h:780
AgXString expandtabs(int tabsize=8) const
Definition: String.h:673
std::string::const_iterator const_iterator
Definition: String.h:45
AgXString()
Default constructor.
Definition: String.h:505
bool islower() const
Definition: String.h:710
bool isalnum() const
Definition: String.h:692
bool isalpha() const
Definition: String.h:698
bool startswith(const AgXString &prefix, int start=0, int end=MAX_32BIT_INT) const
Definition: String.h:831
void rpartition(const AgXString &sep, StringVector &result) const
Split the string around last occurrence of sep.
Definition: String.h:801
int count(const AgXString &substr, int start=0, int end=MAX_32BIT_INT) const
Definition: String.h:655
AgXString operator+(const AgXString &other) const
Operator +, AgXString b = thisAgXString + other.
Definition: String.h:556
bool isupper() const
Definition: String.h:728
size_t rfind(const AgXString &sub, int start=0, int end=MAX_32BIT_INT) const
Definition: String.h:787
static AgXString format(const char *format,...)
C printf formatting of a string.
Definition: String.h:467
AgXString ljust(int width) const
Definition: String.h:735
AgXString(const char *str, size_type length)
Construct given str, final length of this string will be length.
Definition: String.h:536
AgXString lower() const
Definition: String.h:741
void partition(const AgXString &sep, StringVector &result) const
Split the string around first occurrence of sep.
Definition: String.h:768
AgXString(size_type length, char ch)
Construct length number of ch.
Definition: String.h:530
Templated vector class.
Definition: agx/Vector.h:53
#define agxVerifyN(expr, format,...)
Definition: debug.h:133
#define agxAssert(expr)
Definition: debug.h:143
#define AGX_FORCE_INLINE
Definition: macros.h:58
The agx namespace contains the dynamics/math part of the AGX Dynamics API.
double Real64
Definition: Real.h:45
AgXString< T > operator+(const Indentation &indentation, const AgXString< T > &str)
Definition: Indentation.h:66
uint32_t UInt32
Definition: Integer.h:40
uint64_t UInt64
Definition: Integer.h:41
LinearProbingHashSetImplementation< KeyT, HashT >::iterator begin(LinearProbingHashSetImplementation< KeyT, HashT > &set)
Vector< AgXString< std::string > > StringVector
Definition: String.h:31
AgXString< std::string > String
Definition: String.h:423
uint32_t UInt
Definition: Integer.h:34
float Real32
Definition: Real.h:44
LinearProbingHashSetImplementation< KeyT, HashT >::iterator end(LinearProbingHashSetImplementation< KeyT, HashT > &set)
agx::String printHex(const T &val)
Definition: String.h:895
AGXCORE_EXPORT const agx::String & getEmptyString()
Return a reference to an empty string that is owned by an AGX Dynamics shared library.
agx::String printBin(const T &val)
Definition: String.h:889