lipsum-cpp 0.5.4
A basic library written in C++ for generating placeholder Lorem Ipsum text.
Loading...
Searching...
No Matches
lipsum::Generator Class Reference

Main class of lipsum-cpp. More...

#include <generator.hpp>

Public Member Functions

 Generator ()
 Default constructor for Generator.
 Generator (int seed)
 Constructor for Generator.
 Generator (const std::string &path, int seed)
 Constructor for Generator.
 ~Generator ()
 Generator (const std::string &path)
 Constructor for Generator.
void load_source (const std::string &path)
 Reload a source.
void load_seed (int seed)
 Reload a seed.
void change_setting (const std::string &setting, const ArgVec2 &value)
 Change a setting.
void change_setting (const std::string &setting, int minValue, int maxValue)
 Change a setting.
void toggle_lazy ()
 Toggle the lazy flag.
std::string word (int num=1)
 Generate words.
std::string fragment ()
 Generate a sentence fragment.
std::string sentence (int num=1, bool useLipsum=true)
 Generate sentences.
std::string paragraph (int num=1, bool useLipsum=true)
 Generate paragraphs.
std::string text (bool useLipsum=true)
 Generate a random number of random paragraphs.
std::string scramble (int length=16, char minChar=' ', char maxChar='~')
 Generate a random character scramble.
std::string url ()
 Generate a URL.
std::string plain_url ()
 Generate a plain URL.
std::string email ()
 Generate an email.
std::string slug (char separator='-')
 Generate a slug.
std::string case_slug (CaseSlugCase case_=CaseSlugCase::CamelCase)
 Generate a case slug.
std::string code (CodeLanguage lang=CodeLanguage::Cpp)
 Generate a code block.
std::string ip_addr (bool useIpv6=false, bool usePort=false)
 Generate an IP address.
std::string phone_number ()
 Generate a phone number.
std::string fmt_paragraph (int num=1, bool useLipsum=true, bool useHtml=false)
 Generate Markdown or HTML paragraphs.
std::string fmt_text (int numElements=15, bool useHtml=false)
 Generate a Markdown or HTML document.
std::string fmt_header (int level=1, bool useHtml=false)
 Generate a Markdown or HTML header.
std::string fmt_emphasis (bool isBold=true, bool useHtml=false)
 Generate an emphasized Markdown or HTML sentence.
std::string fmt_link (bool useHtml=false)
 Generate a Markdown or HTML link.
std::string fmt_list (bool ordered=false, bool useHtml=false)
 Generate a Markdown or HTML list.
std::string xml (int choices=30)
 Generate an XML document.
std::string json (int maxDepth=3, bool isObject=true)
 Generate a JSON object or array.
std::string json_value (int maxDepth=3)
 Generate a JSON value.
std::string csv ()
 Generate a CSV document.

Private Member Functions

std::string single_sentence (const ArgVec2 &wordArg, const ArgVec2 &frag)
 Generate a single sentence.
std::string single_paragraph (bool useLipsum)
 Generate a single paragraph.
std::string single_fmt_paragraph (bool useLipsum, bool useHtml)
 Generate a single Markdown or HTML paragraph.
std::string json_string ()
 Generate a random JSON string.
std::string json_number ()
 Generate a random JSON number.
template<internal::UniformDistributionType T>
random_number (T min, T max)
 Generate a random number.
char random_number (char min, char max)
 Overload of random_number() for char.
bool random_number (bool min, bool max)
 Overload of random_number() for bool.
int weighted_random_idx (const std::vector< int > &weights)
 Choose a random index based off weights.
std::string tld ()
 Pick a random TLD.

Private Attributes

GeneratorSettings m_Settings
 Settings for generation.
std::mt19937 m_Gen
 Random number generator.
Source m_Source
 Source used for generation.

Detailed Description

Main class of lipsum-cpp.

Since
0.2.0

This is the main class for generation using lipsum-cpp.

Examples
Advanced.cpp, CompressionBenchmarks.cpp, Generator.cpp, GeneratorFormats.cpp, Markdown.cpp, Misc.cpp, and ParserStressTest.cpp.

Constructor & Destructor Documentation

