summaryrefslogtreecommitdiffhomepage
path: root/dev/kernel/NewKit/KString.h
blob: 133fe945d6258d64cefbb8ba2428e3db825c0adf (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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/* -------------------------------------------

  Copyright (C) 2024-2025, Amlal El Mahrouss, all rights reserved.

------------------------------------------- */

#pragma once

#include <CompilerKit/CompilerKit.h>
#include <NewKit/Defines.h>
#include <NewKit/ErrorOr.h>
#include <NewKit/KernelPanic.h>
#include <NewKit/Utils.h>

#define cMinimumStringSize 8196

namespace Kernel {
/// @brief Kernel string class, not dynamic.
class KString final {
 public:
  explicit KString() {
    fDataSz = cMinimumStringSize;

    fData = new Char[fDataSz];
    MUST_PASS(fData);

    rt_set_memory(fData, 0, fDataSz);
  }

  explicit KString(SizeT Sz) : fDataSz(Sz) {
    MUST_PASS(Sz > 1);

    fData = new Char[Sz];
    MUST_PASS(fData);

    rt_set_memory(fData, 0, Sz);
  }

  ~KString() {
    if (fData) {
      delete[] fData;
      fData = nullptr;
    }
  }

  NE_COPY_DEFAULT(KString)

  Char*       Data();
  const Char* CData() const;
  Size        Length() const;

  bool operator==(const Char* rhs) const;
  bool operator!=(const Char* rhs) const;

  bool operator==(const KString& rhs) const;
  bool operator!=(const KString& rhs) const;

  KString& operator+=(const Char* rhs);
  KString& operator+=(const KString& rhs);

  operator bool() { return fData; }

  bool operator!() { return fData; }

 private:
  Char* fData{nullptr};
  Size  fDataSz{0};
  Size  fCur{0};

  friend class KStringBuilder;
};

struct KStringBuilder final {
  static ErrorOr<KString> Construct(const Char* data);
  static const Char*      FromBool(const Char* fmt, bool n);
  static const Char*      Format(const Char* fmt, const Char* from);
  static bool             Equals(const Char* lhs, const Char* rhs);
  static bool             Equals(const Utf16Char* lhs, const Utf16Char* rhs);
  static bool             Equals(const WideChar* lhs, const WideChar* rhs);
};
}  // namespace Kernel