1 /*=========================================================================
2  *
3  *  Copyright Insight Software Consortium
4  *
5  *  Licensed under the Apache License, Version 2.0 (the "License");
6  *  you may not use this file except in compliance with the License.
7  *  You may obtain a copy of the License at
8  *
9  *         http://www.apache.org/licenses/LICENSE-2.0.txt
10  *
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  *
17  *=========================================================================*/
18 
19 #include "itkFancyString.h"
20 #include "itkStringTools.h"
21 
22 namespace itk
23 {
24 
25 FancyString::FancyString() = default; // : std::string()
26 
FancyString(const std::string & str)27 FancyString::FancyString( const std::string& str )// : std::string( str )
28 {
29   this->m_Value = str;
30 }
31 
FancyString(const char * s)32 FancyString::FancyString( const char* s )// : std::string( s )
33 {
34   this->m_Value = std::string( s );
35 }
36 
37 FancyString&
operator =(const std::string & str)38 FancyString::operator=( const std::string& str )
39 {
40   this->m_Value = str;
41   return *this;
42 }
43 
44 FancyString&
operator =(const char * s)45 FancyString::operator=( const char* s )
46 {
47   this->m_Value = std::string( s );
48   return *this;
49 }
50 
51 /** Function to cast this type to "const char *". */
operator const char*() const52 FancyString::operator const char * () const
53 {
54   return this->m_Value.c_str();
55 }
56 
57 /** Function to convert this value to a "std::string". */
operator const std::string&() const58 FancyString::operator const std::string & () const
59 {
60   return this->ToString();
61 }
62 
63 /** Function to convert this value to a "std::string". */
ToString() const64 const std::string& FancyString::ToString() const
65 {
66   return this->m_Value;
67 }
68 
69 /////////////////////////////////////////////////////////////////////////////
70 // helper functions for string manipulations
71 /////////////////////////////////////////////////////////////////////////////
72 
73 /** Clear all characters. */
74 void
ClearContent()75 FancyString::ClearContent()
76 {
77   this->m_Value.clear();
78 }
79 
80 /** Extend the string by appending additional characters. */
81 void
Append(const FancyString & str)82 FancyString::Append(const FancyString& str)
83 {
84   this->m_Value.append(str.ToString());
85 }
86 
87 /** Method to trim the spaces or user-specified characters on both ends of a string. */
88 FancyString&
Trim(const std::string & dislike)89 FancyString::Trim( const std::string& dislike )
90 {
91   StringTools::Trim( this->m_Value, dislike );
92   return *this;
93 }
94 
95 /** Method to trim the spaces or user-specified characters on left end of a string. */
96 FancyString&
TrimLeft(const std::string & dislike)97 FancyString::TrimLeft( const std::string& dislike )
98 {
99   StringTools::TrimLeft( this->m_Value, dislike );
100   return *this;
101 }
102 
103 /** Method to trim the spaces or user-specified characters on right end of a string. */
104 FancyString&
TrimRight(const std::string & dislike)105 FancyString::TrimRight( const std::string& dislike )
106 {
107   StringTools::TrimRight( this->m_Value, dislike );
108   return *this;
109 }
110 
111 /** Method to covert lower-case characters to upper cases in a string. */
112 FancyString&
ToUpperCase()113 FancyString::ToUpperCase()
114 {
115   StringTools::ToUpperCase( this->m_Value );
116   return *this;
117 }
118 
119 /** Method to covert upper-case characters to lower cases in a string. */
120 FancyString&
ToLowerCase()121 FancyString::ToLowerCase()
122 {
123   StringTools::ToLowerCase( this->m_Value );
124   return *this;
125 }
126 
127 /** Method to split a string into two parts with user-defined delimiters. */
128 void
Split(std::string & lpart,std::string & rpart,const std::string & delims) const129 FancyString::Split( std::string& lpart, std::string& rpart, const std::string& delims ) const
130 {
131   StringTools::Split( this->m_Value, lpart, rpart, delims );
132 }
133 
134 /** Method to split a string into a sequence of strings with user-defined delimiters. */
135 void
Split(std::vector<std::string> & result,const std::string & delims) const136 FancyString::Split( std::vector<std::string>& result, const std::string& delims ) const
137 {
138   StringTools::Split( this->m_Value, result, delims );
139 }
140 
141 /**
142  * Method to split a string into a sequence of sub-strings with user-defined delimiters,
143  * then each sub-string is further splitted into a <key,value> pair with separators "=:".
144  */
145 void
Split(std::map<std::string,std::string> & result,const std::string & delims)146 FancyString::Split( std::map<std::string,std::string>& result, const std::string& delims )
147 {
148   StringTools::Split( this->m_Value, result, delims );
149 }
150 
151 /** Method to test whether one string matches with another. */
152 bool
MatchWith(const std::string & s2,bool ignoreCase)153 FancyString::MatchWith( const std::string& s2, bool ignoreCase )
154 {
155   return StringTools::MatchWith( this->m_Value, s2, ignoreCase );
156 }
157 
158 /** Method to test whether a string starts with a user-given sub-string. */
159 bool
StartWith(const std::string & s2,bool ignoreCase)160 FancyString::StartWith( const std::string& s2, bool ignoreCase )
161 {
162   return StringTools::StartWith( this->m_Value, s2, ignoreCase );
163 }
164 
165 /** Method to test whether a string ends with a user-given sub-string. */
166 bool
EndWith(const std::string & s2,bool ignoreCase)167 FancyString::EndWith( const std::string& s2, bool ignoreCase )
168 {
169   return StringTools::EndWith( this->m_Value, s2, ignoreCase );
170 }
171 
172 /** Method to test whether a string contains a user-given sub-string. */
173 bool
ContainSub(const std::string & s2,bool ignoreCase)174 FancyString::ContainSub( const std::string& s2, bool ignoreCase )
175 {
176   return StringTools::ContainSub( this->m_Value, s2, ignoreCase );
177 }
178 
179 } // namespace itk
180 
operator !=(itk::FancyString & s,const std::string & str)181 bool operator!=( itk::FancyString& s, const std::string& str)
182 {
183   return s.ToString() != str;
184 }
185 
operator !=(itk::FancyString & s,const char * str)186 bool operator!=( itk::FancyString& s, const char* str)
187 {
188   return s.ToString() != str;
189 }
190 
operator !=(itk::FancyString & s,const itk::FancyString & str)191 bool operator!=( itk::FancyString& s, const  itk::FancyString& str)
192 {
193   return s.ToString() != str.ToString();
194 }
195 
operator ==(itk::FancyString & s,const std::string & str)196 bool operator==( itk::FancyString& s, const std::string& str)
197 {
198   return s.ToString() == str;
199 }
200 
operator ==(itk::FancyString & s,const char * str)201 bool operator==( itk::FancyString& s, const char* str)
202 {
203   return s.ToString() == str;
204 }
205 
operator ==(itk::FancyString & s,const itk::FancyString & str)206 bool operator==( itk::FancyString& s, const  itk::FancyString& str)
207 {
208   return s.ToString() == str.ToString();
209 }
210