1/*
2 * Copyright (c) 2015, Intel Corporation
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
6 *
7 * * Redistributions of source code must retain the above copyright notice,
8 * this list of conditions and the following disclaimer.
9 * * Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * * Neither the name of Intel Corporation nor the names of its contributors
13 * may be used to endorse or promote products derived from this software
14 * without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28
29#ifndef STRING_UTIL_H
30#define STRING_UTIL_H
31
32#include "ue2common.h"
33#include <iomanip>
34#include <string>
35#include <sstream>
36#include <vector>
37#include <cstring>
38#include <iostream>
39
40//
41// Utility functions
42//
43
44// read a string in and convert it to another type, anything supported
45// by stringstream
46template<typename T>
47inline bool fromString(const std::string &s, T& val)
48{
49 std::istringstream i(s);
50 char c;
51 if (!(i >> val) || i.get(c)) {
52 return false;
53 }
54 return true;
55}
56
57// read in a comma-separated set of values: very simple impl, not for
58// external consumption
59template<typename T>
60inline bool strToList(const std::string &s, std::vector<T>& out)
61{
62 std::istringstream i(s);
63 char c;
64 do {
65 T val;
66 if (!(i >> val)) {
67 break;
68 }
69
70 out.push_back(val);
71 } while (i.get(c) && c == ',');
72
73 return !out.empty();
74}
75
76// return a nicely escaped version of a string: this should probably become
77// an IO manipulator or something
78UNUSED static
79const std::string printable(const std::string &in) {
80 std::ostringstream oss;
81 for (size_t i = 0; i < in.size(); ++i) {
82 unsigned char c = in[i];
83 if (c == '\"') {
84 oss << "\\\"";
85 } else if (c == '\n') {
86 oss << "\\n";
87 } else if (c == '\t') {
88 oss << "\\t";
89 } else if (c == '\r') {
90 oss << "\\r";
91 } else if (0x20 <= c && c <= 0x7e && c != '\\') {
92 oss << c;
93 } else {
94 oss << "\\x"
95 << std::hex << std::setw(2) << std::setfill('0')
96 << (unsigned)(in[i] & 0xff)
97 << std::dec;
98 }
99 }
100 return oss.str();
101}
102
103template<typename it_t>
104void prettyPrintRange(std::ostream &out, it_t begin, it_t end) {
105 bool in_range = false;
106 it_t it = begin;
107 it_t itp = it;
108
109 for (; it != end; itp = it++) {
110 if (it != begin && *it == *itp + 1) {
111 in_range = true;
112 continue;
113 } else if (it != begin) {
114 if (in_range) {
115 out << "-" << *itp;
116 }
117
118 out << ", ";
119 in_range = false;
120 }
121
122 out << *it;
123 }
124
125 if (in_range) {
126 out << "-" << *itp;
127 }
128}
129
130#endif // STRING_UTIL_H
131