summaryrefslogtreecommitdiff
path: root/src/util/string.h
blob: 47da1a0e8aae138f242fbc9fc4fcb80f11ab77ca (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#pragma once
#include <string.h>

#include "typedef.h"

constexpr U32 strlen_ct( const char* str ) {
  U32 len = 0;
  for( ; !!str[len]; ++len );
  return len;
}

template <U32 N>
struct STR {
  char data[N]{ 0 };
  enum {
    size = N
  };
  STR() {
    memset( data, 0, N );
  }
  STR( const char* str ) {
    memcpy( data, str, strlen_ct( str ) );
  }
  STR( const STR<N>& str ) {
    memcpy( data, str.data, N );
  }

  template <U32 other>
  auto operator+( const STR<other>& rhs ) {
    constexpr U32 l1 = strlen_ct( data );
    constexpr U32 l2 = strlen_ct( rhs.data );

    constexpr U32 high = N > other ? N : other;
    constexpr U32 max = (l1 + l2 > high) ? l1 + l2 + 1 : high;

    STR<max> result;
    memcpy( result.data, data, l1 );
    memcpy( result.data + l1, rhs.data, l2 );
    result.data[l1 + l2] = '\0';
    return result;
  }

  template <U32 other>
  auto concat( const STR<other>& str ) {
    return *this + str;
  }

  operator char*() { return data; }
};