NCBI C++ ToolKit
string_constraint.cpp
Go to the documentation of this file.

Go to the SVN repository for this file.

1 /* $Id: string_constraint.cpp 93572 2021-04-30 13:48:31Z stakhovv $
2 * ===========================================================================
3 *
4 * PUBLIC DOMAIN NOTICE
5 * National Center for Biotechnology Information
6 *
7 * This software/database is a "United States Government Work" under the
8 * terms of the United States Copyright Act. It was written as part of
9 * the author's official duties as a United States Government employee and
10 * thus cannot be copyrighted. This software/database is freely available
11 * to the public for use. The National Library of Medicine and the U.S.
12 * Government have not placed any restriction on its use or reproduction.
13 *
14 * Although all reasonable efforts have been taken to ensure the accuracy
15 * and reliability of the software and data, the NLM and the U.S.
16 * Government do not and cannot warrant the performance or results that
17 * may be obtained by using this software or data. The NLM and the U.S.
18 * Government disclaim all warranties, express or implied, including
19 * warranties of performance, merchantability or fitness for any particular
20 * purpose.
21 *
22 * Please cite the author in any work or product based on this material.
23 *
24 * ===========================================================================
25 *
26 * Author: Colleen Bollin, NCBI
27 *
28 * File Description:
29 * String Constraint for editing
30 */
31 #include <ncbi_pch.hpp>
32 #include <corelib/ncbistd.hpp>
34 
38 
39 bool AddValueToString (string& str, const string& value, EExistingText existing_text)
40 {
41  bool rval = false;
42  if (NStr::IsBlank(value)) {
43  // no change to value
44  rval = false;
45  } else if (existing_text == eExistingText_replace_old || NStr::IsBlank (str)) {
46  str = value;
47  rval = true;
48  } else {
49  rval = true;
50  switch (existing_text) {
52  str = str + "; " + value;
53  break;
55  str = str + " " + value;
56  break;
58  str = str + ": " + value;
59  break;
61  str = str + ", " + value;
62  break;
64  str = str + value;
65  break;
67  str = value + "; " + str;
68  break;
70  str = value + " " + str;
71  break;
73  str = value + ": " + str;
74  break;
76  str = value + ", " + str;
77  break;
79  str = value + str;
80  break;
81  default:
82  rval = false;
83  break;
84  }
85  }
86  return rval;
87 }
88 
89 
91 {
92  string match = m_MatchText;
93  if (match.length() == 0) {
94  return true;
95  }
96 
97  bool rval = false;
98  string tmp = text;
99  if (m_IgnoreSpace) {
100  NStr::ReplaceInPlace(match, " ", "");
101  NStr::ReplaceInPlace(tmp, " ", "");
102  }
103  if (m_IgnoreCase) {
106  }
107 
108  switch (m_MatchType) {
109  case eMatchType_Contains:
110  if (NStr::Find(tmp, match) != string::npos) {
111  rval = true;
112  }
113  break;
114  case eMatchType_Equals:
115  if (NStr::Equal(tmp, match)) {
116  rval = true;
117  }
118  break;
120  if (NStr::StartsWith(tmp, match)) {
121  rval = true;
122  }
123  break;
124  case eMatchType_EndsWith:
125  if (NStr::EndsWith(tmp, match)) {
126  rval = true;
127  }
128  break;
129  case eMatchType_IsOneOf:
130  {
131  vector<string> tokens;
132  NStr::Split(match, ",; ", tokens, NStr::fSplit_Tokenize);
133  ITERATE(vector<string>, it, tokens) {
134  if (IsInRange(*it, tmp) || NStr::Equal(*it, tmp)) {
135  rval = true;
136  break;
137  }
138  }
139  }
140  break;
141  }
142  if (m_NotPresent) {
143  rval = !rval;
144  }
145  return rval;
146 }
147 
148 bool CStringConstraint::IsInRange(const string& str, const string &tmp)
149 {
150  bool range(false);
151  if (NStr::Find(str, "-") != NPOS)
152  {
153  string first, last;
154  NStr::SplitInTwo(str, "-", first, last);
159  if (first == last && start <= end)
160  {
161  for ( ; start <= end; ++start)
162  {
163  string match = first + NStr::IntToString(start);
164  if (NStr::Equal(match, tmp))
165  {
166  range = true;
167  break;
168  }
169  }
170  }
171  }
172  return range;
173 }
174 
175 bool CStringConstraint::DoesListMatch(const vector<string>& vals)
176 {
177  bool any_match = false;
178  bool invert = false;
179  if (m_NotPresent) {
180  invert = true;
181  m_NotPresent = false;
182  }
183 
184  ITERATE(vector<string>, s, vals) {
185  any_match = DoesTextMatch(*s);
186  if (any_match) {
187  break;
188  }
189  }
190  if (invert) {
191  m_NotPresent = true;
192  any_match = !any_match;
193  }
194  return any_match;
195 
196 }
197 
198 
200 {
201  m_MatchText = other.m_MatchText;
202  m_MatchType = other.m_MatchType;
203  m_IgnoreCase = other.m_IgnoreCase;
205  m_NotPresent = other.m_NotPresent;
206 }
207 
208 
212 
bool DoesTextMatch(const string &text)
bool DoesListMatch(const vector< string > &vals)
bool IsInRange(const string &str, const string &tmp)
void Assign(const CStringConstraint &other)
Include a standard set of the NCBI C++ Toolkit most basic headers.
#define bool
Definition: bool.h:34
static DLIST_TYPE *DLIST_NAME() first(DLIST_LIST_TYPE *list)
Definition: dlist.tmpl.h:46
static DLIST_TYPE *DLIST_NAME() last(DLIST_LIST_TYPE *list)
Definition: dlist.tmpl.h:51
static const char * str(char *buf, int n)
Definition: stats.c:84
static char tmp[3200]
Definition: utf8.c:42
#define ITERATE(Type, Var, Cont)
ITERATE macro to sequence through container elements.
Definition: ncbimisc.hpp:815
#define END_NCBI_SCOPE
End previously defined NCBI scope.
Definition: ncbistl.hpp:103
#define END_SCOPE(ns)
End the previously defined scope.
Definition: ncbistl.hpp:75
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:100
#define BEGIN_SCOPE(ns)
Define a new scope.
Definition: ncbistl.hpp:72
#define kEmptyStr
Definition: ncbistr.hpp:123
static int StringToInt(const CTempString str, TStringToNumFlags flags=0, int base=10)
Convert string to int.
Definition: ncbistr.cpp:630
static list< string > & Split(const CTempString str, const CTempString delim, list< string > &arr, TSplitFlags flags=0, vector< SIZE_TYPE > *token_pos=NULL)
Split a string using specified delimiters.
Definition: ncbistr.cpp:3461
static bool EndsWith(const CTempString str, const CTempString end, ECase use_case=eCase)
Check if a string ends with a specified suffix value.
Definition: ncbistr.hpp:5430
static bool IsBlank(const CTempString str, SIZE_TYPE pos=0)
Check if a string is blank (has no text).
Definition: ncbistr.cpp:106
#define NPOS
Definition: ncbistr.hpp:133
static string IntToString(int value, TNumToStringFlags flags=0, int base=10)
Convert int to string.
Definition: ncbistr.hpp:5084
static SIZE_TYPE Find(const CTempString str, const CTempString pattern, ECase use_case=eCase, EDirection direction=eForwardSearch, SIZE_TYPE occurrence=0)
Find the pattern in the string.
Definition: ncbistr.cpp:2891
static bool StartsWith(const CTempString str, const CTempString start, ECase use_case=eCase)
Check if a string starts with a specified prefix value.
Definition: ncbistr.hpp:5412
static bool SplitInTwo(const CTempString str, const CTempString delim, string &str1, string &str2, TSplitFlags flags=0)
Split a string into two pieces using the specified delimiters.
Definition: ncbistr.cpp:3554
static bool Equal(const CTempString s1, SIZE_TYPE pos, SIZE_TYPE n, const char *s2, ECase use_case=eCase)
Test for equality of a substring with another string.
Definition: ncbistr.hpp:5384
static string & ReplaceInPlace(string &src, const string &search, const string &replace, SIZE_TYPE start_pos=0, SIZE_TYPE max_replace=0, SIZE_TYPE *num_replace=0)
Replace occurrences of a substring within a string.
Definition: ncbistr.cpp:3405
static string & ToLower(string &str)
Convert string to lower case – string& version.
Definition: ncbistr.cpp:405
@ fConvErr_NoThrow
Do not throw an exception on error.
Definition: ncbistr.hpp:285
@ fAllowLeadingSymbols
Ignore leading non-numeric characters.
Definition: ncbistr.hpp:295
@ fSplit_Tokenize
All delimiters are merged and trimmed, to get non-empty tokens only.
Definition: ncbistr.hpp:2508
static void text(MDB_val *v)
Definition: mdb_dump.c:62
range(_Ty, _Ty) -> range< _Ty >
Definition: fix_pub.hpp:45
const GenericPointer< typename T::ValueType > T2 value
Definition: pointer.h:1227
static int match(register const pcre_uchar *eptr, register const pcre_uchar *ecode, const pcre_uchar *mstart, int offset_top, match_data *md, eptrblock *eptrb, unsigned int rdepth)
Definition: pcre_exec.c:513
static BOOL invert
Definition: pcregrep.c:189
bool AddValueToString(string &str, const string &value, EExistingText existing_text)
Add text to an existing string, using the "existing_text" directive to combine new text with existing...
EExistingText
@ eExistingText_append_semi
@ eExistingText_append_space
@ eExistingText_prefix_colon
@ eExistingText_append_none
@ eExistingText_prefix_none
@ eExistingText_prefix_space
@ eExistingText_append_colon
@ eExistingText_prefix_comma
@ eExistingText_append_comma
@ eExistingText_prefix_semi
@ eExistingText_replace_old
#define const
Definition: zconf.h:232
Modified on Tue Apr 16 20:07:13 2024 by modify_doxy.py rev. 669887