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 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276
|
// Copyright 2015, Tobias Hermann and the FunctionalPlus contributors.
// https://github.com/Dobiasd/FunctionalPlus
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
#pragma once
#include <fplus/container_common.hpp>
#include <fplus/generate.hpp>
#include <fplus/maybe.hpp>
#include <fplus/transform.hpp>
#include <iomanip>
#include <ios>
#include <sstream>
#include <string>
namespace fplus
{
// API search type: show : a -> String
// fwd bind count: 0
// 42 -> "42"
// Useful to simply show values, e.g.:
// Int to String
// Float to String
// Double to String
// Pair to String
// std::vector<std::list<T>> to String
// std::vector<T> to String
template <typename T>
std::string show(const T& x)
{
std::ostringstream ss;
ss << x;
return ss.str();
}
// string identity
// "foo" -> "foo"
inline
std::string show(const std::string& str)
{
return str;
}
template <typename T, typename A>
std::string show(const std::vector<T, A>& xs);
template <typename T, typename A>
std::string show(const std::list<T, A>& xs);
// {1, "one"} -> "(1, one)"
template <typename X, typename Y>
std::string show(const std::pair<X, Y>& p)
{
return std::string("(") + show(p.first) + ", " + show(p.second) + ")";
}
template <typename Container> std::string show_cont(const Container& xs);
template <typename T, typename A>
std::string show(const std::vector<T, A>& xs)
{
return show_cont(xs);
}
template <typename T, typename A>
std::string show(const std::list<T, A>& xs)
{
return show_cont(xs);
}
template <typename T, typename A>
std::string show(const std::set<T, A>& xs)
{
return show_cont(xs);
}
template <typename T, typename A>
std::string show(const std::deque<T, A>& xs)
{
return show_cont(xs);
}
// API search type: show_cont_with_frame_and_newlines : (String, String, String, [a], Int) -> String
// fwd bind count: 3
// show_cont_with_frame_and_newlines (",", "(", ")", [1, 2, 3, 4, 5], 2)
// == "(1,2)
// 3,4)
// 5)"
template <typename Container>
std::string show_cont_with_frame_and_newlines(
const std::string& separator,
const std::string& prefix, const std::string& suffix,
const Container& xs,
std::size_t new_line_every_nth_elem )
{
std::vector<std::string> elemStrs;
elemStrs.reserve(xs.size());
if (new_line_every_nth_elem == 0)
{
for (const auto& x : xs)
{
elemStrs.push_back(show(x));
}
}
else
{
std::size_t i = 0;
std::string newline =
std::string("\n") + std::string(prefix.size(), ' ');
for (const auto& x : xs)
{
if ( i && i % new_line_every_nth_elem == 0)
elemStrs.push_back(newline + show(x));
else
elemStrs.push_back(show(x));
++i;
}
}
return prefix + join(separator, elemStrs) + suffix;
}
// API search type: show_cont_with_frame : (String, String, String, [a]) -> String
// fwd bind count: 3
// show_cont_with_frame (" => ", "{", "}", [1, 2, 3]) == "{1 => 2 => 3}"
template <typename Container>
std::string show_cont_with_frame(
const std::string& separator,
const std::string& prefix, const std::string& suffix,
const Container& xs)
{
return
show_cont_with_frame_and_newlines( separator, prefix, suffix, xs, 0);
}
// API search type: show_cont_with : (String, [a]) -> String
// fwd bind count: 1
// show_cont_with( " - ", [1, 2, 3]) == "[1 - 2 - 3]"
template <typename Container>
std::string show_cont_with(const std::string& separator, const Container& xs)
{
return show_cont_with_frame(separator, "[", "]", xs);
}
// API search type: show_cont : [a] -> String
// fwd bind count: 0
// show_cont [1, 2, 3] -> "[1, 2, 3]"
// Can i.a show std::vector and std::map.
template <typename Container>
std::string show_cont(const Container& xs)
{
return show_cont_with(", ", xs);
}
// API search type: show_maybe : Maybe a -> String
// fwd bind count: 0
// show_maybe(Just 42) -> "Just 42"
template <typename T>
std::string show_maybe(const maybe<T>& maybe)
{
if (is_nothing(maybe))
return "Nothing";
else
return std::string("Just " + show(unsafe_get_just(maybe)));
}
// API search type: show_result : Result a b -> String
// fwd bind count: 0
// show_result(Ok 42) -> "Ok 42"
// show_result(Error "fail") -> "Error fail"
template <typename Ok, typename Error>
std::string show_result(const result<Ok, Error>& result)
{
if (is_error(result))
return std::string("Error " + show(unsafe_get_error(result)));
else
return std::string("Ok " + show(unsafe_get_ok(result)));
}
// API search type: show_float : (Int, Int, Float) -> String
// fwd bind count: 2
// Can be used to show floating point values in a specific format
// (Float to String, Double to String etc.)
// Examples:
// const double pi = 3.14159
// show_float<double>(0, 3, pi) == "3.142"
// show_float<double>(1, 3, pi) == "3.142"
// show_float<double>(2, 3, pi) == "03.142"
// show_float<double>(3, 3, pi) == "003.142"
// show_float<double>(1, 2, pi) == "3.14"
// show_float<double>(1, 4, pi) == "3.1416"
// show_float<double>(1, 7, pi) == "3.1415900"
// show_float<double>(0, 3, -pi) == "-3.142"
// show_float<double>(1, 3, -pi) == "-3.142"
// show_float<double>(2, 3, -pi) == "-3.142"
// show_float<double>(3, 3, -pi) == "-03.142"
// show_float<double>(4, 3, -pi) == "-003.142"
// show_float<double>(0, 3, 0.142) == "0.142";
// show_float<double>(1, 3, 0.142) == "0.142";
// show_float<double>(2, 3, 0.142) == "00.142";
// fill_left(8, ' ', show_float<double>(0, 3, -pi)) == " -3.142"
template <typename T>
std::string show_float(
std::size_t min_left_chars, std::size_t right_char_count, const T& x)
{
bool is_negative = x < 0;
std::size_t min_left_chars_final =
is_negative && min_left_chars > 0
? min_left_chars - 1
: min_left_chars;
std::stringstream stream;
stream
<< std::fixed
<< std::setprecision(static_cast<int>(right_char_count))
<< std::abs(x);
std::string s = stream.str();
std::size_t min_dest_length = min_left_chars_final + 1 + right_char_count;
std::string result = fill_left('0', min_dest_length, s);
if (is_negative)
{
result = std::string("-") + result;
}
return result;
}
// API search type: show_float_fill_left : (Char, Int, Int, Float) -> String
// fwd bind count: 3
// Can be used to show floating point values in a specific precision
// left-padded with some character.
// (Float to String, Double to String etc.)
// Examples:
// const double pi = 3.14159
// show_float_fill_left<double>(' ', 8, 3, pi) == " 3.142"
// show_float_fill_left<double>(' ', 8, 6, pi) == "3.141590"
// show_float_fill_left<double>(' ', 8, 3, -pi) == " -3.142"
// show_float_fill_left<double>(' ', 2, 3, -pi) == "-3.142"
template <typename T>
std::string show_float_fill_left(const std::string::value_type& filler,
std::size_t min_size, std::size_t right_char_count, const T& x)
{
return fill_left(filler, min_size, show_float<T>(0, right_char_count, x));
}
// API search type: show_fill_left : (Char, Int, a) -> String
// fwd bind count: 2
// Convert some value to a string with left-padded with some character.
// (Int to String etc.)
// Examples:
// show_fill_left<int>(' ', 4, 3) == " 3"
// show_fill_left<int>('0', 4, 3) == "0003"
// show_fill_left<int>(' ', 4, 12345) == "12345"
template <typename T>
std::string show_fill_left(
const std::string::value_type& filler, std::size_t min_size, const T& x)
{
return fill_left(filler, min_size, show<T>(x));
}
// API search type: show_fill_right : (Char, Int, a) -> String
// fwd bind count: 2
// Convert some value to a string with left-padded with some character.
// (Int to String etc.)
// Examples:
// show_fill_right<int>(' ', 4, 3) == "3 "
// show_fill_right<int>(' ', 4, 12345) == "12345"
template <typename T>
std::string show_fill_right(const std::string::value_type& filler,
std::size_t min_size, const T& x)
{
return fill_right(filler, min_size, show<T>(x));
}
} // namespace fplus
|