1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
|
/* M-runtime for c++
* Copyright (C) 2005-2008 Vladimir Menshakov
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "fmt.h"
#include "chunk.h"
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <ctype.h>
#include <algorithm>
#include "exception.h"
#if defined _WINDOWS
# if !defined vsnprintf
# define vsnprintf _vsnprintf
# endif
/*
# include <malloc.h>
# if !defined alloca
# define alloca _alloca
# endif
*/
#endif
using namespace mrt;
#define FORMAT_BUFFER_SIZE 1024
//#include "logger.h"
const std::string mrt::format_string(const char *fmt, ...) {
va_list ap;
//try static buffer on the stack to avoid malloc calls
char static_buf[FORMAT_BUFFER_SIZE];
va_start(ap, fmt);
int r = vsnprintf (static_buf, FORMAT_BUFFER_SIZE - 1, fmt, ap);
va_end(ap);
if (r > -1 && r <= FORMAT_BUFFER_SIZE)
return std::string(static_buf, r);
int size = FORMAT_BUFFER_SIZE * 2;
mrt::Chunk buf;
while(true) {
buf.set_size(size);
va_start(ap, fmt);
int r = vsnprintf ((char *)buf.get_ptr(), size - 1, fmt, ap);
va_end(ap);
if (r > -1 && r <= size)
return std::string((char *)buf.get_ptr(), r);
size *= 2;
}
}
void mrt::trim(std::string &str, const std::string chars) {
size_t i = str.find_first_not_of(chars);
if (i > 0)
str.erase(0, i);
i = str.find_last_not_of(chars);
if (i != str.npos)
str.erase(i + 1, str.size());
}
void mrt::join(std::string &result, const std::vector<std::string>& array, const std::string &delimiter, const size_t limit) {
result.clear();
if (array.empty())
return;
size_t n = array.size();
if (limit > 0 && limit < n)
n = limit;
--n;
for(size_t i = 0; i < n; ++i) {
result += array[i];
result += delimiter;
}
result += array[n];
}
void mrt::split(std::vector<std::string> & result, const std::string &str, const std::string &delimiter, const size_t limit) {
result.clear();
std::string::size_type pos = 0, p;
size_t n = limit;
while(pos < str.size()) {
do {
p = str.find(delimiter, pos);
if (p == pos) {
result.push_back(std::string());
p += delimiter.size();
pos += delimiter.size();
}
} while(p < str.size() && p == pos);
if (p != std::string::npos)
result.push_back(str.substr(pos, p - pos));
else {
result.push_back(str.substr(pos));
break;
}
if (n > 0) {
if (--n == 0) {
result[result.size() - 1] += str.substr(p);
break;
}
}
pos = p + delimiter.size();
}
if (limit)
result.resize(limit);
}
void mrt::to_upper(std::string &str) {
std::transform(str.begin(), str.end(), str.begin(), toupper);
}
void mrt::to_lower(std::string &str) {
std::transform(str.begin(), str.end(), str.begin(), tolower);
}
void mrt::replace(std::string &str, const std::string &from, const std::string &to, const size_t limit) {
if (str.empty())
return;
if (from.empty())
throw_ex(("replace string must not be empty"));
std::string::size_type pos = 0;
size_t n = limit;
while(pos < str.size()) {
pos = str.find(from, pos);
if (pos == str.npos)
break;
str.replace(pos, from.size(), to);
pos += from.size() - to.size() + 1;
if (n > 0) {
if (--n == 0) {
break;
}
}
}
}
|