Skip to content

Commit

Permalink
Add FixedSizeString
Browse files Browse the repository at this point in the history
  • Loading branch information
durswd committed Jul 22, 2023
1 parent 9219bb5 commit 19f7ff9
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 17 deletions.
123 changes: 107 additions & 16 deletions Dev/Cpp/Effekseer/Effekseer/Utils/String.h
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
#ifndef __EFFEKSEER_STRING_H__
#define __EFFEKSEER_STRING_H__

#include <array>
#include <stdint.h>
#include <string>
#include <vector>

#include "Effekseer.CustomAllocator.h"

namespace Effekseer
{

Expand Down Expand Up @@ -89,6 +92,80 @@ class StringView
size_t size_;
};

template <typename T, int N>
class FixedSizeString
{
using Traits = std::char_traits<T>;

public:
FixedSizeString()
: size_(0)
{
data_[0] = 0;
}

FixedSizeString(const T* ptr)
{
size_t original_length = Traits::length(ptr);
size_ = std::min(data_.size() - 1, original_length);
memcpy(data_.data(), ptr, size_ * sizeof(T));
data_[size_] = 0;
}

FixedSizeString<T, N> substr(size_t pos = 0,
size_t n = std::string::npos) const
{
FixedSizeString<T, N> ret;
if (n == std::string::npos)
{
ret.size_ = size_ - pos;
}
else
{
ret.size_ = n;
}

memcpy(ret.data_.data(), data_.data() + pos, ret.size_ * sizeof(T));
ret.data_[ret.size_] = 0;

return ret;
}

T& operator[](size_t i)
{
return data_[i];
}

T operator[](size_t i) const
{
return data_[i];
}

const T* data() const
{
return data_.data();
}

size_t size() const
{
return size_;
}

bool operator==(const FixedSizeString<T, N>& rhs) const
{
return size() == rhs.size() && Traits::compare(data(), rhs.data(), size()) == 0;
}

bool operator!=(const FixedSizeString<T, N>& rhs) const
{
return size() != rhs.size() || Traits::compare(data(), rhs.data(), size()) != 0;
}

private:
std::array<T, N> data_;
size_t size_;
};

class StringHelper
{
public:
Expand Down Expand Up @@ -203,6 +280,30 @@ class PathHelper
return StringHelper::Join(elems, StringHelper::To<T>("/"));
}

template <typename STRING, typename T>
static STRING GetDirectoryName(const STRING& path)
{
if (path.size() == 0)
{
return STRING();
}

int last_separator = -1;
for (size_t i = 0; i < path.size(); i++)
{
if (IsSeparator(path[i]))
{
last_separator = i;
}
}

if (last_separator >= 0)
{
return path.substr(0, last_separator + 1);
}
return STRING();
}

public:
template <typename T>
static bool IsSeparator(T s)
Expand Down Expand Up @@ -300,23 +401,13 @@ class PathHelper
template <typename T>
static std::basic_string<T> GetDirectoryName(const std::basic_string<T>& path)
{
if (path == StringHelper::To<T>(""))
return StringHelper::To<T>("");

int last_separator = -1;
for (size_t i = 0; i < path.size(); i++)
{
if (IsSeparator(path[i]))
{
last_separator = i;
}
}
return GetDirectoryName<std::basic_string<T>, T>(path);
}

if (last_separator >= 0)
{
return path.substr(0, last_separator + 1);
}
return StringHelper::To<T>("");
template <typename T, int N>
static FixedSizeString<T, N> GetDirectoryName(const FixedSizeString<T, N>& path)
{
return GetDirectoryName<FixedSizeString<T, N>, T>(path);
}

template <typename T>
Expand Down
4 changes: 3 additions & 1 deletion Dev/Cpp/Test/Runtime/String.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

void StringAndPathHelperTest()
{

if (Effekseer::StringHelper::To<char16_t>("hoge") != std::u16string(u"hoge"))
throw "";

Expand Down Expand Up @@ -31,6 +30,9 @@ void StringAndPathHelperTest()
if (Effekseer::PathHelper::GetDirectoryName(std::u16string(u"/a/b/e")) != std::u16string(u"/a/b/"))
throw "";

if (Effekseer::PathHelper::GetDirectoryName(Effekseer::FixedSizeString<char16_t, 256>(u"/a/b/e")) != Effekseer::FixedSizeString<char16_t, 256>(u"/a/b/"))
throw "";

if (Effekseer::PathHelper::GetDirectoryName(std::u16string(u"/a/b/e/")) != std::u16string(u"/a/b/e/"))
throw "";

Expand Down

0 comments on commit 19f7ff9

Please sign in to comment.