souffle  2.0.2-371-g6315b36
Functions | Variables
souffle::profile::Tools Namespace Reference

Functions

std::string cleanJsonOut (double val)
 Convert doubles to NaN or scientific notation. More...
 
std::string cleanJsonOut (std::string value)
 escape escapes and quotes, and remove surrounding quotes More...
 
std::string cleanString (std::string val)
 Remove
and \t characters,
and \t sequence of two chars, and wrapping quotes. More...
 
bool file_exists (const std::string &name)
 
std::string formatMemory (uint64_t kbytes)
 
std::string formatNum (double amount)
 
std::string formatNum (int precision, int64_t amount)
 
std::vector< std::vector< std::string > > formatTable (Table table, int precision)
 
std::string formatTime (std::chrono::microseconds number)
 
std::vector< std::string > split (std::string toSplit, std::string delimiter)
 split on the delimiter More...
 
std::string trimWhitespace (std::string str)
 

Variables

static const std::vector< std::string > abbreviations
 

Function Documentation

◆ cleanJsonOut() [1/2]

std::string souffle::profile::Tools::cleanJsonOut ( double  val)
inline

Convert doubles to NaN or scientific notation.

Definition at line 253 of file StringUtils.h.

253  {
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 }

References souffle::profile::ss.

◆ cleanJsonOut() [2/2]

std::string souffle::profile::Tools::cleanJsonOut ( std::string  value)
inline

escape escapes and quotes, and remove surrounding quotes

Definition at line 232 of file StringUtils.h.

232  {
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 }

Referenced by genJsonAtoms(), genJsonConfiguration(), souffle::profile::Tui::genJsonRelations(), souffle::genJsonRules(), souffle::genJsonUsage(), and TEST().

◆ cleanString()

std::string souffle::profile::Tools::cleanString ( std::string  val)
inline

Remove
and \t characters,
and \t sequence of two chars, and wrapping quotes.

Definition at line 206 of file StringUtils.h.

206  {
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 }

Referenced by genJsonAtoms(), and TEST().

◆ file_exists()

bool souffle::profile::Tools::file_exists ( const std::string &  name)
inline

Definition at line 196 of file StringUtils.h.

196  {
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 }

Referenced by outputHtml().

◆ formatMemory()

std::string souffle::profile::Tools::formatMemory ( uint64_t  kbytes)
inline

Definition at line 97 of file StringUtils.h.

97  {
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 }

Referenced by memoryUsage(), and TEST().

◆ formatNum() [1/2]

std::string souffle::profile::Tools::formatNum ( double  amount)
inline

Definition at line 40 of file StringUtils.h.

40  {
41  std::stringstream ss;
42  ss << amount;
43  return ss.str();
44 }

References souffle::profile::ss.

Referenced by souffle::profile::ProgramRun::formatNum(), graphBySize(), TEST(), souffle::profile::Cell< double >::toString(), and souffle::profile::Cell< long >::toString().

◆ formatNum() [2/2]

std::string souffle::profile::Tools::formatNum ( int  precision,
int64_t  amount 
)
inline

Definition at line 46 of file StringUtils.h.

46  {
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 }

References abbreviations, and i.

◆ formatTable()

std::vector<std::vector<std::string> > souffle::profile::Tools::formatTable ( Table  table,
int  precision 
)
inline

Definition at line 145 of file StringUtils.h.

145  {
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 }

References souffle::profile::Table::getRows().

Referenced by souffle::profile::ProgramRun::formatTable(), id(), iterRel(), iterRul(), rel(), relRul(), rul(), setupTabCompletion(), usageRelation(), usageRule(), and verRul().

Here is the call graph for this function:

◆ formatTime()

std::string souffle::profile::Tools::formatTime ( std::chrono::microseconds  number)
inline

Definition at line 108 of file StringUtils.h.

108  {
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 }

Referenced by souffle::profile::ProgramRun::formatTime(), TEST(), souffle::profile::Cell< std::chrono::microseconds >::toString(), and usage().

◆ split()

std::vector<std::string> souffle::profile::Tools::split ( std::string  toSplit,
std::string  delimiter 
)
inline

split on the delimiter

Definition at line 162 of file StringUtils.h.

162  {
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 }

Referenced by souffle::genJsonRules(), souffle::genJsonUsage(), souffle::profile::Cli::parse(), souffle::profile::Tui::runProf(), TEST(), verGraph(), and verRul().

◆ trimWhitespace()

std::string souffle::profile::Tools::trimWhitespace ( std::string  str)
inline

Definition at line 182 of file StringUtils.h.

182  {
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 }

References str.

Referenced by souffle::profile::Tui::runProf(), and TEST().

Variable Documentation

◆ abbreviations

const std::vector<std::string> souffle::profile::Tools::abbreviations
static
Initial value:
{
"K", "M", "B", "t", "q", "Q", "s", "S", "o", "n", "d", "U"}

Definition at line 37 of file StringUtils.h.

Referenced by formatNum().

str
const std::string & str
Definition: json11.h:662
i
size_t i
Definition: json11.h:663
souffle::profile::Tools::abbreviations
static const std::vector< std::string > abbreviations
Definition: StringUtils.h:37
souffle::profile::ss
class souffle::profile::Tui ss
Definition: Tui.h:336