◆ Generator() [1/4]

lipsum::Generator::Generator ( )

Default constructor for Generator.

Since
0.2.0

Load the default lorem ipsum source and choose a random seed.

◆ Generator() [2/4]

lipsum::Generator::Generator ( int seed)

Constructor for Generator.

Since
0.5.0

Load the default lorem ipsum source and the specified seed.

Parameters
seedThe seed specified.

◆ Generator() [3/4]

lipsum::Generator::Generator ( const std::string & path,
int seed )

Constructor for Generator.

Since
0.5.0

Load the specified lorem ipsum source and the specified seed.

Parameters
pathThe filepath or name of the lorem ipsum source.
seedThe seed specified.
See also
lipsum::Source::load

◆ ~Generator()

lipsum::Generator::~Generator ( )

Destructor for Generator

Since
0.2.0

Print a message if verbose mode is enabled.

◆ Generator() [4/4]

lipsum::Generator::Generator ( const std::string & path)

Constructor for Generator.

Since
0.4.1

Load the specified lorem ipsum source into m_Source. If path is invalid, it will print a warning and default to the standard lorem-ipsum source.

Parameters
pathThe filepath or name of the lorem ipsum source.
See also
lipsum::Source::load

Member Function Documentation

◆ case_slug()

std::string lipsum::Generator::case_slug ( CaseSlugCase case_ = CaseSlugCase::CamelCase)

Generate a case slug.

Since
0.5.1

Generate a case slug in the specified case, e.g. camel case, snake case, etc.

Parameters
case_The case. By default camel case.
Returns
std::string The case slug.
Examples
GeneratorFormats.cpp.

◆ change_setting() [1/2]

void lipsum::Generator::change_setting ( const std::string & setting,
const ArgVec2 & value )

Change a setting.

Since
0.4.4

Change a setting for generation. Valid setting names include "word", "frag", "sent", "para", "point", "wordFmt", "fragFmt", "wordURL", "level", and "jsonLength". If an invalid setting name is used, raise a warning and ignore.

Parameters
settingThe name of the setting.
valueThe new value for the setting.
Examples
Advanced.cpp, GeneratorFormats.cpp, Markdown.cpp, and ParserStressTest.cpp.

◆ change_setting() [2/2]

void lipsum::Generator::change_setting ( const std::string & setting,
int minValue,
int maxValue )

Change a setting.

Since
0.4.4

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Parameters
settingThe name of the setting.
minValueThe new minimum value of the setting.
maxValueThe new maximum value of the setting.

◆ code()

std::string lipsum::Generator::code ( CodeLanguage lang = CodeLanguage::Cpp)

Generate a code block.

Since
0.5.1

Generate a pseudo main function in the specified programming language.

Parameters
langThe language chosen. By default C++.
Returns
std::string The code block.
Examples
GeneratorFormats.cpp.

◆ csv()

std::string lipsum::Generator::csv ( )

Generate a CSV document.

Since
0.5.2

Generate a CSV document. It does this by choosing a random number of rows and columns, and selecting the types of each column out of the following: integers, floating point values, sentences, words, emails, and scrambles. It sets up the top row, and then for every row, fill each column.

Returns
std::string The CSV document.
Examples
GeneratorFormats.cpp.

◆ email()

std::string lipsum::Generator::email ( )

Generate an email.

Since
0.5.1

Generate two words separated by a period, with an at-sign and a plain url.

Returns
std::string The email.
Examples
GeneratorFormats.cpp.

◆ fmt_emphasis()

std::string lipsum::Generator::fmt_emphasis ( bool isBold = true,
bool useHtml = false )

Generate an emphasized Markdown or HTML sentence.

Since
0.4.6

Generate a bold or italic sentence in Markdown or HTML format that is either bold or italic. If isBold is true, make the sentence bold; else italic. If useHtml is true, use HTML bold or italic tags. If useHtml is false, use Markdown bold or italic specifiers. Formerly known as md_emphasis(). If doing a minimum build, return an empty string.

Parameters
isBoldWhether the sentence is bold or italic. By default true. (bold)
useHtmlWhether HTML should be outputted instead of Markdown. By default false.
Returns
std::string The random emphasized Markdown sentence.
Examples
GeneratorFormats.cpp.

