ome-common  5.2.0-m2
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 <cstdarg>
50 #include <cstring>
51 
52 namespace ome
53 {
54  namespace common
55  {
56 
66  inline std::string
67  ltrim(const std::string& str)
68  {
69  std::string::size_type pos = str.find_first_not_of(" \r\n\t\v");
70  if (pos == std::string::npos)
71  return std::string();
72 
73  return str.substr(pos, std::string::npos);
74  }
75 
85  inline std::string
86  rtrim(const std::string& str)
87  {
88  std::string::size_type pos = str.find_last_not_of(" \r\n\t\v");
89  if (pos == std::string::npos)
90  return std::string();
91 
92  return str.substr(0, pos + 1);
93  }
94 
104  inline std::string
105  trim(const std::string& str)
106  {
107  std::string::size_type fpos = str.find_first_not_of(" \r\n\t\v");
108  if (fpos == std::string::npos)
109  return std::string();
110 
111  std::string::size_type lpos = str.find_last_not_of(" \r\n\t\v");
112  if (lpos == std::string::npos)
113  return std::string();
114 
115  return str.substr(fpos, lpos - fpos + 1);
116  }
117 
118  // C99/C++11 compatibility for MSVC users. MSVC does not have a
119  // current C99 or C++ standard library, so these functions are
120  // missing. Thanks to Valentin Milea.
121  // http://stackoverflow.com/questions/2915672/snprintf-and-visual-studio-2010
122 
123 #if defined(_MSC_VER) && !defined(OME_HAVE_SNPRINTF)
124 
125 # define snprintf c99_snprintf
126 # define vsnprintf c99_vsnprintf
127 
128 # include <stdio.h>
129 # include <stdarg.h>
130 
131  inline int
132  c99_vsnprintf(char* str, size_t size, const char* format, va_list ap)
133  {
134  int count = -1;
135 
136  if (size != 0)
137  count = _vsnprintf_s(str, size, _TRUNCATE, format, ap);
138  if (count == -1)
139  count = _vscprintf(format, ap);
140 
141  return count;
142  }
143 
144  inline int
145  c99_snprintf(char* str, size_t size, const char* format, ...)
146  {
147  int count;
148  va_list ap;
149 
150  va_start(ap, format);
151  count = vsnprintf(str, size, format, ap);
152  va_end(ap);
153 
154  return count;
155  }
156 
157 #endif // _MSC_VER && !OME_HAVE_SNPRINTF
158 
159  }
160 }
161 
162 #endif // OME_COMMON_STRING_H
163 
164 /*
165  * Local Variables:
166  * mode:C++
167  * End:
168  */
169 
std::string trim(const std::string &str)
Trim leading and trailing whitespace from a string.
Definition: string.h:105
Definition: boolean.h:48
std::string ltrim(const std::string &str)
Trim leading whitespace from a string.
Definition: string.h:67
std::string rtrim(const std::string &str)
Trim trailing whitespace from a string.
Definition: string.h:86