bioformats  5.1.1
string.h
Go to the documentation of this file.
1 /*
2  * #%L
3  * OME-COMMON C++ library for C++ compatibility/portability
4  * %%
5  * Copyright © 2006 - 2015 Open Microscopy Environment:
6  * - Massachusetts Institute of Technology
7  * - National Institutes of Health
8  * - University of Dundee
9  * - Board of Regents of the University of Wisconsin-Madison
10  * - Glencoe Software, Inc.
11  * %%
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions are met:
14  *
15  * 1. Redistributions of source code must retain the above copyright notice,
16  * this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright notice,
18  * this list of conditions and the following disclaimer in the documentation
19  * and/or other materials provided with the distribution.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
25  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31  * POSSIBILITY OF SUCH DAMAGE.
32  *
33  * The views and conclusions contained in the software and documentation are
34  * those of the authors and should not be interpreted as representing official
35  * policies, either expressed or implied, of any organization.
36  * #L%
37  */
38 
43 #ifndef OME_COMMON_STRING_H
44 # define OME_COMMON_STRING_H
45 
46 # include <ome/common/config.h>
47 
48 #include <string>
49 #include <cstring>
50 
51 namespace ome
52 {
53  namespace common
54  {
55 
65  inline std::string
66  ltrim(const std::string& str)
67  {
68  std::string::size_type pos = str.find_first_not_of(" \r\n\t\v");
69  if (pos == std::string::npos)
70  return std::string();
71 
72  return str.substr(pos, std::string::npos);
73  }
74 
84  inline std::string
85  rtrim(const std::string& str)
86  {
87  std::string::size_type pos = str.find_last_not_of(" \r\n\t\v");
88  if (pos == std::string::npos)
89  return std::string();
90 
91  return str.substr(0, pos + 1);
92  }
93 
103  inline std::string
104  trim(const std::string& str)
105  {
106  std::string::size_type fpos = str.find_first_not_of(" \r\n\t\v");
107  if (fpos == std::string::npos)
108  return std::string();
109 
110  std::string::size_type lpos = str.find_last_not_of(" \r\n\t\v");
111  if (lpos == std::string::npos)
112  return std::string();
113 
114  return str.substr(fpos, lpos - fpos + 1);
115  }
116 
117  // C99/C++11 compatibility for MSVC users. MSVC does not have a
118  // current C99 or C++ standard library, so these functions are
119  // missing. Thanks to Valentin Milea.
120  // http://stackoverflow.com/questions/2915672/snprintf-and-visual-studio-2010
121 
122 #ifdef _MSC_VER
123 
124 #define snprintf c99_snprintf
125 #define vsnprintf c99_vsnprintf
126 
127  inline int
128  c99_snprintf(char* str,
129  size_t size,
130  const char* format,
131  ...)
132  {
133  int count;
134  va_list ap;
135 
136  va_start(ap, format);
137  count = c99_vsnprintf(str, size, format, ap);
138  va_end(ap);
139 
140  return count;
141  }
142 
143  inline int
144  c99_vsnprintf(char* str,
145  size_t size,
146  const char* format,
147  va_list ap)
148  {
149  int count = -1;
150 
151  if (size != 0)
152  count = _vsnprintf_s(str, size, _TRUNCATE, format, ap);
153  if (count == -1)
154  count = _vscprintf(format, ap);
155 
156  return count;
157  }
158 
159 #endif // _MSC_VER
160 
161  }
162 }
163 
164 #endif // OME_COMMON_STRING_H
165 
166 /*
167  * Local Variables:
168  * mode:C++
169  * End:
170  */
171 
std::string trim(const std::string &str)
Trim leading and trailing whitespace from a string.
Definition: string.h:104
Open Microscopy Environment C++ implementation.
Definition: CoreMetadata.cpp:40
std::string ltrim(const std::string &str)
Trim leading whitespace from a string.
Definition: string.h:66
std::string rtrim(const std::string &str)
Trim trailing whitespace from a string.
Definition: string.h:85