◆ fmt_header()

std::string lipsum::Generator::fmt_header ( int level = 1,
bool useHtml = false )

Generate a Markdown or HTML header.

Since
0.4.6

Generate a header in Markdown or HTML format. If level is less than 1 or greater than 6, print a warning to the console. If useHtml is true, start an HTML heading tag at the specified level. If useHtml is false, start a Markdown heading at the specified level. Append a sentence fragment with the first letter capitalized, close the HTML tag and append two newlines. Formerly known as md_header(). If doing a minimum build, return an empty string.

Parameters
levelThe level of the heading. By default 1.
useHtmlWhether HTML should be outputted instead of Markdown. By default false.
Returns
std::string The random Markdown header.
Examples
Advanced.cpp, and GeneratorFormats.cpp.

◆ fmt_link()

std::string lipsum::Generator::fmt_link ( bool useHtml = false)

Generate a Markdown or HTML link.

Since
0.4.6

Generate a link in Markdown or HTML format. Generate a random sentence and random URL; if useHtml is false, return an opening square bracket, the sentence, a closing square bracket and opening parenthesis, the URL, and a closing parenthesis to form a Markdown URL. If useHtml is true, return an HTML <a> tag linking to the url with the sentence. Formerly known as md_link(). If doing a minimum build, return an empty string.

Parameters
useHtmlWhether HTML should be outputted instead of Markdown. By default false.
Returns
std::string The random Markdown link.
See also
lipsum::Generator::url()
Examples
Advanced.cpp, and GeneratorFormats.cpp.

◆ fmt_list()

std::string lipsum::Generator::fmt_list ( bool ordered = false,
bool useHtml = false )

Generate a Markdown or HTML list.

Since
0.4.6

Generate an ordered or unordered list in Markdown or HTML format. It does this by picking a random number of points, and if useHtml is true, start an HTML list tag. Repeat the number of points: if useHtml is false and ordered is true, append the current index and a period; if useHtml is false and ordered is false, append a hyphen and a space. If useHtml is true, start an <li> HTML tag. Append a random sentence, and append a newline or close the HTML tag. End the loop, and close the HTML list tag. Formerly known as md_list(). If doing a minimum build, return an empty string.

Parameters
orderedWhether the list is ordered. By default false.
useHtmlWhether HTML should be outputted instead of Markdown. By default false.
Returns
std::string The random Markdown list.
Examples
Advanced.cpp, and GeneratorFormats.cpp.

◆ fmt_paragraph()

std::string lipsum::Generator::fmt_paragraph ( int num = 1,
bool useLipsum = true,
bool useHtml = false )

Generate Markdown or HTML paragraphs.

Since
0.3.0

Generate multiple paragraphs in Markdown or HTML format. Formerly known as md_paragraph(). If num is negative, print a warning to the console. If doing a minimum build, return an empty string.

Parameters
numThe number of paragraphs. By default 1.
useLipsumWhether "Lorem ipsum..." should start the paragraph. By default true.
useHtmlWhether HTML should be outputted instead of Markdown. By default false.
Returns
std::string The random Markdown paragraphs.
Examples
Advanced.cpp, and GeneratorFormats.cpp.

◆ fmt_text()

std::string lipsum::Generator::fmt_text ( int numElements = 15,
bool useHtml = false )

Generate a Markdown or HTML document.

Since
0.3.0

Generate a document in Markdown or HTML format. Formerly known as md_text(). If numElements is negative, print a warning to the console. If numElements is greater than or equal to 1, add a heading. If numElements is greater than or equal to 2, add a formatted paragraph. For every numElements beyond 2, pick a random number from 0 to 2. If it is equal to 1 and the last roll resulted in 1, or there is only 3 total elements, set it to 0. This is meant to stop headings after each other and headings at the end of a document. Choose if a list will be ordered, and if the random number is equal to 0, add a formatted paragraph. If the random number is equal to 1, add a heading. If it is equal to 2, add a list. If doing a minimum build, return an empty string.

