Initial commit

This commit is contained in:
2024-06-01 23:43:44 -04:00
commit 7d241a5a21
4 changed files with 51 additions and 0 deletions

14
CMakeLists.txt Normal file
View File

@@ -0,0 +1,14 @@
cmake_minimum_required(VERSION 3.18)
project(uuid)
set(CMAKE_CXX_STANDARD 20)
file(GLOB_RECURSE HEADERS "include/*.h")
file(GLOB_RECURSE SOURCES "src/*.cpp")
include_directories("include")
add_library(uuid SHARED ${SOURCES} ${HEADERS})
add_executable(uuidtest main.cpp)
set_target_properties(uuid PROPERTIES LINKER_LANGUAGE CXX)
target_link_libraries(uuidtest PUBLIC uuid)

7
include/uuid/uuid.h Normal file
View File

@@ -0,0 +1,7 @@
#pragma once
#include <string>
namespace UUID {
int dice(int min, int max);
std::string generateUUID(unsigned int length);
}

6
main.cpp Normal file
View File

@@ -0,0 +1,6 @@
#include <iostream>
#include <uuid/uuid.h>
int main() {
std::cout << UUID::generateUUID(16) << std::endl;
std::cout << UUID::dice(0, 65535) << std::endl;
}

24
src/uuid.cpp Normal file
View File

@@ -0,0 +1,24 @@
#include <random>
#include <uuid/uuid.h>
#include <array>
std::random_device randomDevice;
std::mt19937 rng(randomDevice());
std::array<char, 64> characters = {
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '%', '!'
};
int UUID::dice(int min, int max) {
std::uniform_int_distribution<> generate(min, max);
return generate(rng);
}
std::string UUID::generateUUID(const unsigned int length) {
std::string result;
for (int i = 0; i < length; i++)
result.push_back(characters[UUID::dice(0, 63)]);
return result;
}