StrUtil.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2017 Fraunhofer Institute for Manufacturing Engineering and Automation (IPA)
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9 
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 
18 #include <cob_utilities/StrUtil.h>
19 #include <algorithm>
20 
21 std::string StringToUpper(std::string strToConvert) {
22  for(unsigned int i=0;i<strToConvert.length();i++)
23  {
24  strToConvert[i] = toupper(strToConvert[i]);
25  }
26  return strToConvert;
27 }
28 
29 std::string StringToLower(std::string strToConvert) {
30  for(unsigned int i=0;i<strToConvert.length();i++)
31  {
32  strToConvert[i] = tolower(strToConvert[i]);
33  }
34  return strToConvert;
35 }
36 
37 std::string NumToString(const int n) {
38  std::stringstream ss;
39  ss << n;
40  return ss.str();
41 }
42 
43 std::string NumToString(const unsigned int n) {
44  std::stringstream ss;
45  ss << n;
46  return ss.str();
47 }
48 
49 std::string NumToString(const long l) {
50  std::stringstream ss;
51  ss << l;
52  return ss.str();
53 }
54 
55 std::string NumToString(const float f, unsigned int width, unsigned int precise) {
56  std::stringstream ss;
57  ss << std::setw(width) << std::setprecision(precise) << f;
58  return ss.str();
59 }
60 
61 std::string NumToString(const double d, unsigned int width, unsigned int precise) {
62  std::stringstream ss;
63  ss << std::setw(width) << std::setprecision(precise) << d;
64  return ss.str();
65 }
66 
70 char* itoa( int value, char* result, int base )
71 {
72  // check that the base if valid
73  if (base < 2 || base > 16) {
74  *result = 0; return result;
75  }
76 
77  char* out = result;
78  int quotient = value;
79 
80  do {
81  *out = "0123456789abcdef"[ std::abs( quotient % base ) ];
82  ++out;
83  quotient /= base;
84  } while ( quotient );
85 
86  // Only apply negative sign for base 10
87  if ( value < 0 && base == 10) *out++ = '-';
88 
89  std::reverse( result, out );
90  *out = 0;
91  return result;
92 }
93 
97 std::string itoa(int value, int base)
98 {
99  enum { kMaxDigits = 35 };
100  std::string buf;
101 
102  buf.reserve( kMaxDigits ); // Pre-allocate enough space.
103 
104  // check that the base if valid
105  if (base < 2 || base > 16) return buf;
106 
107  int quotient = value;
108 
109  // Translating number to string with base:
110  do {
111  buf += "0123456789abcdef"[ std::abs( quotient % base ) ];
112  quotient /= base;
113  } while ( quotient );
114 
115  // Append the negative sign for base 10
116  if ( value < 0 && base == 10) buf += '-';
117  std::reverse( buf.begin(), buf.end() );
118  return buf;
119 }
120 
char * itoa(int value, char *result, int base)
Definition: StrUtil.cpp:70
std::string NumToString(const int n)
Definition: StrUtil.cpp:37
std::string StringToUpper(std::string strToConvert)
Definition: StrUtil.cpp:21
std::string StringToLower(std::string strToConvert)
Definition: StrUtil.cpp:29


cob_utilities
Author(s): Christian Connette
autogenerated on Wed Apr 7 2021 02:11:52