11#include " helpers.hpp"
22
3- char *Helpers::itoa (int value, char *result, int base)
4- {
5- // check that the base if valid
6- if (base < 2 || base > 36 )
7- {
8- *result = ' \0 ' ;
9- return result;
10- }
3+ char * Helpers::itoa (int value, char * result, int base) {
4+ // check that the base if valid
5+ if (base < 2 || base > 36 ) {
6+ *result = ' \0 ' ;
7+ return result;
8+ }
119
12- char *ptr = result, *ptr1 = result, tmp_char;
13- int tmp_value;
10+ char *ptr = result, *ptr1 = result, tmp_char;
11+ int tmp_value;
1412
15- do
16- {
17- tmp_value = value;
18- value /= base;
19- *ptr++ = " zyxwvutsrqponmlkjihgfedcba9876543210123456789abcdefghijklmnopqrstuvwxyz" [35 + (tmp_value - value * base)];
20- } while (value);
13+ do {
14+ tmp_value = value;
15+ value /= base;
16+ *ptr++ =
17+ " zyxwvutsrqponmlkjihgfedcba9876543210123456789abcdefghijklmnopqrstuvwxy"
18+ " z" [35 + (tmp_value - value * base)];
19+ } while (value);
2120
22- // Apply negative sign
23- if (tmp_value < 0 )
24- *ptr++ = ' -' ;
25- *ptr-- = ' \0 ' ;
26- while (ptr1 < ptr)
27- {
28- tmp_char = *ptr;
29- *ptr-- = *ptr1;
30- *ptr1++ = tmp_char;
31- }
32- return result;
21+ // Apply negative sign
22+ if (tmp_value < 0 )
23+ *ptr++ = ' -' ;
24+ *ptr-- = ' \0 ' ;
25+ while (ptr1 < ptr) {
26+ tmp_char = *ptr;
27+ *ptr-- = *ptr1;
28+ *ptr1++ = tmp_char;
29+ }
30+ return result;
3331}
3432
35- void split (const std::string &str, const std::string &splitBy, std::vector<std::string> &tokens)
36- {
37- /* Store the original string in the array, so we can loop the rest
38- * of the algorithm. */
39- tokens.emplace_back (str);
33+ void split (const std::string& str,
34+ const std::string& splitBy,
35+ std::vector<std::string>& tokens) {
36+ /* Store the original string in the array, so we can loop the rest
37+ * of the algorithm. */
38+ tokens.emplace_back (str);
4039
41- // Store the split index in a 'size_t' (unsigned integer) type.
42- size_t splitAt;
43- // Store the size of what we're splicing out.
44- size_t splitLen = splitBy.size ();
45- // Create a string for temporarily storing the fragment we're processing.
46- std::string frag;
47- // Loop infinitely - break is internal.
48- while (true )
49- {
50- /* Store the last string in the vector, which is the only logical
51- * candidate for processing. */
52- frag = tokens.back ();
53- /* The index where the split is. */
54- splitAt = frag.find (splitBy);
55- // If we didn't find a new split point...
56- if (splitAt == std::string::npos)
57- {
58- // Break the loop and (implicitly) return.
59- break ;
60- }
61- /* Put everything from the left side of the split where the string
62- * being processed used to be. */
63- tokens.back () = frag.substr (0 , splitAt);
64- /* Push everything from the right side of the split to the next empty
65- * index in the vector. */
66- tokens.emplace_back (frag.substr (splitAt + splitLen, frag.size () - (splitAt + splitLen)));
40+ // Store the split index in a 'size_t' (unsigned integer) type.
41+ size_t splitAt;
42+ // Store the size of what we're splicing out.
43+ size_t splitLen = splitBy.size ();
44+ // Create a string for temporarily storing the fragment we're processing.
45+ std::string frag;
46+ // Loop infinitely - break is internal.
47+ while (true ) {
48+ /* Store the last string in the vector, which is the only logical
49+ * candidate for processing. */
50+ frag = tokens.back ();
51+ /* The index where the split is. */
52+ splitAt = frag.find (splitBy);
53+ // If we didn't find a new split point...
54+ if (splitAt == std::string::npos) {
55+ // Break the loop and (implicitly) return.
56+ break ;
6757 }
58+ /* Put everything from the left side of the split where the string
59+ * being processed used to be. */
60+ tokens.back () = frag.substr (0 , splitAt);
61+ /* Push everything from the right side of the split to the next empty
62+ * index in the vector. */
63+ tokens.emplace_back (
64+ frag.substr (splitAt + splitLen, frag.size () - (splitAt + splitLen)));
65+ }
6866}
6967
70- std::vector<std::string> Helpers::split (const std::string &s, char delimiter)
71- {
72- std::vector<std::string> parts;
73- std::string part;
74- std::istringstream tokenStream (s);
75- while (std::getline (tokenStream, part, delimiter))
76- {
77- parts.push_back (part);
78- }
79- return parts;
68+ std::vector<std::string> Helpers::split (const std::string& s, char delimiter) {
69+ std::vector<std::string> parts;
70+ std::string part;
71+ std::istringstream tokenStream (s);
72+ while (std::getline (tokenStream, part, delimiter)) {
73+ parts.push_back (part);
74+ }
75+ return parts;
8076}
8177
82- void Helpers::update_progress_bar (int progress, int total)
83- {
84- int barWidth = 70 ;
78+ void Helpers::update_progress_bar (int progress, int total) {
79+ int barWidth = 70 ;
8580
86- std::cout << " \r [" ;
87- int pos = barWidth * progress / total;
88- for (int i = 0 ; i < barWidth; ++i)
89- {
90- if (i < pos)
91- std::cout << " =" ;
92- else if (i == pos)
93- std::cout << " >" ;
94- else
95- std::cout << " " ;
96- }
97- std::cout << " ] " << int (progress * 100.0 / total) << " %\r " ;
98- std::cout.flush ();
81+ std::cout << " \r [" ;
82+ int pos = barWidth * progress / total;
83+ for (int i = 0 ; i < barWidth; ++i) {
84+ if (i < pos)
85+ std::cout << " =" ;
86+ else if (i == pos)
87+ std::cout << " >" ;
88+ else
89+ std::cout << " " ;
90+ }
91+ std::cout << " ] " << int (progress * 100.0 / total) << " %\r " ;
92+ std::cout.flush ();
9993}
0 commit comments