souffle  2.0.2-371-g6315b36
StringUtils.h
Go to the documentation of this file.
1 /*
2  * Souffle - A Datalog Compiler
3  * Copyright (c) 2016, The Souffle Developers. All rights reserved
4  * Licensed under the Universal Permissive License v 1.0 as shown at:
5  * - https://opensource.org/licenses/UPL
6  * - <souffle root>/licenses/SOUFFLE-UPL.txt
7  */
8 
9 #pragma once
10 
12 #include "souffle/profile/Row.h"
13 #include "souffle/profile/Table.h"
14 #include <algorithm>
15 #include <chrono>
16 #include <cmath>
17 #include <cstdio>
18 #include <fstream>
19 #include <iomanip>
20 #include <ios>
21 #include <memory>
22 #include <sstream>
23 #include <string>
24 #include <vector>
25 #include <unistd.h>
26 
27 #include <sys/stat.h>
28 
29 namespace souffle {
30 namespace profile {
31 
32 /*
33  * A series of functions necessary throughout the code
34  * Mostly string manipulation
35  */
36 namespace Tools {
37 static const std::vector<std::string> abbreviations{
38  "K", "M", "B", "t", "q", "Q", "s", "S", "o", "n", "d", "U"};
39 
40 inline std::string formatNum(double amount) {
41  std::stringstream ss;
42  ss << amount;
43  return ss.str();
44 }
45 
46 inline std::string formatNum(int precision, int64_t amount) {
47  // assumes number is < 999*10^12
48  if (amount == 0) {
49  return "0";
50  }
51 
52  if (precision <= 0) {
53  return std::to_string(amount);
54  }
55 
56  std::string result;
57 
58  if (amount < 1000) {
59  return std::to_string(amount);
60  }
61 
62  for (size_t i = 0; i < abbreviations.size(); ++i) {
63  if (amount > std::pow(1000, i + 2)) {
64  continue;
65  }
66 
67  double r = amount / std::pow(1000, i + 1);
68  result = std::to_string(r);
69 
70  if (r >= 100) { // 1000 > result >= 100
71 
72  switch (precision) {
73  case 1: result = result.substr(0, 1) + "00"; break;
74  case 2: result = result.substr(0, 2) + "0"; break;
75  case 3: result = result.substr(0, 3); break;
76  default: result = result.substr(0, precision + 1);
77  }
78  } else if (r >= 10) { // 100 > result >= 10
79  switch (precision) {
80  case 1: result = result.substr(0, 1) + "0"; break;
81  case 2: result = result.substr(0, 2); break;
82  default: result = result.substr(0, precision + 1);
83  }
84  } else { // 10 > result > 0
85  switch (precision) {
86  case 1: result = result.substr(0, 1); break;
87  default: result = result.substr(0, precision + 1);
88  }
89  }
90  result += abbreviations.at(i);
91  return result;
92  }
93  // If we ever have integers too large to handle, fall back to this
94  return std::to_string(amount);
95 }
96 
97 inline std::string formatMemory(uint64_t kbytes) {
98  if (kbytes < 1024L * 2) {
99  return std::to_string(kbytes) + "kB";
100  } else if (kbytes < 1024L * 1024 * 2) {
101  return std::to_string(kbytes / 1024) + "MB";
102  } else if (kbytes < 1024L * 1024 * 1024 * 2) {
103  return std::to_string(kbytes / (1024 * 1024)) + "GB";
104  }
105  return std::to_string(kbytes / (1024 * 1024 * 1024)) + "TB";
106 }
107 
108 inline std::string formatTime(std::chrono::microseconds number) {
109  uint64_t sec = number.count() / 1000000;
110  if (sec >= 100) {
111  uint64_t min = std::floor(sec / 60);
112  if (min >= 100) {
113  uint64_t hours = std::floor(min / 60);
114  if (hours >= 100) {
115  uint64_t days = std::floor(hours / 24);
116  return std::to_string(days) + "D";
117  }
118  return std::to_string(hours) + "h";
119  }
120  if (min < 10) {
121  // temp should always be 1 digit long
122  uint64_t temp = std::floor((sec - (min * 60.0)) * 10.0 / 6.0);
123  return std::to_string(min) + "." + std::to_string(temp).substr(0, 1) + "m";
124  }
125  return std::to_string(min) + "m";
126  } else if (sec >= 10) {
127  return std::to_string(sec) + "s";
128  } else if (number.count() >= 1000000) {
129  std::string temp = std::to_string(number.count() / 100);
130  return temp.substr(0, 1) + "." + temp.substr(1, 2) + "s";
131  } else if (number.count() >= 100000) {
132  std::string temp = std::to_string(number.count() / 1000);
133  return "." + temp.substr(0, 3) + "s";
134  } else if (number.count() >= 10000) {
135  std::string temp = std::to_string(number.count() / 1000);
136  return ".0" + temp.substr(0, 2) + "s";
137  } else if (number.count() >= 1000) {
138  std::string temp = std::to_string(number.count() / 1000);
139  return ".00" + temp.substr(0, 1) + "s";
140  }
141 
142  return ".000s";
143 }
144 
145 inline std::vector<std::vector<std::string>> formatTable(Table table, int precision) {
146  std::vector<std::vector<std::string>> result;
147  for (auto& row : table.getRows()) {
148  std::vector<std::string> result_row;
149  for (auto& cell : row->getCells()) {
150  if (cell != nullptr) {
151  result_row.push_back(cell->toString(precision));
152  } else {
153  result_row.push_back("-");
154  }
155  }
156  result.push_back(result_row);
157  }
158  return result;
159 }
160 
161 /** @brief split on the delimiter */
162 inline std::vector<std::string> split(std::string toSplit, std::string delimiter) {
163  std::vector<std::string> elements;
164  std::string::size_type lastPos = 0;
165  auto pos = toSplit.find(delimiter, lastPos);
166 
167  while (pos != std::string::npos) {
168  if (pos > 0) {
169  std::string newElement = toSplit.substr(lastPos, pos - lastPos);
170  elements.push_back(newElement);
171  }
172  lastPos = pos + delimiter.size();
173  pos = toSplit.find(delimiter, lastPos);
174  }
175  if (lastPos < toSplit.size()) {
176  elements.push_back(toSplit.substr(lastPos));
177  }
178 
179  return elements;
180 }
181 
182 inline std::string trimWhitespace(std::string str) {
183  std::string whitespace = " \t";
184  size_t first = str.find_first_not_of(whitespace);
185  if (first != std::string::npos) {
186  str.erase(0, first);
187  size_t last = str.find_last_not_of(whitespace);
188  str.erase(last + 1);
189  } else {
190  str.clear();
191  }
192 
193  return str;
194 }
195 
196 inline bool file_exists(const std::string& name) {
197  struct stat buffer = {};
198  if (stat(name.c_str(), &buffer) == 0) {
199  if ((buffer.st_mode & S_IFMT) != 0) {
200  return true;
201  }
202  }
203  return false;
204 }
205 /** @brief Remove \n and \t characters, \n and \t sequence of two chars, and wrapping quotes */
206 inline std::string cleanString(std::string val) {
207  if (val.size() < 2) {
208  return val;
209  }
210 
211  size_t start_pos = 0;
212  while ((start_pos = val.find('\\', start_pos)) != std::string::npos) {
213  val.erase(start_pos, 1);
214  if (start_pos < val.size()) {
215  if (val[start_pos] == 'n' || val[start_pos] == 't') {
216  val.replace(start_pos, 1, " ");
217  }
218  }
219  }
220 
221  if (val.at(0) == '"' && val.at(val.size() - 1) == '"') {
222  val = val.substr(1, val.size() - 2);
223  }
224 
225  std::replace(val.begin(), val.end(), '\n', ' ');
226  std::replace(val.begin(), val.end(), '\t', ' ');
227 
228  return val;
229 }
230 
231 /** @brief escape escapes and quotes, and remove surrounding quotes */
232 inline std::string cleanJsonOut(std::string value) {
233  if (value.size() >= 2) {
234  if (value.at(0) == '"' && value.at(value.size() - 1) == '"') {
235  value = value.substr(1, value.size() - 2);
236  }
237  }
238 
239  size_t start_pos = 0;
240  while ((start_pos = value.find('\\', start_pos)) != std::string::npos) {
241  value.replace(start_pos, 1, "\\\\");
242  start_pos += 2;
243  }
244  start_pos = 0;
245  while ((start_pos = value.find('"', start_pos)) != std::string::npos) {
246  value.replace(start_pos, 1, "\\\"");
247  start_pos += 2;
248  }
249  return value;
250 }
251 
252 /** @brief Convert doubles to NaN or scientific notation */
253 inline std::string cleanJsonOut(double val) {
254  if (std::isnan(val)) {
255  return "NaN";
256  }
257  std::ostringstream ss;
258  ss << std::scientific << std::setprecision(6) << val;
259  return ss.str();
260 }
261 } // namespace Tools
262 
263 } // namespace profile
264 } // namespace souffle
Table.h
souffle::profile::Tools::file_exists
bool file_exists(const std::string &name)
Definition: StringUtils.h:196
CellInterface.h
souffle::profile::Tools::cleanString
std::string cleanString(std::string val)
Remove and \t characters, and \t sequence of two chars, and wrapping quotes.
Definition: StringUtils.h:206
souffle::profile::Tools::formatTable
std::vector< std::vector< std::string > > formatTable(Table table, int precision)
Definition: StringUtils.h:145
souffle::profile::Tools::trimWhitespace
std::string trimWhitespace(std::string str)
Definition: StringUtils.h:182
souffle::profile::Tools::formatMemory
std::string formatMemory(uint64_t kbytes)
Definition: StringUtils.h:97
str
const std::string & str
Definition: json11.h:662
souffle::profile::Tools::formatNum
std::string formatNum(double amount)
Definition: StringUtils.h:40
i
size_t i
Definition: json11.h:663
souffle::profile::Tools::cleanJsonOut
std::string cleanJsonOut(std::string value)
escape escapes and quotes, and remove surrounding quotes
Definition: StringUtils.h:232
souffle::profile::Tools::split
std::vector< std::string > split(std::string toSplit, std::string delimiter)
split on the delimiter
Definition: StringUtils.h:162
souffle::profile::Tools::formatTime
std::string formatTime(std::chrono::microseconds number)
Definition: StringUtils.h:108
souffle::profile::Tools::abbreviations
static const std::vector< std::string > abbreviations
Definition: StringUtils.h:37
souffle
Definition: AggregateOp.h:25
Row.h
souffle::profile::Table::getRows
std::vector< std::shared_ptr< Row > > getRows()
Definition: Table.h:34
souffle::profile::Table
Definition: Table.h:24
souffle::profile::ss
class souffle::profile::Tui ss
Definition: Tui.h:336