Skip to content

Commit

Permalink
chore: remove sstream
Browse files Browse the repository at this point in the history
  • Loading branch information
cieslarmichal committed Jun 15, 2024
1 parent 27f5c14 commit 7fd3d28
Show file tree
Hide file tree
Showing 51 changed files with 14,723 additions and 15,083 deletions.
1 change: 1 addition & 0 deletions .clang-format
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ SpaceInEmptyParentheses: false
SpacesBeforeTrailingComments: 1
SpacesInAngles: false
ReferenceAlignment: Left
SeparateDefinitionBlocks: Always
SpacesInCStyleCastParentheses: false
SpacesInContainerLiterals: false
SpacesInParentheses: false
Expand Down
3 changes: 1 addition & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@ set(FAKER_SOURCES
src/common/FormatHelper.cpp
src/common/LuhnCheck.cpp
src/common/StringHelper.cpp
src/common/PrecisionMapper.cpp
)

add_library(${CMAKE_PROJECT_NAME} ${FAKER_SOURCES})
Expand Down Expand Up @@ -144,4 +143,4 @@ endif ()

if (BUILD_EXAMPLES)
add_subdirectory(examples)
endif ()
endif ()
54 changes: 19 additions & 35 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,48 +58,32 @@ Please avoid working directly on the ``main`` branch.
Before making changes, make sure you have `clang-format` installed. If you're using Visual Studio Code, you can
install the `clang-format` extension.
When you make a commit, `pre-commit` will automatically run `clang-format` on your code, ensuring it adheres to the
project's coding style.
3**Make commits of logical units:**
3. **Set up pre-commit hooks:**
This means that each commit should contain a complete and coherent piece of work that can be understood independently
of other changes. For example, if you're fixing two different bugs, it's better to make two separate commits (one for
each bug) rather than one commit that contains fixes for both bugs. This makes it easier to understand the purpose of
each commit, and allows each change to be reviewed and applied separately.
This project uses `pre-commit` hooks with `clang-format` to ensure code style consistency. Before making changes, set
up the pre-commit hooks with the following commands:
4**Make sure you have added the necessary tests for your changes:**
```sh
pip install pre-commit
pre-commit install
```

Now, `pre-commit` will automatically run `clang-format` on your code whenever you make a commit, ensuring it adheres
to the project's coding style.
4. **Make commits of logical units:**
This means that each commit should contain a complete and coherent piece of work that can be understood independently
of other changes. For example, if you're fixing two different bugs, it's better to make two separate commits (one for
each bug) rather than one commit that contains fixes for both bugs. This makes it easier to understand the purpose of
each commit, and allows each change to be reviewed and applied separately.
5. **Make sure you have added the necessary tests for your changes:**
If you're adding a new feature or changing existing functionality, it's important to update or add tests that verify
your changes. This helps to ensure that your changes work as expected and don't introduce new bugs. It also helps
other developers understand what your code is supposed to do.
If you're adding a new feature or changing existing functionality, it's important to update or add tests that verify
your changes. This helps to ensure that your changes work as expected and don't introduce new bugs. It also helps
other developers understand what your code is supposed to do.

6. **Run all the tests to assure nothing else was accidentally broken:**
5**Run all the tests to assure nothing else was accidentally broken:**

Before you submit your changes, you should run all the project's tests to make sure your changes haven't
inadvertently broken anything. Even if you think your changes are isolated, there could be unexpected interactions
with other parts of the codebase.
Before you submit your changes, you should run all the project's tests to make sure your changes haven't
inadvertently broken anything. Even if you think your changes are isolated, there could be unexpected interactions
with other parts of the codebase.

7. **If you've added a new file to your project with non-Latin characters, ensure that the file encoding is set to
Unicode (UTF-8 without signature) - Codepage 65001 in Microsoft Visual Studio Code:**
6**If you've added a new file to your project with non-Latin characters, ensure that the file encoding is set to
Unicode (UTF-8 without signature) - Codepage 65001 in Microsoft Visual Studio Code:**
If a file contains non-Latin characters (such as characters from Chinese, Arabic, or many other non-Latin alphabets),
it's important to save the file with the correct encoding to ensure that the characters are displayed correctly. In
Visual Studio Code, you can set the encoding for a file by clicking on the "UTF-8" button in the status bar at the
bottom of the window, and then selecting "Save with Encoding" and choosing "UTF-8 without BOM".
If a file contains non-Latin characters (such as characters from Chinese, Arabic, or many other non-Latin alphabets),
it's important to save the file with the correct encoding to ensure that the characters are displayed correctly. In
Visual Studio Code, you can set the encoding for a file by clicking on the "UTF-8" button in the status bar at the
bottom of the window, and then selecting "Save with Encoding" and choosing "UTF-8 without BOM".

## Building and Testing the Project

