blob: 77b0a3aece49da5637233fe6781bcb8be7652f4c [file] [log] [blame]
/**
* Copyright 2011-2015 Quickstep Technologies LLC.
* Copyright 2015 Pivotal Software, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
#define __STDC_FORMAT_MACROS
#include "utility/StringUtil.hpp"
#include <algorithm>
#include <cctype>
#include <cinttypes>
#include <cstddef>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <string>
#include <vector>
#include "glog/logging.h"
// NetBSD's libc has snprintf, but it doesn't show up in the std namespace
// for c++.
#ifndef __NetBSD__
using std::snprintf;
#endif
using std::string;
namespace quickstep {
std::string ToLower(const std::string& str) {
std::string lower_str(str.size(), ' ');
std::transform(str.begin(), str.end(), lower_str.begin(), tolower);
return lower_str;
}
std::string EscapeSpecialChars(const std::string& text) {
std::string new_text;
for (const char& c : text) {
switch (c) {
case '\'':
new_text.append("\\\'");
break;
case '\"':
new_text.append("\\\"");
break;
case '\?':
new_text.append("\\\?");
break;
case '\t':
new_text.append("\\t");
break;
case '\a':
new_text.append("\\a");
break;
case '\b':
new_text.append("\\b");
break;
case '\f':
new_text.append("\\f");
break;
case '\n':
new_text.append("\\n");
break;
case '\r':
new_text.append("\\r");
break;
case '\v':
new_text.append("\\v");
break;
default:
new_text += c;
}
}
return new_text;
}
bool ParseIntString(const std::string &int_string,
const char delimiter,
std::vector<int> *parsed_output) {
std::vector<int>::size_type original_size = parsed_output->size();
std::size_t pos = 0;
while (pos < int_string.size()) {
char *endptr = nullptr;
int element = std::strtol(int_string.c_str() + pos, &endptr, 10);
if ((endptr > int_string.c_str() + pos)
&& ((*endptr == delimiter) || (*endptr == '\0'))) {
parsed_output->push_back(element);
pos = endptr - int_string.c_str() + 1;
} else {
parsed_output->resize(original_size);
return false;
}
}
return true;
}
string ToZeroPaddedString(std::uint64_t val, int pad_width) {
DCHECK_GT(pad_width, 0);
const std::size_t str_len = pad_width + 1;
char *str = new char[str_len]();
int bytes = snprintf(str,
str_len,
"%0*" PRIu64,
pad_width,
val);
DCHECK_EQ(pad_width, bytes);
string result(str, pad_width);
delete[] str;
return result;
}
} // namespace quickstep