AGX Dynamics 2.42.1.1
Loading...
Searching...
No Matches
String.h
Go to the documentation of this file.
1/*
2Copyright 2007-2025. Algoryx Simulation AB.
3
4All AGX source code, intellectual property, documentation, sample code,
5tutorials, scene files and technical white papers, are copyrighted, proprietary
6and confidential material of Algoryx Simulation AB. You may not download, read,
7store, distribute, publish, copy or otherwise disseminate, use or expose this
8material unless having a written signed agreement with Algoryx Simulation AB, or having been
9advised so by Algoryx Simulation AB for a time limited evaluation, or having purchased a
10valid commercial license from Algoryx Simulation AB.
11
12Algoryx Simulation AB disclaims all responsibilities for loss or damage caused
13from using this software, unless otherwise stated in written agreements with
14Algoryx 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
29namespace 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
117
122
127
132 static AgXString format( const char* format, ... );
133 static AgXString formatVA( const char* format, va_list ap);
134
138 bool operator== (const AgXString& other) const;
139
143 bool operator== (const std::string& other) const;
144
148 bool operator== (const char* other) const;
149
153 bool operator!= (const std::string& other) const;
154
158 bool operator!= (const char* other) const;
159
160 /* Pystring extensions */
162 // Copyright (c) 2008, Sony Pictures Imageworks
163 // All rights reserved.
164 //
165 // Redistribution and use in source and binary forms, with or without
166 // modification, are permitted provided that the following conditions are
167 // met:
168 //
169 // Redistributions of source code must retain the above copyright notice,
170 // this list of conditions and the following disclaimer.
171 // Redistributions in binary form must reproduce the above copyright
172 // notice, this list of conditions and the following disclaimer in the
173 // documentation and/or other materials provided with the distribution.
174 // Neither the name of the organization Sony Pictures Imageworks nor the
175 // names of its contributors
176 // may be used to endorse or promote products derived from this software
177 // without specific prior written permission.
178 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
179 // "AS
180 // IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
181 // TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
182 // PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
183 // OWNER
184 // OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
185 // SPECIAL,
186 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
187 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
188 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
189 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
190 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
191 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
193
194#define MAX_32BIT_INT 2147483647
195
200
204 AgXString center(int width) const;
205
210 int count(const AgXString& substr, int start = 0, int end = MAX_32BIT_INT) const;
211
216 bool endswith(const AgXString& suffix, int start = 0, int end = MAX_32BIT_INT) const;
217 bool endswithCI(const AgXString& suffix, int start = 0, int end = MAX_32BIT_INT) const;
218
223 AgXString expandtabs(int tabsize = 8) const;
224
230 bool contains(const AgXString& sub, size_t start = 0) const;
231
235 int index(const AgXString& sub, int start = 0, int end = MAX_32BIT_INT) const;
236
241 bool isalnum() const;
242
247 bool isalpha() const;
248
253 bool isdigit() const;
254
259 bool islower() const;
260
265 bool isspace() const;
266
272 bool istitle() const;
273
278 bool isupper() const;
279
284 AgXString ljust(int width) const;
285
290
297 AgXString lstrip(const AgXString& chars = "") const;
298
303 AgXString stripLeadingString(const AgXString& toBeRemoved) const;
304
311 void partition(const AgXString& sep, StringVector& result ) const;
312
317 AgXString replace(const AgXString& oldstr, const AgXString& newstr, int count = -1) const;
318
324 size_t find(const AgXString& sub, int start = 0, int end = MAX_32BIT_INT ) const;
325
331 size_t rfind(const AgXString& sub, int start = 0, int end = MAX_32BIT_INT ) const;
332
337 AgXString rjust(int width) const;
338
345 void rpartition(const AgXString& sep, StringVector& result ) const;
346
352 AgXString rstrip(const AgXString& chars = "") const;
353
359 void split(StringVector& result, const AgXString& sep = "", int maxsplit = -1) const;
360
368 void rsplit(StringVector& result, const AgXString& sep = "", int maxsplit = -1) const;
369
374 void splitlines(StringVector& result, bool keepends = false ) const;
375
381 bool startswith(const AgXString& prefix, int start = 0, int end = MAX_32BIT_INT ) const;
382 bool startswithCI(const AgXString& prefix, int start = 0, int end = MAX_32BIT_INT ) const;
383
389 AgXString strip(const AgXString& chars = "") const;
390
395
401
407 AgXString translate(const AgXString& table, const AgXString& deletechars = "") const;
408
413
418 AgXString zfill(int width) const;
419
423 AgXString slice(int start = 0, int end = MAX_32BIT_INT) const;
424 };
425
426#ifndef SWIG
428#endif
429
438
439 // Utility function to print bit patterns
440 // NOTE: Ignores endianess, handles data as a block of bytes
441 template <typename T>
442 agx::String printBin(const T& val);
443
444 template <typename T>
445 agx::String printHex(const T& val);
446
447 agx::String AGXCORE_EXPORT printBin(const void* data, UInt numBytes);
448 agx::String AGXCORE_EXPORT printHex(const void* data, UInt numBytes);
449
450
451 // Specializations for basic types, handles endianess
456
461
462} // namespace agx
463
464
465#include <pystring/pystring.h>
466
467namespace agx
468{
469
470 template<typename T>
471 inline AgXString<T> AgXString<T>::format(const char* format, ...)
472 {
473 va_list ap;
474 va_start(ap, format);
475 AgXString<T> result = AgXString<T>::formatVA(format, ap);
476 va_end(ap);
477 return result;
478 }
479
480 template<typename T>
481 inline AgXString<T> AgXString<T>::formatVA(const char* format, va_list ap)
482 {
483 char* buf = 0;
484
485#ifdef _MSC_VER
486 int len = _vscprintf( format, ap ) + 1;
487 buf = (char*) malloc( len * sizeof(char) );
488 if (buf)
489 vsprintf_s( buf, len, format, ap );
490#else
491 int ret = vasprintf(&buf, format, ap);
492 agxVerifyN(ret >= 0, "Could not allocate memory for formatted string: \'%s\'", format);
493#endif
494
495 agxAssert(buf);
496 if (buf) {
497 AgXString<T> result(buf);
498 free(buf);
499
500 return result;
501 }
502 else
503 return AgXString<T>(); // Fall back.
504 }
505
506 // Implementation
507
508 template <typename T>
510 {
511
512 }
513
514 template <typename T>
516 : T( str )
517
518 {
519 }
520
521 template <typename T>
522 inline AgXString<T>::AgXString( const std::string& str )
523 : T( str )
524 {
525 }
526
527 template <typename T>
528 inline AgXString<T>::AgXString( const char* str )
529 : T( str )
530 {
531 }
532
533 template <typename T>
534 inline AgXString<T>::AgXString( size_type length, char ch )
535 : T( length, ch )
536 {
537 }
538
539 template <typename T>
540 inline AgXString<T>::AgXString( const char* str, size_type length )
541 : T( str, length )
542
543 {
544 }
545
546 template <typename T>
547 inline AgXString<T>::AgXString( const char* str, size_type index, size_type length )
548 : T( str, index, length )
549
550 {
551 }
552
553 template <typename T>
555 : T( begin, end )
556 {
557 }
558
559 template <typename T>
561 {
562 AgXString ret = *this;
563 ret += other;
564 return ret;
565 }
566
567 template <typename T>
568 inline AgXString<T> AgXString<T>::operator + ( const char* other ) const
569 {
570 AgXString ret = *this;
571 ret += other;
572 return ret;
573 }
574
575 template <typename T>
576 inline AgXString<T> AgXString<T>::operator + ( const char elem ) const
577 {
578 AgXString ret = *this;
579 ret += elem;
580 return ret;
581 }
582
583 template <typename T>
585 {
586 return AgXString( *this );
587 }
588
589 template <typename T>
591 {
592 return AgXString( this->c_str(), index, this->size() );
593 }
594
595 template <typename T>
597 {
598 return AgXString( this->c_str(), index, length );
599 }
600
601 template <typename T>
603 {
604 return static_cast<const std::string&>(*this) == static_cast<const std::string&>(other);
605 }
606
607 template <typename T>
608 AGX_FORCE_INLINE bool AgXString<T>::operator== (const std::string& other) const
609 {
610 return *(std::string*)this == other;
611 }
612
613 template <typename T>
614 AGX_FORCE_INLINE bool AgXString<T>::operator== (const char* other) const
615 {
616 return *(std::string*)this == other;
617 }
618
619 template <typename T>
620 AGX_FORCE_INLINE bool AgXString<T>::operator!= (const std::string& other) const
621 {
622 return *(std::string*)this != other;
623 }
624
625 template <typename T>
626 AGX_FORCE_INLINE bool AgXString<T>::operator!= (const char* other) const
627 {
628 return *(std::string*)this != other;
629 }
630
631
632
633 template<typename T>
634 AgXString<T> operator + ( const char* str1, const AgXString<T>& str2 )
635 {
636 return AgXString<T>( str1 ) + str2;
637 }
638
639 template<typename T>
640 AgXString<T> operator + ( const char elem, const AgXString<T>& str )
641 {
642 return AgXString<T>( 1, elem ) + str;
643 }
644
645 template<typename T>
646 inline bool AgXString<T>::contains(const AgXString<T>& sub, size_t start) const
647 {
648 return this->find(sub, int(start)) != AgXString<T>::npos;
649 }
650
651
652 template<typename T>
654 {
655 return pystring::capitalize(*this);
656 }
657
658 template<typename T>
660 {
661 return pystring::center(*this, width);
662 }
663
664 template<typename T>
665 int AgXString<T>::count(const AgXString<T>& substr, int start, int end) const
666 {
667 return pystring::count(*this, substr, start, end);
668 }
669
670 template<typename T>
671 bool AgXString<T>::endswith(const AgXString<T>& suffix, int start, int end) const
672 {
673 return pystring::endswith(*this, suffix, start, end);
674 }
675
676 template<typename T>
677 bool AgXString<T>::endswithCI(const AgXString<T>& suffix, int start, int end) const
678 {
679 return pystring::endswithCI(*this, suffix, start, end);
680 }
681
682 template<typename T>
684 {
685 return pystring::expandtabs(*this, tabsize);
686 }
687
688 /*
689 int AgXString<T>::find(const AgXString<T>& sub, int start, int end) const
690 {
691 return pystring::find(*this, sub, start, end);
692 }
693 */
694
695 template<typename T>
696 int AgXString<T>::index(const AgXString<T>& sub, int start, int end) const
697 {
698 return pystring::index(*this, sub, start, end);
699 }
700
701 template<typename T>
703 {
704 return pystring::isalnum(*this);
705 }
706
707 template<typename T>
709 {
710 return pystring::isalpha(*this);
711 }
712
713 template<typename T>
715 {
716 return pystring::isdigit(*this);
717 }
718
719 template<typename T>
721 {
722 return pystring::islower(*this);
723 }
724
725 template<typename T>
727 {
728 return pystring::isspace(*this);
729 }
730
731 template<typename T>
733 {
734 return pystring::istitle(*this);
735 }
736
737 template<typename T>
739 {
740 return pystring::isupper(*this);
741 }
742
743
744 template<typename T>
746 {
747 return pystring::ljust(*this, width);
748 }
749
750 template<typename T>
752 {
753 return pystring::lower(*this);
754 }
755
756
757 template<typename T>
759 {
760 std::size_t start = this->find(toBeRemoved); // Find the string to be removed
761 if (start == 0 ) {// Did we find it, and its from the start of the string?
762 if (T::length() > toBeRemoved.size()) {
763 return this->substr(toBeRemoved.size(), T::length() ); // Remove it.
764 } else
765 return "";
766 }
767 return *this;
768 }
769
770
771 template<typename T>
773 {
774 return pystring::lstrip(*this, chars);
775 }
776
777 template<typename T>
778 void AgXString<T>::partition(const AgXString<T>& sep, StringVector& result ) const
779 {
780 return pystring::partition(*this, sep, result);
781 }
782
783 template<typename T>
784 AgXString<T> AgXString<T>::replace(const AgXString<T>& oldstr, const AgXString<T>& newstr, int count) const
785 {
786 return pystring::replace(*this, oldstr, newstr, count);
787 }
788
789 template<typename T>
790 size_t AgXString<T>::find(const AgXString<T>& sub, int start, int end) const
791 {
792 int index = pystring::find(*this, sub, start, end);
793 return index < 0 ? AgXString<T>::npos : (size_t)index;
794 }
795
796 template<typename T>
797 size_t AgXString<T>::rfind(const AgXString<T>& sub, int start, int end) const
798 {
799 int index = pystring::rfind(*this, sub, start, end);
800 return index < 0 ? AgXString<T>::npos : (size_t)index;
801 }
802
803
804 template<typename T>
806 {
807 return pystring::rjust(*this, width);
808 }
809
810 template<typename T>
811 void AgXString<T>::rpartition(const AgXString<T>& sep, StringVector& result ) const
812 {
813 return pystring::rpartition(*this, sep, result);
814 }
815
816 template<typename T>
818 {
819 return pystring::rstrip(*this, chars);
820 }
821
822 template<typename T>
823 void AgXString<T>::split(StringVector& result, const AgXString<T>& sep, int maxsplit) const
824 {
825 return pystring::split(*this, result, sep, maxsplit);
826 }
827
828 template<typename T>
829 void AgXString<T>::rsplit(StringVector& result, const AgXString<T>& sep, int maxsplit) const
830 {
831 return pystring::rsplit(*this, result, sep, maxsplit);
832 }
833
834 template<typename T>
835 void AgXString<T>::splitlines(StringVector& result, bool keepends) const
836 {
837 return pystring::splitlines(*this, result, keepends);
838 }
839
840 template<typename T>
841 bool AgXString<T>::startswith(const AgXString<T>& prefix, int start, int end) const
842 {
843 return pystring::startswith(*this, prefix, start, end);
844 }
845
846 template<typename T>
847 bool AgXString<T>::startswithCI(const AgXString<T>& prefix, int start, int end) const
848 {
849 return pystring::startswithCI(*this, prefix, start, end);
850 }
851
852 template<typename T>
854 {
855 if (this->size() == 0)
856 return *this;
857
858 return pystring::strip(*this, chars);
859 }
860
861 template<typename T>
863 {
864 return pystring::swapcase(*this);
865 }
866
867 template<typename T>
869 {
870 return pystring::title(*this);
871 }
872
873 template<typename T>
874 AgXString<T> AgXString<T>::translate(const AgXString<T>& table, const AgXString<T>& deletechars) const
875 {
876 return pystring::translate(*this, table, deletechars);
877 }
878
879 template<typename T>
881 {
882 return pystring::upper(*this);
883 }
884
885 template<typename T>
887 {
888 return pystring::zfill(*this, width);
889 }
890
891 template<typename T>
893 {
894 return pystring::slice(*this, start, end);
895 }
896
897
898 template <typename T>
899 agx::String printBin(const T& val)
900 {
901 return printBin(&val, sizeof(T));
902 }
903
904 template <typename T>
905 agx::String printHex(const T& val)
906 {
907 return printHex(&val, sizeof(T));
908 }
909
910}
911
912namespace std
913{
915 {
916 std::swap(static_cast<std::string&>(lhs), static_cast<std::string&>(rhs));
917 }
918}
919
920
921#endif /* _AGX_STRING_H_ */
#define MAX_32BIT_INT
Definition: String.h:194
#define AGXCORE_EXPORT
AgXString center(int width) const
Definition: String.h:659
AgXString swapcase() const
Definition: String.h:862
AgXString(const_iterator begin, const_iterator end)
Construct sub-string given iterators.
Definition: String.h:554
void splitlines(StringVector &result, bool keepends=false) const
Fills "result" with a list of the lines in the string, breaking at line boundaries.
Definition: String.h:835
AgXString zfill(int width) const
Definition: String.h:886
bool istitle() const
Definition: String.h:732
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:829
AgXString(const std::string &str)
Create a AgXString from stl string.
Definition: String.h:522
bool isdigit() const
Definition: String.h:714
AgXString(const AgXString &str)
Copy constructors.
Definition: String.h:515
AgXString upper() const
Definition: String.h:880
bool operator!=(const std::string &other) const
Definition: String.h:620
bool contains(const AgXString &sub, size_t start=0) const
Definition: String.h:646
AgXString strip(const AgXString &chars="") const
Definition: String.h:853
AgXString rstrip(const AgXString &chars="") const
Definition: String.h:817
AgXString copy() const
Copy method, if implementation of this class is changed.
Definition: String.h:584
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:823
int index(const AgXString &sub, int start=0, int end=MAX_32BIT_INT) const
Synonym of find right now.
Definition: String.h:696
AgXString replace(const AgXString &oldstr, const AgXString &newstr, int count=-1) const
Definition: String.h:784
AgXString substr(size_type index) const
Definition: String.h:590
bool isspace() const
Definition: String.h:726
AgXString stripLeadingString(const AgXString &toBeRemoved) const
If this string begins with toBeRemoved, it will be removed and returned.
Definition: String.h:758
AgXString title() const
Definition: String.h:868
AgXString(const char *str, size_type index, size_type length)
Construct sub-string of str from index and length characters forward.
Definition: String.h:547
static AgXString formatVA(const char *format, va_list ap)
Definition: String.h:481
bool endswith(const AgXString &suffix, int start=0, int end=MAX_32BIT_INT) const
Definition: String.h:671
AgXString(const char *str)
Element type pointer constructor.
Definition: String.h:528
AgXString translate(const AgXString &table, const AgXString &deletechars="") const
Definition: String.h:874
bool startswithCI(const AgXString &prefix, int start=0, int end=MAX_32BIT_INT) const
Definition: String.h:847
AgXString rjust(int width) const
Definition: String.h:805
std::string::size_type size_type
Definition: String.h:43
std::string::iterator iterator
Definition: String.h:44
AgXString capitalize() const
Definition: String.h:653
bool endswithCI(const AgXString &suffix, int start=0, int end=MAX_32BIT_INT) const
Definition: String.h:677
AgXString slice(int start=0, int end=MAX_32BIT_INT) const
function matching python's slice functionality.
Definition: String.h:892
AgXString lstrip(const AgXString &chars="") const
Definition: String.h:772
AgXString substr(size_type index, size_type length) const
Definition: String.h:596
size_t find(const AgXString &sub, int start=0, int end=MAX_32BIT_INT) const
Definition: String.h:790
AgXString expandtabs(int tabsize=8) const
Definition: String.h:683
std::string::const_iterator const_iterator
Definition: String.h:45
AgXString()
Default constructor.
Definition: String.h:509
bool islower() const
Definition: String.h:720
bool isalnum() const
Definition: String.h:702
bool operator==(const AgXString &other) const
Definition: String.h:602
bool isalpha() const
Definition: String.h:708
bool startswith(const AgXString &prefix, int start=0, int end=MAX_32BIT_INT) const
Definition: String.h:841
void rpartition(const AgXString &sep, StringVector &result) const
Split the string around last occurrence of sep.
Definition: String.h:811
int count(const AgXString &substr, int start=0, int end=MAX_32BIT_INT) const
Definition: String.h:665
AgXString operator+(const AgXString &other) const
Operator +, AgXString b = thisAgXString + other.
Definition: String.h:560
bool isupper() const
Definition: String.h:738
size_t rfind(const AgXString &sub, int start=0, int end=MAX_32BIT_INT) const
Definition: String.h:797
static AgXString format(const char *format,...)
C printf formatting of a string.
Definition: String.h:471
AgXString ljust(int width) const
Definition: String.h:745
AgXString(const char *str, size_type length)
Construct given str, final length of this string will be length.
Definition: String.h:540
AgXString lower() const
Definition: String.h:751
void partition(const AgXString &sep, StringVector &result) const
Split the string around first occurrence of sep.
Definition: String.h:778
AgXString(size_type length, char ch)
Construct length number of ch.
Definition: String.h:534
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:44
uint32_t UInt32
Definition: Integer.h:32
uint64_t UInt64
Definition: Integer.h:33
Vector< AgXString< std::string > > StringVector
Definition: String.h:34
LinearProbingHashSetImplementation< KeyT, HashT >::iterator end(LinearProbingHashSetImplementation< KeyT, HashT > &set)
AgXString< std::string > String
Definition: String.h:427
AGXCORE_EXPORT const agx::String & getEmptyString()
Return a reference to an empty string that is owned by an AGX Dynamics shared library.
uint64_t UInt
Definition: Integer.h:27
float Real32
Definition: Real.h:43
LinearProbingHashSetImplementation< KeyT, HashT >::iterator begin(LinearProbingHashSetImplementation< KeyT, HashT > &set)
AgXString< T > operator+(const Indentation &indentation, const AgXString< T > &str)
Definition: Indentation.h:66
agx::String printHex(const T &val)
Definition: String.h:905
agx::String printBin(const T &val)
Definition: String.h:899
STL namespace.
void swap(agx::Name &lhs, agx::Name &rhs)
Definition: Name.h:323