OpenMS
StringListUtils.h
Go to the documentation of this file.
1 // --------------------------------------------------------------------------
2 // OpenMS -- Open-Source Mass Spectrometry
3 // --------------------------------------------------------------------------
4 // Copyright The OpenMS Team -- Eberhard Karls University Tuebingen,
5 // ETH Zurich, and Freie Universitaet Berlin 2002-2023.
6 //
7 // This software is released under a three-clause BSD license:
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above copyright
11 // notice, this list of conditions and the following disclaimer in the
12 // documentation and/or other materials provided with the distribution.
13 // * Neither the name of any author or any participating institution
14 // may be used to endorse or promote products derived from this software
15 // without specific prior written permission.
16 // For a full list of authors, refer to the file AUTHORS.
17 // --------------------------------------------------------------------------
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 // ARE DISCLAIMED. IN NO EVENT SHALL ANY OF THE AUTHORS OR THE CONTRIBUTING
22 // INSTITUTIONS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
25 // OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26 // WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
27 // OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
28 // ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 //
30 // --------------------------------------------------------------------------
31 // $Maintainer: Timo Sachsenberg$
32 // $Authors: Marc Sturm $
33 // --------------------------------------------------------------------------
34 
35 #pragma once
36 
39 #include <OpenMS/OpenMSConfig.h>
40 
41 class QStringList;
42 
43 namespace OpenMS
44 {
45 
51  class OPENMS_DLLAPI StringListUtils
52  {
53 public:
58  typedef std::vector<String>::iterator Iterator;
60  typedef std::vector<String>::const_iterator ConstIterator;
62  typedef std::vector<String>::reverse_iterator ReverseIterator;
64  typedef std::vector<String>::const_reverse_iterator ConstReverseIterator;
66 
68  static StringList fromQStringList(const QStringList& rhs);
69 
71 
72 
81  static Iterator searchPrefix(const Iterator& start, const Iterator& end, const String& text, bool trim = false);
82 
92  static ConstIterator searchPrefix(const ConstIterator& start, const ConstIterator& end, const String& text, bool trim = false);
93 
102  static ConstIterator searchPrefix(const StringList& container, const String& text, bool trim = false);
103 
112  static Iterator searchPrefix(StringList& container, const String& text, bool trim = false);
113 
123  static Iterator searchSuffix(const Iterator& start, const Iterator& end, const String& text, bool trim = false);
124 
134  static ConstIterator searchSuffix(const ConstIterator& start, const ConstIterator& end, const String& text, bool trim = false);
135 
144  static ConstIterator searchSuffix(const StringList& container, const String& text, bool trim = false);
145 
154  static Iterator searchSuffix(StringList& container, const String& text, bool trim = false);
155 
156 
158 
164  static void toUpper(StringList& sl);
165 
171  static void toLower(StringList& sl);
172 
173 private:
175  struct TrimmableStringPredicate_
176  {
177  TrimmableStringPredicate_(const String& target, const bool trim) :
178  trim_(trim),
179  target_(target)
180  {
181  if (trim_) target_.trim();
182  }
183 
184  inline String getValue(const String& value) const
185  {
186  if (trim_)
187  {
188  // trim is not a const function so we need to create a copy first
189  String cp = value;
190  return cp.trim();
191  }
192  else
193  {
194  return value;
195  }
196  }
197 
198 protected:
200  bool trim_;
202  String target_;
203  };
204 
206  struct PrefixPredicate_ :
207  TrimmableStringPredicate_
208  {
209  PrefixPredicate_(const String& target, const bool trim) :
210  TrimmableStringPredicate_(target, trim)
211  {}
212 
219  inline bool operator()(const String& value)
220  {
221  return getValue(value).hasPrefix(target_);
222  }
223 
224  };
225 
227  struct SuffixPredicate_ :
228  TrimmableStringPredicate_
229  {
230  SuffixPredicate_(const String& target, const bool trim) :
231  TrimmableStringPredicate_(target, trim)
232  {}
233 
240  inline bool operator()(const String& value)
241  {
242  return getValue(value).hasSuffix(target_);
243  }
244 
245  };
247 
252  };
253 
254 } // namespace OPENMS
255 
Utilities operating on lists of Strings.
Definition: StringListUtils.h:52
static void toLower(StringList &sl)
Transforms all strings contained in the passed StringList to lower case.
static Iterator searchPrefix(const Iterator &start, const Iterator &end, const String &text, bool trim=false)
Searches for the first line that starts with text beginning at line start.
std::vector< String >::reverse_iterator ReverseIterator
Mutable reverse iterator.
Definition: StringListUtils.h:62
StringListUtils & operator=(StringListUtils &)
Definition: StringListUtils.h:251
std::vector< String >::const_iterator ConstIterator
Non-mutable iterator.
Definition: StringListUtils.h:60
static Iterator searchSuffix(const Iterator &start, const Iterator &end, const String &text, bool trim=false)
Searches for the first line that ends with text beginning at line start.
static ConstIterator searchSuffix(const StringList &container, const String &text, bool trim=false)
Searches for the first line that ends with text in the StringList container.
static void toUpper(StringList &sl)
Transforms all strings contained in the passed StringList to upper case.
static Iterator searchPrefix(StringList &container, const String &text, bool trim=false)
Searches for the first line that starts with text in the StringList container.
std::vector< String >::const_reverse_iterator ConstReverseIterator
Non-mutable reverse iterator.
Definition: StringListUtils.h:64
static Iterator searchSuffix(StringList &container, const String &text, bool trim=false)
Searches for the first line that ends with text in the StringList container.
static ConstIterator searchPrefix(const StringList &container, const String &text, bool trim=false)
Searches for the first line that starts with text in the StringList container.
static ConstIterator searchSuffix(const ConstIterator &start, const ConstIterator &end, const String &text, bool trim=false)
Searches for the first line that ends with text beginning at line start.
INTERNAL StringListUtils()
hide c'tors to avoid instantiation of utils class
Definition: StringListUtils.h:249
StringListUtils(const StringListUtils &)
Definition: StringListUtils.h:250
std::vector< String >::iterator Iterator
Mutable iterator.
Definition: StringListUtils.h:58
static StringList fromQStringList(const QStringList &rhs)
Creates a StringList from a QStringList.
static ConstIterator searchPrefix(const ConstIterator &start, const ConstIterator &end, const String &text, bool trim=false)
Searches for the first line that starts with text beginning at line start.
A more convenient string class.
Definition: String.h:60
String & trim()
removes whitespaces (space, tab, line feed, carriage return) at the beginning and the end of the stri...
std::vector< String > StringList
Vector of String.
Definition: ListUtils.h:70
static String & trim(String &this_s)
Definition: StringUtilsSimple.h:229
Main OpenMS namespace.
Definition: FeatureDeconvolution.h:48