lipsum-cpp 0.5.4
A basic library written in C++ for generating placeholder Lorem Ipsum text.
Loading...
Searching...
No Matches
misc.inl
Go to the documentation of this file.
1
14#pragma once
15
16#include "core/internal.hpp"
17
18static int
19LipsumMiscInlFind(int count, const std::string& str, const std::string& check)
20{
21 size_t pos = 0;
22 while ((pos = str.find(check, pos)) != std::string::npos)
23 {
24 ++count;
25 pos += check.size();
26 }
27 return count;
28}
29
30namespace lipsum
31{
32
33 /*
34 * MISC
35 * ----------------
36 */
37
39 {
40 return {"Lorem ipsum dolor sit amet, consectetur adipiscing elit."};
41 }
42
43 int CountWords(const std::string& str)
44 {
45 int count = 0;
46 int urlNum = 0;
47 bool inWord = false;
48 char lastLetter = '\0';
49 for (const char& letter : str)
50 {
51 if (letter == '(')
52 {
53 ++urlNum;
54 }
55 if (letter == ')')
56 {
57 --urlNum;
58 }
59
60 bool isWordChar =
61 static_cast<bool>(
62 std::isalnum(static_cast<unsigned char>(letter))) ||
63 (letter == '-' && lastLetter != '\n' &&
64 lastLetter != ' ') ||
65 (letter == '+' && lastLetter != '\n' &&
66 lastLetter != ' ') ||
67 (letter == '\'' && lastLetter != '\n' && lastLetter != ' ');
68 // if not in parens
69 if (urlNum <= 0)
70 {
71 if (isWordChar && !inWord)
72 {
73 inWord = true;
74 ++count;
75 }
76 if (!isWordChar)
77 {
78 inWord = false;
79 }
80 }
81
82 lastLetter = letter;
83 }
84 LPSM_VERBOSE_LOG(Trace, "Counting words, got ", count);
85 return count;
86 }
87
88 int CountSentenceFragments(const std::string& str)
89 {
90 if (str.empty())
91 {
92 return 0;
93 }
94 int res = 0;
95 int urlNum = 0;
96 char lastLetter = '\0';
97 for (const char& letter : str)
98 {
99 if (letter == '(')
100 {
101 ++urlNum;
102 }
103 if (letter == ')')
104 {
105 --urlNum;
106 }
107
108 // if not in parens
109 if (urlNum <= 0)
110 {
111 if (letter == ',' || letter == ';' || letter == ':' ||
112 letter == '.' || letter == '?' || letter == '!')
113 {
114 ++res;
115 }
116 if (letter == '-' && lastLetter != '\n')
117 {
118 ++res;
119 }
120 }
121 lastLetter = letter;
122 }
123 LPSM_VERBOSE_LOG(Trace, "Counting sentence fragments, got ", res);
124 return res;
125 }
126
127 int CountSentences(const std::string& str)
128 {
129 int res = 0;
130 int urlNum = 0;
131 for (const char& letter : str)
132 {
133 if (letter == '(')
134 {
135 ++urlNum;
136 }
137 if (letter == ')')
138 {
139 --urlNum;
140 }
141 if ((letter == '.' || letter == '?' || letter == '!') &&
142 urlNum <= 0)
143 {
144 ++res;
145 }
146 }
147 LPSM_VERBOSE_LOG(Trace, "Counting sentences, got ", res);
148 return res;
149 }
150
151 int CountParagraphs(const std::string& str, Format format)
152 {
153 switch (format)
154 {
155 case Format::Plain:
156 {
157 return static_cast<int>(
158 std::count(str.begin(), str.end(), '\t'));
159 }
160 case Format::Markdown:
161 {
162 int count = LipsumMiscInlFind(0, str, "\n\n");
163 return count;
164 }
165 case Format::HTML:
166 {
167 int count = LipsumMiscInlFind(0, str, "<p>");
168 count = LipsumMiscInlFind(count, str, "<ol>");
169 count = LipsumMiscInlFind(count, str, "<ul>");
170 count = LipsumMiscInlFind(count, str, "<h1>");
171 count = LipsumMiscInlFind(count, str, "<h2>");
172 count = LipsumMiscInlFind(count, str, "<h3>");
173 count = LipsumMiscInlFind(count, str, "<h4>");
174 count = LipsumMiscInlFind(count, str, "<h5>");
175 count = LipsumMiscInlFind(count, str, "<h6>");
176 return count;
177 }
178 default:
179 {
180 internal::LogWarn(internal::LogType::Warn,
181 "lpsm::CountParagraphs(): invalid format for "
182 "conversion: ",
183 static_cast<int>(format));
184 return 0;
185 }
186 }
187 }
188
189 std::string
190 ConvertFormat(const std::string& str, Format format1, Format format2)
191 {
192 auto plainConvs = [&]() -> std::string
193 {
194 LPSM_VERBOSE_LOG(Trace,
195 "Format is Plain. Attempting conversion...");
196 std::string ret;
197 ret.reserve(str.size());
198 switch (format2)
199 {
200 case Format::Plain:
201 {
202 LPSM_VERBOSE_LOG(Trace, "Using Plain->Plain.");
203 return str;
204 }
205 case Format::Markdown:
206 {
207 for (const auto& letter : str)
208 {
209 if (letter == '\t')
210 {
211 ret += "\n\n";
212 }
213 else
214 {
215 ret += internal::HandleHTMLEntity(letter,
216 Format::Markdown);
217 }
218 }
219 LPSM_VERBOSE_LOG(Trace, "Using Plain->Markdown.");
220 return ret;
221 }
222 case Format::XML:
223 {
224 LPSM_VERBOSE_LOG(Trace, "Using Plain->XML.");
225 ret += R"(<?xml version="1.0" encoding="UTF-8"?>)";
226 [[fallthrough]];
227 }
228 case Format::HTML:
229 {
230 LPSM_VERBOSE_LOG(Trace, "Using Plain->HTML.");
231 for (const auto& letter : str)
232 {
233 if (letter == '\t')
234 {
235 ret += "<p>";
236 }
237 ret += internal::HandleHTMLEntity(letter, Format::HTML);
238 if (letter == '\n')
239 {
240 ret += "</p>";
241 }
242 }
243 return ret;
244 }
245 case Format::JSON:
246 {
247 LPSM_VERBOSE_LOG(Trace, "Using Plain->JSON.");
248 ret += R"({"text": ")";
249 for (const auto& letter : str)
250 {
251 ret += internal::HandleHTMLEntity(letter, Format::JSON);
252 }
253 ret += "\"}";
254 return ret;
255 }
256 default:
257 {
258 internal::LogWarn(internal::LogType::Error,
259 "lpsm::ConvertFormat(): Format option ",
260 static_cast<int>(format2),
261 " is out of range.");
262 return "";
263 }
264 }
265 };
266
267 LPSM_VERBOSE_LOG(Info, "Converting between formats...");
268 switch (format1)
269 {
270 case Format::Plain:
271 {
272 return plainConvs();
273 }
274 default:
275 {
276 internal::LogWarn(internal::LogType::Error,
277 "lpsm::ConvertFormat(): Unknown route from "
278 "format ",
279 static_cast<int>(format1),
280 " to ",
281 static_cast<int>(format2));
282 return "";
283 }
284 }
285 }
286
287} // namespace lipsum
#define LPSM_VERBOSE_LOG(type,...)
Verbose log messages.
Definition core.hpp:49
Declaration of lipsum::internal.
void LogWarn(LogType type, const Args &... args)
Log a warning.
Definition internal.hpp:126
LIPSUM_API std::string HandleHTMLEntity(char letter, Format format=Format::HTML)
Handle HTML entities.
Definition argvec2.inl:56
This is the main namespace of lipsum-cpp.
LIPSUM_API int CountSentences(const std::string &str)
Count the number of sentences in a string.
Definition misc.inl:127
LIPSUM_API int CountSentenceFragments(const std::string &str)
Count the number of sentence fragments in a string.
Definition misc.inl:88
LIPSUM_API int CountWords(const std::string &str)
Count the number of words in a string.
Definition misc.inl:43
LIPSUM_API std::string ConvertFormat(const std::string &str, Format format1=Format::Plain, Format format2=Format::HTML)
Convert a string between two formats.
Definition misc.inl:190
Format
Types of formats.
Definition misc.hpp:33
LIPSUM_API int CountParagraphs(const std::string &str, Format format=Format::Plain)
Count the number of paragraphs or elements in a string.
Definition misc.inl:151
LIPSUM_API std::string GenerateDefaultLipsumSentence()
Generate the beginning Lorem Ipsum sentence.
Definition misc.inl:38