OpenMS
StringListUtils.h
Go to the documentation of this file.
1 // Copyright (c) 2002-present, OpenMS Inc. -- EKU Tuebingen, ETH Zurich, and FU Berlin
2 // SPDX-License-Identifier: BSD-3-Clause
3 //
4 // --------------------------------------------------------------------------
5 // $Maintainer: Timo Sachsenberg$
6 // $Authors: Marc Sturm $
7 // --------------------------------------------------------------------------
8 
9 #pragma once
10 
13 #include <OpenMS/OpenMSConfig.h>
14 
15 class QStringList;
16 
17 namespace OpenMS
18 {
19 
25  class OPENMS_DLLAPI StringListUtils
26  {
27 public:
32  typedef std::vector<String>::iterator Iterator;
34  typedef std::vector<String>::const_iterator ConstIterator;
36  typedef std::vector<String>::reverse_iterator ReverseIterator;
38  typedef std::vector<String>::const_reverse_iterator ConstReverseIterator;
40 
42  static StringList fromQStringList(const QStringList& rhs);
43 
45 
46 
55  static Iterator searchPrefix(const Iterator& start, const Iterator& end, const String& text, bool trim = false);
56 
66  static ConstIterator searchPrefix(const ConstIterator& start, const ConstIterator& end, const String& text, bool trim = false);
67 
76  static ConstIterator searchPrefix(const StringList& container, const String& text, bool trim = false);
77 
86  static Iterator searchPrefix(StringList& container, const String& text, bool trim = false);
87 
97  static Iterator searchSuffix(const Iterator& start, const Iterator& end, const String& text, bool trim = false);
98 
108  static ConstIterator searchSuffix(const ConstIterator& start, const ConstIterator& end, const String& text, bool trim = false);
109 
118  static ConstIterator searchSuffix(const StringList& container, const String& text, bool trim = false);
119 
128  static Iterator searchSuffix(StringList& container, const String& text, bool trim = false);
129 
130 
132 
138  static void toUpper(StringList& sl);
139 
145  static void toLower(StringList& sl);
146 
147 private:
149  struct TrimmableStringPredicate_
150  {
151  TrimmableStringPredicate_(const String& target, const bool trim) :
152  trim_(trim),
153  target_(target)
154  {
155  if (trim_) target_.trim();
156  }
157 
158  inline String getValue(const String& value) const
159  {
160  if (trim_)
161  {
162  // trim is not a const function so we need to create a copy first
163  String cp = value;
164  return cp.trim();
165  }
166  else
167  {
168  return value;
169  }
170  }
171 
172 protected:
174  bool trim_;
176  String target_;
177  };
178 
180  struct PrefixPredicate_ :
181  TrimmableStringPredicate_
182  {
183  PrefixPredicate_(const String& target, const bool trim) :
184  TrimmableStringPredicate_(target, trim)
185  {}
186 
193  inline bool operator()(const String& value)
194  {
195  return getValue(value).hasPrefix(target_);
196  }
197 
198  };
199 
201  struct SuffixPredicate_ :
202  TrimmableStringPredicate_
203  {
204  SuffixPredicate_(const String& target, const bool trim) :
205  TrimmableStringPredicate_(target, trim)
206  {}
207 
214  inline bool operator()(const String& value)
215  {
216  return getValue(value).hasSuffix(target_);
217  }
218 
219  };
221 
226  };
227 
228 } // namespace OPENMS
229 
Utilities operating on lists of Strings.
Definition: StringListUtils.h:26
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:36
StringListUtils & operator=(StringListUtils &)
Definition: StringListUtils.h:225
std::vector< String >::const_iterator ConstIterator
Non-mutable iterator.
Definition: StringListUtils.h:34
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:38
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:223
StringListUtils(const StringListUtils &)
Definition: StringListUtils.h:224
std::vector< String >::iterator Iterator
Mutable iterator.
Definition: StringListUtils.h:32
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:34
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:44
static String & trim(String &this_s)
Definition: StringUtilsSimple.h:204
Main OpenMS namespace.
Definition: openswathalgo/include/OpenMS/OPENSWATHALGO/DATAACCESS/ISpectrumAccess.h:19