Expand Down
1 change: 1 addition & 0 deletions include/faker-cxx/RandomGenerator.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class RandomGenerator
{
public:
RandomGenerator() : generator_{T(std::random_device{}())} {}

~RandomGenerator() = default;

RandomGenerator(const RandomGenerator&) = default;
Expand Down
31 changes: 15 additions & 16 deletions include/faker-cxx/String.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
#include <optional>
#include <random>
#include <set>
#include <sstream>
#include <string>

#include "RandomGenerator.h"
Expand Down Expand Up @@ -86,44 +85,44 @@ class String
{
static std::uniform_int_distribution<> dist(0, 15);
static std::uniform_int_distribution<> dist2(8, 11);
static std::string_view hexCharacters{"0123456789abcdef"};

std::stringstream ss;
ss << std::hex;
std::string result;
result.reserve(36);

for (int i = 0; i < 8; i++)
{
ss << gen(dist);
result.append(1, hexCharacters[static_cast<size_t>(gen(dist))]);
}
result.append(1, '-');

ss << "-";
for (int i = 0; i < 4; i++)
{
ss << gen(dist);
result.append(1, hexCharacters[static_cast<size_t>(gen(dist))]);
}
result.append(1, '-');

ss << "-4";
result.append(1, '4');
for (int i = 0; i < 3; i++)
{
ss << gen(dist);
result.append(1, hexCharacters[static_cast<size_t>(gen(dist))]);
}
result.append(1, '-');

ss << "-";

ss << gen(dist2);
result.append(1, hexCharacters[static_cast<size_t>(gen(dist2))]);

for (int i = 0; i < 3; i++)
{
ss << gen(dist);
result.append(1, hexCharacters[static_cast<size_t>(gen(dist))]);
}

ss << "-";
result.append(1, '-');

for (int i = 0; i < 12; i++)
{
ss << gen(dist);
result.append(1, hexCharacters[static_cast<size_t>(gen(dist))]);
}

return ss.str();
return result;
}

/**
Expand Down
1 change: 1 addition & 0 deletions include/faker-cxx/System.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ namespace faker
struct FileOptions
{
int extensionCount = 1;

struct
{
int min = 1;
Expand Down
31 changes: 28 additions & 3 deletions src/common/FormatHelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ FormatHelper::fillTokenValues(const std::string& format,
return filledFormat;
}

std::string
FormatHelper::fillTokenValues(const std::string& format,
std::unordered_map<std::string_view, std::function<std::string_view()>> tokenValueGenerators)
std::string FormatHelper::fillTokenValues(
const std::string& format,
std::unordered_map<std::string_view, std::function<std::string_view()>> tokenValueGenerators)
{
std::string filledFormat;

Expand Down Expand Up @@ -85,4 +85,29 @@ FormatHelper::fillTokenValues(const std::string& format,
return filledFormat;
}

std::string FormatHelper::precisionFormat(Precision precision, double value)
{
switch (precision)
{
case Precision::ZeroDp:
return FormatHelper::format("{:.0f}", value);
case Precision::OneDp:
return FormatHelper::format("{:.1f}", value);
case Precision::TwoDp:
return FormatHelper::format("{:.2f}", value);
case Precision::ThreeDp:
return FormatHelper::format("{:.3f}", value);
case Precision::FourDp:
return FormatHelper::format("{:.4f}", value);
case Precision::FiveDp:
return FormatHelper::format("{:.5f}", value);
case Precision::SixDp:
return FormatHelper::format("{:.6f}", value);
case Precision::SevenDp:
return FormatHelper::format("{:.7f}", value);
default:
throw std::invalid_argument("Invalid precision");
}
}

}
11 changes: 8 additions & 3 deletions src/common/FormatHelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@
#include <string>
#include <string_view>
#include <unordered_map>
#include <vector>

#if !defined(HAS_STD_FORMAT)
#include <fmt/format.h>
#include <fmt/chrono.h>
#include <fmt/core.h>
#else
#include <format>

#include "faker-cxx/types/Precision.h"
#endif

namespace faker
Expand All @@ -31,11 +33,14 @@ class FormatHelper
}
#endif

static std::string precisionFormat(Precision precision, double value);

static std::string
fillTokenValues(const std::string& format,
std::unordered_map<std::string, std::function<std::string()>> tokenValueGenerators);

static std::string fillTokenValues(const std::string& format,
static std::string
fillTokenValues(const std::string& format,
std::unordered_map<std::string_view, std::function<std::string_view()>> tokenValueGenerators);
};
}
1 change: 0 additions & 1 deletion src/common/LuhnCheck.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#pragma once

#include <algorithm>
#include <string>

namespace faker
Expand Down
17 changes: 0 additions & 17 deletions src/common/PrecisionMapper.cpp

This file was deleted.

17 changes: 0 additions & 17 deletions src/common/PrecisionMapper.h

This file was deleted.

56 changes: 34 additions & 22 deletions src/common/StringHelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

#include <algorithm>
#include <cctype>
#include <cstddef>
#include <sstream>
#include <string>
#include <vector>

Expand Down Expand Up @@ -32,42 +30,56 @@ std::vector<std::string> StringHelper::split(const std::string& data, const std:

std::string StringHelper::joinString(const std::vector<std::string>& data, const std::string& separator)
{
std::ostringstream result;

for (size_t i = 0; i < (data.size() - 1); ++i)
switch (data.size())
{
result << data[i] << separator;
}

if (!data.empty()) [[likely]]
case 0:
return "";
case 1:
return data[0];
default:
{
result << data[data.size() - 1];
}
std::string result{data[0]};

return result.str();
for (auto it = data.begin() + 1; it != data.end(); ++it)
{
result += separator;
result += *it;
}

return result;
}
}
}

std::string StringHelper::join(const std::vector<std::string_view>& data, const std::string& separator)
{
std::ostringstream result;

for (size_t i = 0; i < (data.size() - 1); ++i)
switch (data.size())
{
result << data[i] << separator;
}

if (!data.empty()) [[likely]]
case 0:
return "";
case 1:
return std::string{data[0]};
default:
{
result << data[data.size() - 1];
}
std::string result{data[0]};

for (auto it = data.begin() + 1; it != data.end(); ++it)
{
result += separator;
result += *it;
}

return result.str();
return result;
}
}
}

std::string StringHelper::repeat(const std::string& data, int repetition)
{
std::string result;

result.reserve(data.size() * static_cast<unsigned long>(repetition));

for (int i = 0; i < repetition; ++i)
{
result += data;
Expand Down
Loading

0 comments on commit 7fd3d28

Please sign in to comment.