1 // =================================
  2 // Copyright (c) 2024 Seppo Laakko
  3 // Distributed under the MIT license
  4 // =================================
  5 
  6 namespace System
  7 {
  8     public inline constexpr long Align(long nlong alignment)
  9     {
 10         #assert(alignment > 0);
 11         return (n + alignment - 1) & -alignment;
 12     }
 13 
 14     public constexpr long StrLen(const char* s)
 15     {
 16         long len = 0;
 17         if (s != null)
 18         {
 19             while (*s != '\0')
 20             {
 21                 ++len;
 22                 ++s;
 23             }
 24         }
 25         return len;
 26     }
 27 
 28     public constexpr long StrLen(const wchar* s)
 29     {
 30         long len = 0;
 31         if (s != null)
 32         {
 33             while (*s != '\0')
 34             {
 35                 ++len;
 36                 ++s;
 37             }
 38         }
 39         return len;
 40     }
 41 
 42     public constexpr long StrLen(const uchar* s)
 43     {
 44         long len = 0;
 45         if (s != null)
 46         {
 47             while (*s != '\0')
 48             {
 49                 ++len;
 50                 ++s;
 51             }
 52         }
 53         return len;
 54     }
 55 
 56     public void StrCopy(char* bufconst char* from)
 57     {
 58         #assert(buf != null);
 59         if (from != null)
 60         {
 61             while (*from != '\0')
 62             {
 63                 *buf++ = *from++;
 64             }
 65         }
 66         *buf = '\0';
 67     }
 68 
 69     public void StrCopy(wchar* bufconst wchar* from)
 70     {
 71         #assert(buf != null);
 72         if (from != null)
 73         {
 74             while (*from != '\0')
 75             {
 76                 *buf++ = *from++;
 77             }
 78         }
 79         *buf = '\0';
 80     }
 81 
 82     public void StrCopy(uchar* bufconst uchar* from)
 83     {
 84         #assert(buf != null);
 85         if (from != null)
 86         {
 87             while (*from != '\0')
 88             {
 89                 *buf++ = *from++;
 90             }
 91         }
 92         *buf = '\0';
 93     }
 94 
 95     public long StrCopy(char* bufconst char* fromlong length)
 96     {
 97         long resultLen = 0;
 98         #assert(buf != null);
 99         if (from != null)
100         {
101             while (resultLen < length)
102             {
103                 if (*from == '\0')
104                 {
105                     break;
106                 }
107                 *buf++ = *from++;
108                 ++resultLen;
109             }
110         }
111         *buf = '\0';
112         return resultLen;
113     }
114 
115     public long StrCopy(wchar* bufconst wchar* fromlong length)
116     {
117         long resultLen = 0;
118         #assert(buf != null);
119         if (from != null)
120         {
121             while (resultLen < length)
122             {
123                 if (*from == '\0')
124                 {
125                     break;
126                 }
127                 *buf++ = *from++;
128                 ++resultLen;
129             }
130         }
131         *buf = '\0';
132         return resultLen;
133     }
134 
135     public long StrCopy(uchar* bufconst uchar* fromlong length)
136     {
137         long resultLen = 0;
138         #assert(buf != null);
139         if (from != null)
140         {
141             while (resultLen < length)
142             {
143                 if (*from == '\0')
144                 {
145                     break;
146                 }
147                 *buf++ = *from++;
148                 ++resultLen;
149             }
150         }
151         *buf = '\0';
152         return resultLen;
153     }
154 
155     public long MemGrow(long size)
156     {
157         if (size < 8)
158         {
159             return 8;
160         }
161         else if (size < 64)
162         {
163             return 64;
164         }
165         else if (size < 512)
166         {
167             return 512;
168         }
169         else if (size < 4096)
170         {
171             return 4096;
172         }
173         else
174         {
175             return 2 * Align(size4096);
176         }
177     }