Alexandria  2.27.0
SDC-CH common library for the Euclid project
StringFunctions.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2012-2021 Euclid Science Ground Segment
3  *
4  * This library is free software; you can redistribute it and/or modify it under
5  * the terms of the GNU Lesser General Public License as published by the Free
6  * Software Foundation; either version 3.0 of the License, or (at your option)
7  * any later version.
8  *
9  * This library is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11  * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12  * details.
13  *
14  * You should have received a copy of the GNU Lesser General Public License
15  * along with this library; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18 
26 #include "StringFunctions.h"
27 
28 namespace Euclid {
29 namespace XYDataset {
30 
31 //
32 // Make sure the string does start with only one "/" character
33 //
35 
36  std::string output_str{};
37  size_t pos = input_str.find_first_not_of("/");
38  if (pos != std::string::npos && pos != 0) {
39  output_str = input_str.substr(pos);
40  output_str = "/" + output_str;
41  } else {
42  // no slash at the beginning
43  output_str = "/" + input_str;
44  }
45 
46  return (output_str);
47 }
48 
49 //
50 // Make sure the string does not start with a "/" character
51 //
53 
54  std::string output_str{};
55 
56  if (!input_str.empty()) {
57  size_t pos = input_str.find_first_not_of("/");
58  if (pos != 0) {
59  output_str = input_str.substr(pos);
60  } else {
61  // no slash
62  output_str = input_str;
63  }
64  }
65 
66  return (output_str);
67 }
68 
69 //
70 // Make sure the string finishes with a "/" character and only one
71 //
73 
74  std::string output_str{};
75 
76  size_t pos = input_str.find_last_not_of("/");
77  if (pos != input_str.length() - 1) {
78  // add one
79  output_str = input_str.substr(0, pos + 1) + "/";
80  } else {
81  // No slash at the end
82  output_str = input_str + "/";
83  }
84 
85  return (output_str);
86 }
87 
88 //
89 // Remove an extension, so any character after the last "." character
90 //
92 
93  std::string output_str{};
94 
95  if (!input_str.empty()) {
96  // Remove any file extension
97  size_t pos = input_str.find_last_of(".");
98  if (pos != std::string::npos) {
99  output_str = input_str.substr(0, pos);
100  } else {
101  output_str = input_str;
102  }
103  }
104 
105  return (output_str);
106 }
107 
108 //
109 // Remove all characters before the last "/" character
110 //
112 
113  std::string output_str{};
114 
115  if (!input_str.empty()) {
116  // Remove any file extension
117  size_t pos = input_str.find_last_of("/");
118  if (pos != std::string::npos) {
119  output_str = input_str.substr(pos + 1);
120  } else {
121  output_str = input_str;
122  }
123  }
124 
125  return (output_str);
126 }
127 
128 } // namespace XYDataset
129 } // end of namespace Euclid
T empty(T... args)
T find_first_not_of(T... args)
T find_last_not_of(T... args)
T find_last_of(T... args)
std::string checkNoBeginSlashes(const std::string &input_str)
std::string checkBeginSlashes(const std::string &input_str)
std::string removeExtension(const std::string &input_str)
std::string removeAllBeforeLastSlash(const std::string &input_str)
std::string checkEndSlashes(const std::string &input_str)
T length(T... args)
T substr(T... args)