Parameters
numElementsThe number of elements (paragraph, list, heading) in the document. By default 15.
useHtmlWhether HTML should be outputted instead of Markdown. By default false.
Returns
std::string The random Markdown document.
Examples
CompressionBenchmarks.cpp, GeneratorFormats.cpp, Markdown.cpp, and Misc.cpp.

◆ fragment()

std::string lipsum::Generator::fragment ( )

Generate a sentence fragment.

Since
0.2.0

Generate a sentence fragment by choosing a random number of words and calling word(). Formerly known as sentence_fragment().

Returns
std::string The random sentence fragment.
Examples
Generator.cpp.

◆ ip_addr()

std::string lipsum::Generator::ip_addr ( bool useIpv6 = false,
bool usePort = false )

Generate an IP address.

Since
0.5.3

Generate an IPv4 or IPv6 IP address, optionally with a port. Start by choosing a port. If useIpv6 is false, generate 4 numbers from 0-255 and fill them into the format x.x.x.x, or x.x.x.x:port. If useIpv6 is true, generate 8 hexadecimal numbers from 0-65536 and fill them into the format x:x:x:x:x:x:x:x, or

Parameters
useIpv6Whether to use IPv6. By default false (IPv4).
usePortWhether to add a port. By default false.
Returns
std::string The IP address.
Examples
GeneratorFormats.cpp.

◆ json()

std::string lipsum::Generator::json ( int maxDepth = 3,
bool isObject = true )

Generate a JSON object or array.

Since
0.4.4

Generate an object or array in JSON format. If doing a minimum build, return an empty string.

Parameters
maxDepthThe maximum level of recursion. By default 3.
isObjectWhether to output an object (true) or an array (false). By default true.
Returns
std::string The random JSON object or array.
Examples
GeneratorFormats.cpp, and ParserStressTest.cpp.

◆ json_number()

std::string lipsum::Generator::json_number ( )
private

Generate a random JSON number.

Since
0.5.0

Generate a random number from -1000 to 1000, converted to a string. This function is private. If doing a minimum build, return an empty string.

Returns
std::string The random JSON number.

◆ json_string()

std::string lipsum::Generator::json_string ( )
private

Generate a random JSON string.

Since
0.5.0

Generate a random word, surrounded by quotes. This function is private. If doing a minimum build, return an empty string.

Returns
std::string The random JSON string.

◆ json_value()

std::string lipsum::Generator::json_value ( int maxDepth = 3)

Generate a JSON value.

Since
0.5.0

Generate an object, array, number, string, boolean, or null in JSON format. If maxDepth is greater than or equal to 50, print a warning. If doing a minimum build, return an empty string.

Parameters
maxDepthThe maximum level of recursion. By default 3.
Returns
std::string The random JSON value.
Examples
GeneratorFormats.cpp.

◆ load_seed()

void lipsum::Generator::load_seed ( int seed)

Reload a seed.

Since
0.5.0

Reload the seed of m_Gen.

Parameters
seedThe seed specified.

◆ load_source()

void lipsum::Generator::load_source ( const std::string & path)

Reload a source.

Since
0.4.5

Reload the specified lorem ipsum source into m_Source. If path is invalid, it will print a warning and default to the standard lorem-ipsum source.

Parameters
pathThe filepath or name of the lorem ipsum source.
See also
lipsum::Source::load

◆ paragraph()

std::string lipsum::Generator::paragraph ( int num = 1,
bool useLipsum = true )

Generate paragraphs.

Since
0.2.0

Generate multiple paragraphs. If num is negative, print a warning to the console.

Parameters
numThe number of paragraphs. By default 1.
useLipsumWhether "Lorem ipsum..." should start the paragraph(s). By default true.
Returns
std::string The random paragraph(s).
See also
lipsum::Generator::single_paragraph(bool)
Examples
Advanced.cpp, Generator.cpp, and Misc.cpp.

◆ phone_number()

std::string lipsum::Generator::phone_number ( )

Generate a phone number.

Since
0.5.3

Generate a phone number in the format "(+1) xxx-xxx-xxxx".

Returns
std::string The phone number.
Examples
GeneratorFormats.cpp.

◆ plain_url()

std::string lipsum::Generator::plain_url ( )

Generate a plain URL.

