Skip to content

Commit

Permalink
add generating emojis (#69)
Browse files Browse the repository at this point in the history
  • Loading branch information
cieslarmichal authored Jul 21, 2023
1 parent 32546a9 commit 2c601dc
Show file tree
Hide file tree
Showing 9 changed files with 2,618 additions and 28 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ int main()
## Requirements

### Compiler Support

- [MSVC➚](https://en.wikipedia.org/wiki/Microsoft_Visual_Studio) version 143 or newer.
- [GCC➚](https://gcc.gnu.org/) version 13 or newer.
- [Clang➚](https://clang.llvm.org/) version 16 or newer.
Expand All @@ -79,6 +80,7 @@ target_link_libraries(main faker-cxx)
- 🌐 Internet - Generate emails, usernames, passwords, images urls.
- 🌍 Location - Generate countries, cities, zip codes, street addresses.
- 🧑 Person - Generate first, last names, job titles, genders, sex.
- 📞 Phone - Generate phone number, IMEI.
- 🛒 Commerce - Generate commerce department, product name, sku, price.
- 📅 Date - Generate past, future dates.
- 🏦 Finance - Generate currency, IBAN, BIC, account name, account number, pin, credit card numbers.
Expand All @@ -105,6 +107,7 @@ Please check [CONTRIBUTING](https://github.com/cieslarmichal/faker-cxx/blob/main
## Building from sources with Clang 16

#### 1. Install Clang 16

```
sudo add-apt-repository ppa:trebelnik-stefina/launchpad-getkeys \
&& sudo apt-get update \
Expand All @@ -115,7 +118,9 @@ sudo add-apt-repository ppa:trebelnik-stefina/launchpad-getkeys \
&& sudo apt-get install -y lld-16 ninja-build build-essential libstdc++-13-dev \
clang-16 clang-tools-16 llvm-16 lcov
```

#### 2. Prepare build directory

```
git clone https://github.com/cieslarmichal/faker-cxx.git
cd faker-cxx
Expand All @@ -125,11 +130,13 @@ cd build
```

#### 3. CMake setup with Clang 16

```
cmake .. -DCMAKE_CXX_COMPILER=/usr/bin/clang++-16
```

#### 4. Build 🔨

```
make
```
3 changes: 3 additions & 0 deletions TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@
- [ ] port
- [x] protocol
- [ ] url
- [ ] domainName
- [ ] domainSuffix
- [ ] domainWord
- [x] image url
- [x] github avatar url

Expand Down
42 changes: 24 additions & 18 deletions include/faker-cxx/Internet.h
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
#pragma once

#include <array>
#include <optional>
#include <string>

#include "types/EmojiType.h"
#include "types/Ipv4Address.h"
#include "types/Ipv4Class.h"

namespace faker
{
enum class HttpResponseType
Expand Down Expand Up @@ -113,6 +116,20 @@ class Internet
*/
static std::string githubAvatarUrl();

/**
* @brief Returns a random emoji.
*
* @param type The optional type of them emoji to be generated.
*
* @returns Emoji.
*
* @code
* Internet::emoji() // "👑"
* Internet::emoji(EmojiType::Food) // "🍕"
* @endcode
*/
static std::string emoji(std::optional<EmojiType> type = std::nullopt);

/**
* @brief Returns a random web protocol. Either `http` or `https`.
*
Expand Down Expand Up @@ -149,24 +166,12 @@ class Internet
*/
static unsigned httpStatusCode(std::optional<HttpResponseType> responseType = std::nullopt);

/**
* @brief Determines which address class user wants to generate in the ipv4 method.
*
*/
enum class IPv4Class
{
A,
B,
C
};

using IPv4Type = std::array<unsigned int, 4>;

/**
* @brief Returns a string containing randomized ipv4 address of the given class.
*
* @param ipv4class address class to be generated
* @return std::string representation of the ipv4 address
* @param ipv4class Address class to be generated.
*
* @return String representation of the ipv4 address.
*
* @code
* Internet::ipv4() // "192.168.0.1"
Expand All @@ -184,13 +189,14 @@ class Internet
*
* @param generationMask Mask deciding which bits of the base address should be kept during randomization.
* @param baseIpv4Address Address to randomize from.
* @return std::string representation of the ipv4 address
*
* @return std::string representation of the ipv4 address.
*
* @code
* Internet::ipv4({255.0.0.0}, {10.100.100.100}) // "10.128.17.1"
* Internet::ipv4({255.255.128.0}, {129.168.255.0}) // "192.168.128.10"
* @endcode
*/
static std::string ipv4(const IPv4Type& baseIpv4Address, const IPv4Type& generationMask);
static std::string ipv4(const IPv4Address& baseIpv4Address, const IPv4Address& generationMask);
};
}
18 changes: 18 additions & 0 deletions include/faker-cxx/types/EmojiType.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#pragma once

namespace faker
{
enum class EmojiType
{
Smiley,
Body,
Person,
Nature,
Food,
Travel,
Activity,
Object,
Symbol,
Flag
};
}
8 changes: 8 additions & 0 deletions include/faker-cxx/types/Ipv4Address.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#pragma once

#include <array>

namespace faker
{
using IPv4Address = std::array<unsigned int, 4>;
}
11 changes: 11 additions & 0 deletions include/faker-cxx/types/Ipv4Class.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#pragma once

namespace faker
{
enum class IPv4Class
{
A,
B,
C
};
}
45 changes: 42 additions & 3 deletions src/modules/internet/Internet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <utility>

#include "data/EmailHosts.h"
#include "data/Emojis.h"
#include "faker-cxx/Helper.h"
#include "faker-cxx/Person.h"

Expand Down Expand Up @@ -36,7 +37,14 @@ constexpr unsigned int ipv4ClassBFirstSector = 172;
constexpr unsigned int ipv4ClassBSecondSectorLowerBound = 16u;
constexpr unsigned int ipv4ClassBSecondSectorUpperBound = 31u;
constexpr unsigned int ipv4SectorUpperBound = 255u;
const std::map<EmojiType, std::vector<std::string>> emojiTypeToEmojisMapping{
{EmojiType::Smiley, smileyEmojis}, {EmojiType::Body, bodyEmojis}, {EmojiType::Person, personEmojis},
{EmojiType::Nature, natureEmojis}, {EmojiType::Food, foodEmojis}, {EmojiType::Travel, travelEmojis},
{EmojiType::Activity, activityEmojis}, {EmojiType::Object, objectEmojis}, {EmojiType::Symbol, symbolEmojis},
{EmojiType::Flag, flagEmojis},
};
}

std::string Internet::username(std::optional<std::string> firstNameInit, std::optional<std::string> lastNameInit)
{
const auto firstName = firstNameInit ? *firstNameInit : Person::firstName();
Expand Down Expand Up @@ -100,6 +108,31 @@ std::string Internet::githubAvatarUrl()
return std::format("https://avatars.githubusercontent.com/u/{}", Number::integer<int>(100000000));
}

std::string Internet::emoji(std::optional<EmojiType> type)
{
if (type)
{
const auto& emojis = emojiTypeToEmojisMapping.at(*type);

return Helper::arrayElement<std::string>(emojis);
}

std::vector<std::string> emojis;

emojis.insert(emojis.end(), smileyEmojis.begin(), smileyEmojis.end());
emojis.insert(emojis.end(), bodyEmojis.begin(), bodyEmojis.end());
emojis.insert(emojis.end(), personEmojis.begin(), personEmojis.end());
emojis.insert(emojis.end(), natureEmojis.begin(), natureEmojis.end());
emojis.insert(emojis.end(), foodEmojis.begin(), foodEmojis.end());
emojis.insert(emojis.end(), travelEmojis.begin(), travelEmojis.end());
emojis.insert(emojis.end(), activityEmojis.begin(), activityEmojis.end());
emojis.insert(emojis.end(), objectEmojis.begin(), objectEmojis.end());
emojis.insert(emojis.end(), symbolEmojis.begin(), symbolEmojis.end());
emojis.insert(emojis.end(), flagEmojis.begin(), flagEmojis.end());

return Helper::arrayElement<std::string>(emojis);
}

std::string Internet::protocol()
{
return Helper::arrayElement<std::string>(webProtocols);
Expand Down Expand Up @@ -129,11 +162,14 @@ unsigned Internet::httpStatusCode(std::optional<HttpResponseType> responseType)

return Helper::arrayElement<unsigned>(statusCodes);
}

std::string Internet::ipv4(IPv4Class ipv4class)
{
IPv4Type sectors;
IPv4Address sectors;

sectors[3] = Number::integer<unsigned int>(ipv4SectorUpperBound);
sectors[2] = Number::integer<unsigned int>(ipv4SectorUpperBound);

switch (ipv4class)
{
case IPv4Class::A:
Expand All @@ -154,17 +190,20 @@ std::string Internet::ipv4(IPv4Class ipv4class)
sectors[0] = ipv4ClassCFirstSector;
}
}

return std::format("{}.{}.{}.{}", sectors[0], sectors[1], sectors[2], sectors[3]);
}

std::string Internet::ipv4(const IPv4Type& baseIpv4Address, const IPv4Type& generationMask)
std::string Internet::ipv4(const IPv4Address& baseIpv4Address, const IPv4Address& generationMask)
{
IPv4Type sectors;
IPv4Address sectors;

for (std::size_t i = 0; i < ipv4AddressSectors; i++)
{
sectors[i] = (~generationMask[i]) & Number::integer<unsigned int>(ipv4SectorUpperBound);
sectors[i] |= (baseIpv4Address[i] & generationMask[i]);
}

return std::format("{}.{}.{}.{}", sectors[0], sectors[1], sectors[2], sectors[3]);
}

Expand Down
Loading

0 comments on commit 2c601dc

Please sign in to comment.