Since
0.4.5

Generate a URL with "lpsmcpp-" and a random word followed by a random TLD.

Returns
std::string The plain URL.
Examples
GeneratorFormats.cpp.

◆ random_number() [1/3]

bool lipsum::Generator::random_number ( bool min,
bool max )
private

Overload of random_number() for bool.

Since
0.5.0

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Parameters
minThe minimum value.
maxThe maximum value.
Returns
bool The random character.

◆ random_number() [2/3]

char lipsum::Generator::random_number ( char min,
char max )
private

Overload of random_number() for char.

Since
0.5.0

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Parameters
minThe minimum value.
maxThe maximum value.
Returns
char The random character.

◆ random_number() [3/3]

template<internal::UniformDistributionType T>
T lipsum::Generator::random_number ( T min,
T max )
inlineprivate

Generate a random number.

Since
0.5.0

Generate a random number between min and max, inclusive. If T is an integer, use std::uniform_int_distribution. Else, use std::uniform_real_distribution. This function is private.

Template Parameters
TThe type of the random number. Must be a uniform-distribution type, i.e. ints (excluding chars and bool) and floats.
Parameters
minThe minimum value.
maxThe maximum value.
Returns
T The random number.

◆ scramble()

std::string lipsum::Generator::scramble ( int length = 16,
char minChar = ' ',
char maxChar = '~' )

Generate a random character scramble.

Since
0.4.5

Generate a random string of text with random characters. If length is negative, print a warning to the console. Add length number of random characters from minChar to maxChar to the string.

Parameters
lengthThe number of characters. By default 16.
minCharThe lowest-valued ASCII character that can appear. By default a space.
maxCharThe highest-valued ASCII character than can appear. By default a tilde.
Returns
std::string The character scramble.
Examples
GeneratorFormats.cpp.

◆ sentence()

std::string lipsum::Generator::sentence ( int num = 1,
bool useLipsum = true )

Generate sentences.

Since
0.2.0

Generate multiple sentences separated by spaces. If num is negative, print a warning to the console. If useLipsum is true, make the first sentence the default lorem-ipsum sentence.

Parameters
numThe number of sentences. By default 1.
useLipsumWhether "Lorem ipsum..." should start the sentence(s). By default true.
Returns
std::string The random sentence(s).
See also
lipsum::Generator::single_sentence(const lpsm::ArgVec2&, const lpsm::ArgVec2&)
Examples
Advanced.cpp, and Generator.cpp.

◆ single_fmt_paragraph()

std::string lipsum::Generator::single_fmt_paragraph ( bool useLipsum,
bool useHtml )
private

Generate a single Markdown or HTML paragraph.

Since
0.5.0

Generate a single paragraph in Markdown or HTML format. This function is private. Formerly known as single_md_paragraph(). It works by first choosing a random number of sentences, and if useHtml is true, start an HTML paragraph tag. It then loops that number of times, choosing an internal value fmtRoll with a 1 in 15 chance of being 1. If fmtRoll is 1, choose whether to add a link and whether an emphasized sentence will be bold or italic. If useLipsum is true and it is currently on the first line, add the default lorem-ipsum sentence. Else, if fmtRoll is 1 and a link is being added, append a link. Else, if fmtRoll is 1 and a link is not being added, append an emphasized sentence. Else, append a normal sentence. Then, append a space. If useHtml is true, close the HTML paragraph tag. Finally, end with two new line characters. If doing a minimum build, return an empty string.

Parameters
useLipsumWhether "Lorem ipsum..." should start the paragraph.
useHtmlWhether HTML should be outputted instead of Markdown.
Returns
std::string The random Markdown paragraph.

◆ single_paragraph()

std::string lipsum::Generator::single_paragraph ( bool useLipsum)
private

Generate a single paragraph.

Since
0.5.0

Generate a single paragraph. This function is private. It works by starting the paragraph with a single tab character and picking a random number of sentences. It loops that number of times, and if useLipsum is true and it is the first line, add the default lorem-ipsum sentence. Else, add a random sentence. It then will add a space. Once the loop is done, if the string is not empty, remove the last trailing space, and finally end with a newline.

Parameters
useLipsumWhether "Lorem ipsum..." should start the paragraph.
Returns
std::string The paragraph.

◆ single_sentence()

std::string lipsum::Generator::single_sentence ( const ArgVec2 & wordArg,
const ArgVec2 & frag )
private

Generate a single sentence.

Since
0.5.0

Generate a single sentence. This function is private. It works by picking a random number of sentence fragments and joining them with random punctuation marks (weighted towards commas, but semicolons, colons, and dashes can appear). It ends the sentence with a period, exclamation mark, or comma. It then capitalizes the first letter of the sentence. If in minimum build mode, only use commas and periods.

Parameters
wordArgThe minimum and maximum possible number of words in a sentence fragment.
fragThe minimum and maximum possible number of sentence fragments in the sentence.
Returns
std::string The sentence.

◆ slug()

std::string lipsum::Generator::slug ( char separator = '-')

Generate a slug.

Since
0.4.5

Generate a sentence fragment with the spaces replaced with the specified separator character.

Parameters
separatorThe separator character. By default a hyphen.
Returns
std::string The slug.
Examples
GeneratorFormats.cpp.

◆ text()

std::string lipsum::Generator::text ( bool useLipsum = true)

Generate a random number of random paragraphs.

Since
0.4.4

Generate a random number of random paragraphs by choosing a random number of paragraphs and calling paragraph().

Parameters
useLipsumWhether "Lorem ipsum..." should start the paragraph(s). By default true.
Returns
std::string The random text.
Examples
Generator.cpp.

◆ tld()

std::string lipsum::Generator::tld ( )
private

Pick a random TLD.

Since
0.5.0

This function picks a random TLD out of .com, .org, .net, .edu, .io, .ca, and .co.uk, each with chances of 70%, 10%, 7%, 5%, 5%, 2%, and 1% respectively. This function is private. If doing a minimum build, choose .com.

Returns
std::string The random TLD.

◆ toggle_lazy()

void lipsum::Generator::toggle_lazy ( )

Toggle the lazy flag.

Since
0.5.3

Toggle the lazy flag. If "lazy" is false, set it to true; else, set it to false.

◆ url()

std::string lipsum::Generator::url ( )

Generate a URL.

Since
0.4.5

Generate a URL starting in https://, followed by a plain url, followed by a slash and a random HTML id as a slug separated by hyphens.

Returns
std::string The URL.
See also
lipsum::Generator::plain_url()
Examples
GeneratorFormats.cpp.

◆ weighted_random_idx()

int lipsum::Generator::weighted_random_idx ( const std::vector< int > & weights)
private

Choose a random index based off weights.

Since
0.5.0

This function chooses a random index of weights, with higher valued weights being more likely. To do this, it passes the weights into an std::discrete_distribution.

Parameters
weightsThe weights.
Returns
int The random index.

◆ word()

std::string lipsum::Generator::word ( int num = 1)

Generate words.

Since
0.2.0

Generate the specified number of words, separated by spaces. If num is negative, print a warning to the console.

Parameters
numThe number of words. By default 1.
Returns
std::string The random word.
Examples
Advanced.cpp, and Generator.cpp.

◆ xml()

std::string lipsum::Generator::xml ( int choices = 30)

Generate an XML document.

Since
0.4.4

Generate a document in XML format. It does this by starting the XML prolog, choosing the root element, and starting it. It then repeats choices times: it generates a random number from 0 to 2. If it is 0, start an XML tag with a random element with optionally an attribute, and also add it to the tag stack. If the number is 1, add a closed element with a random sentence inside. If the number is 2, close the previously opened tag if possible. Finally, end the loop and close all remaining tags in the tag stack, and close the root element. If doing a minimum build, return an empty string.

Parameters
choicesThe number of choices (start element, end element, add data element) that are made. By default 30.
Returns
std::string The random XML document.
Examples
GeneratorFormats.cpp, and ParserStressTest.cpp.

Member Data Documentation

◆ m_Source

Source lipsum::Generator::m_Source
private

Source used for generation.

The source passed into functions. By default the lorem-ipsum source "lorem" from sample.inl.

Since
0.4.1

The documentation for this class was generated from the following files: