mirror of
https://github.com/ClassiCube/ClassiCube.git
synced 2025-09-19 12:35:52 -04:00
Implement parsing string to decimal for C string, combine String/StringConvert.h
This commit is contained in:
parent
9a77423171
commit
b75ed01813
@ -94,7 +94,7 @@ namespace ClassicalSharp {
|
||||
col = BlackTextShadows ? FastColour.Black : FastColour.Scale(col, 0.25f);
|
||||
i++; continue; // Skip over the colour code.
|
||||
}
|
||||
int coords = ConvertToCP437(c);
|
||||
int coords = Utils.UnicodeToCP437(c);
|
||||
|
||||
// First character in the string, begin run counting
|
||||
if (lastY == -1) {
|
||||
@ -185,7 +185,7 @@ namespace ClassicalSharp {
|
||||
}
|
||||
if (shadowCol) col = FastColour.Black.ToArgb();
|
||||
|
||||
int coords = ConvertToCP437(c);
|
||||
int coords = Utils.UnicodeToCP437(c);
|
||||
int width = PaddedWidth(point, widths[coords]);
|
||||
for (int xx = 0; xx < width; xx++)
|
||||
dstRow[x + xx] = col;
|
||||
@ -210,7 +210,7 @@ namespace ClassicalSharp {
|
||||
i++; continue; // Skip over the colour code.
|
||||
}
|
||||
|
||||
int coords = ConvertToCP437(c);
|
||||
int coords = Utils.UnicodeToCP437(c);
|
||||
total.Width += PaddedWidth(point, widths[coords]);
|
||||
}
|
||||
|
||||
@ -223,16 +223,6 @@ namespace ClassicalSharp {
|
||||
return total;
|
||||
}
|
||||
|
||||
public static int ConvertToCP437(char c) {
|
||||
if (c >= ' ' && c <= '~') return (int)c;
|
||||
|
||||
int cIndex = Utils.ControlCharReplacements.IndexOf(c);
|
||||
if (cIndex >= 0) return cIndex;
|
||||
int eIndex = Utils.ExtendedCharReplacements.IndexOf(c);
|
||||
if (eIndex >= 0) return 127 + eIndex;
|
||||
return (int)'?';
|
||||
}
|
||||
|
||||
protected int ShadowOffset(float fontSize) {
|
||||
return (int)(fontSize / 8);
|
||||
}
|
||||
|
@ -122,15 +122,8 @@ namespace ClassicalSharp.Network {
|
||||
byte code = buffer[index + i];
|
||||
if (length == 0 && !(code == 0 || code == 0x20))
|
||||
length = i + 1;
|
||||
|
||||
// Treat code as an index in code page 437
|
||||
if (code < 0x20) {
|
||||
characters[i] = Utils.ControlCharReplacements[code];
|
||||
} else if (code < 0x7F) {
|
||||
characters[i] = (char)code;
|
||||
} else {
|
||||
characters[i] = Utils.ExtendedCharReplacements[code - 0x7F];
|
||||
}
|
||||
|
||||
characters[i] = Utils.CP437ToUnicode(code);
|
||||
}
|
||||
index += maxLength;
|
||||
return length;
|
||||
|
@ -21,7 +21,7 @@ namespace ClassicalSharp.Network {
|
||||
for (int i = 0; i < count; i++) {
|
||||
char c = value[i];
|
||||
if (c == '&') c = '%'; // escape colour codes
|
||||
buffer[index + i] = (byte)IDrawer2D.ConvertToCP437(c);
|
||||
buffer[index + i] = Utils.UnicodeToCP437(c);
|
||||
}
|
||||
|
||||
for (int i = value.Length; i < Utils.StringLength; i++)
|
||||
|
@ -177,22 +177,33 @@ namespace ClassicalSharp {
|
||||
#endif
|
||||
|
||||
/// <summary> Conversion for code page 437 characters from index 0 to 31 to unicode. </summary>
|
||||
public const string ControlCharReplacements = "\0☺☻♥♦♣♠•◘○◙♂♀♪♫☼►◄↕‼¶§▬↨↑↓→←∟↔▲▼";
|
||||
const string ControlCharReplacements = "\0☺☻♥♦♣♠•◘○◙♂♀♪♫☼►◄↕‼¶§▬↨↑↓→←∟↔▲▼";
|
||||
|
||||
/// <summary> Conversion for code page 437 characters from index 127 to 255 to unicode. </summary>
|
||||
public const string ExtendedCharReplacements = "⌂ÇüéâäàåçêëèïîìÄÅÉæÆôöòûùÿÖÜ¢£¥₧ƒáíóúñѪº¿⌐¬½¼¡«»" +
|
||||
const string ExtendedCharReplacements = "⌂ÇüéâäàåçêëèïîìÄÅÉæÆôöòûùÿÖÜ¢£¥₧ƒáíóúñѪº¿⌐¬½¼¡«»" +
|
||||
"░▒▓│┤╡╢╖╕╣║╗╝╜╛┐└┴┬├─┼╞╟╚╔╩╦╠═╬╧╨╤╥╙╘╒╓╫╪┘┌" +
|
||||
"█▄▌▐▀αßΓπΣσµτΦΘΩδ∞φε∩≡±≥≤⌠⌡÷≈°∙·√ⁿ²■\u00a0";
|
||||
|
||||
#if !LAUNCHER
|
||||
public static bool IsValidInputChar(char c, bool supportsCP437) {
|
||||
if (c >= ' ' && c <= '~') return true; // ascii
|
||||
|
||||
bool isCP437 = Utils.ControlCharReplacements.IndexOf(c) >= 0 ||
|
||||
Utils.ExtendedCharReplacements.IndexOf(c) >= 0;
|
||||
return supportsCP437 && isCP437;
|
||||
bool isCP437 = UnicodeToCP437(c) != c;
|
||||
return supportsCP437 || !isCP437;
|
||||
}
|
||||
|
||||
public static byte UnicodeToCP437(char c) {
|
||||
if (c >= ' ' && c <= '~') return (byte)c;
|
||||
|
||||
int cIndex = ControlCharReplacements.IndexOf(c);
|
||||
if (cIndex >= 0) return (byte)cIndex;
|
||||
int eIndex = ExtendedCharReplacements.IndexOf(c);
|
||||
if (eIndex >= 0) return (byte)(127 + eIndex);
|
||||
return (byte)'?';
|
||||
}
|
||||
|
||||
public static char CP437ToUnicode(byte c) {
|
||||
if (c < 0x20) return ControlCharReplacements[c];
|
||||
if (c < 0x7F) return (char)c;
|
||||
return ExtendedCharReplacements[c - 0x7F];
|
||||
}
|
||||
#endif
|
||||
|
||||
public unsafe static string ToLower(string value) {
|
||||
fixed(char* ptr = value) {
|
||||
|
@ -225,7 +225,6 @@
|
||||
<ClInclude Include="SkyboxRenderer.h" />
|
||||
<ClInclude Include="Stream.h" />
|
||||
<ClInclude Include="GameStructs.h" />
|
||||
<ClInclude Include="StringConvert.h" />
|
||||
<ClInclude Include="TerrainAtlas.h" />
|
||||
<ClInclude Include="TickQueue.h" />
|
||||
<ClInclude Include="TiltComp.h" />
|
||||
@ -300,7 +299,6 @@
|
||||
<ClCompile Include="SkyboxRenderer.c" />
|
||||
<ClCompile Include="Stream.c" />
|
||||
<ClCompile Include="String.c" />
|
||||
<ClCompile Include="StringConvert.c" />
|
||||
<ClCompile Include="TerrainAtlas.c" />
|
||||
<ClCompile Include="Texture.c" />
|
||||
<ClCompile Include="TickQueue.c" />
|
||||
|
@ -276,9 +276,6 @@
|
||||
<ClInclude Include="DateTime.h">
|
||||
<Filter>Header Files\Utils</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="StringConvert.h">
|
||||
<Filter>Header Files\Utils</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="MapRenderer.h">
|
||||
<Filter>Header Files\Rendering\Map</Filter>
|
||||
</ClInclude>
|
||||
@ -467,9 +464,6 @@
|
||||
<ClCompile Include="WeatherRenderer.c">
|
||||
<Filter>Source Files\Rendering\Env</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="StringConvert.c">
|
||||
<Filter>Source Files\Utils</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="ChunkInfo.c">
|
||||
<Filter>Source Files\Rendering\Map</Filter>
|
||||
</ClCompile>
|
||||
|
@ -28,7 +28,7 @@
|
||||
Thus it is safe to allocate a string on the stack. */
|
||||
#define STRING_TRANSIENT
|
||||
|
||||
/* Indicates that a reference to the buffer in a string argument is retained after the function has completed.
|
||||
/* Indicates that a reference to the buffer in a string argument is persisted after the function has completed.
|
||||
Thus it is NOT SAFE to allocate a string on the stack. */
|
||||
#define STRING_REF
|
||||
#endif
|
@ -3,7 +3,7 @@
|
||||
#include "GraphicsAPI.h"
|
||||
#include "Game.h"
|
||||
#include "Events.h"
|
||||
#include "StringConvert.h"
|
||||
#include "String.h"
|
||||
|
||||
String ModelCache_charPngString;
|
||||
Int32 ModelCache_texCount, ModelCache_modelCount;
|
||||
|
@ -29,22 +29,16 @@ String String_FromReadonly(const UInt8* buffer) {
|
||||
length++; buffer++;
|
||||
}
|
||||
|
||||
String str;
|
||||
str.buffer = ptr;
|
||||
str.capacity = length;
|
||||
String str = String_FromEmptyBuffer(ptr, length);
|
||||
str.length = length;
|
||||
return str;
|
||||
}
|
||||
|
||||
String String_MakeNull(void) {
|
||||
String str;
|
||||
str.buffer = NULL;
|
||||
str.capacity = 0;
|
||||
str.length = 0;
|
||||
return str;
|
||||
return String_FromEmptyBuffer(NULL, 0);
|
||||
}
|
||||
|
||||
void String_MakeLowercase(String* str) {
|
||||
void String_MakeLowercase(STRING_TRANSIENT String* str) {
|
||||
Int32 i;
|
||||
for (i = 0; i < str->length; i++) {
|
||||
str->buffer[i] = Char_ToLower(str->buffer[i]);
|
||||
@ -52,7 +46,7 @@ void String_MakeLowercase(String* str) {
|
||||
}
|
||||
|
||||
|
||||
bool String_Equals(String* a, String* b) {
|
||||
bool String_Equals(STRING_TRANSIENT String* a, STRING_TRANSIENT String* b) {
|
||||
if (a->length != b->length) return false;
|
||||
Int32 i;
|
||||
|
||||
@ -62,7 +56,7 @@ bool String_Equals(String* a, String* b) {
|
||||
return true;
|
||||
}
|
||||
|
||||
bool String_CaselessEquals(String* a, String* b) {
|
||||
bool String_CaselessEquals(STRING_TRANSIENT String* a, STRING_TRANSIENT String* b) {
|
||||
if (a->length != b->length) return false;
|
||||
Int32 i;
|
||||
|
||||
@ -75,7 +69,7 @@ bool String_CaselessEquals(String* a, String* b) {
|
||||
}
|
||||
|
||||
|
||||
bool String_Append(String* str, UInt8 c) {
|
||||
bool String_Append(STRING_TRANSIENT String* str, UInt8 c) {
|
||||
if (str->length == str->capacity) return false;
|
||||
|
||||
str->buffer[str->length] = c;
|
||||
@ -83,7 +77,17 @@ bool String_Append(String* str, UInt8 c) {
|
||||
return true;
|
||||
}
|
||||
|
||||
bool String_AppendInt32(String* str, Int32 num) {
|
||||
Int32 String_MakeInt32(Int32 num, UInt8* numBuffer) {
|
||||
Int32 len = 0;
|
||||
|
||||
do {
|
||||
numBuffer[len] = (UInt8)('0' + (num % 10));
|
||||
num /= 10; len++;
|
||||
} while (num > 0);
|
||||
return len;
|
||||
}
|
||||
|
||||
bool String_AppendInt32(STRING_TRANSIENT String* str, Int32 num) {
|
||||
if (num < 0) {
|
||||
num = -num;
|
||||
if (!String_Append(str, (UInt8)'-')) return false;
|
||||
@ -99,7 +103,7 @@ bool String_AppendInt32(String* str, Int32 num) {
|
||||
return true;
|
||||
}
|
||||
|
||||
bool String_AppendPaddedInt32(String* str, Int32 num, Int32 minDigits) {
|
||||
bool String_AppendPaddedInt32(STRING_TRANSIENT String* str, Int32 num, Int32 minDigits) {
|
||||
UInt8 numBuffer[20];
|
||||
Int32 i;
|
||||
for (i = 0; i < minDigits; i++) {
|
||||
@ -115,17 +119,7 @@ bool String_AppendPaddedInt32(String* str, Int32 num, Int32 minDigits) {
|
||||
return true;
|
||||
}
|
||||
|
||||
static Int32 String_MakeInt32(Int32 num, UInt8* numBuffer) {
|
||||
Int32 len = 0;
|
||||
|
||||
do {
|
||||
numBuffer[len] = (UInt8)('0' + (num % 10));
|
||||
num /= 10; len++;
|
||||
} while (num > 0);
|
||||
return len;
|
||||
}
|
||||
|
||||
bool String_AppendConstant(String* str, const UInt8* buffer) {
|
||||
bool String_AppendConstant(STRING_TRANSIENT String* str, const UInt8* buffer) {
|
||||
UInt8 cur = 0;
|
||||
|
||||
while ((cur = *buffer) != 0) {
|
||||
@ -135,7 +129,7 @@ bool String_AppendConstant(String* str, const UInt8* buffer) {
|
||||
return true;
|
||||
}
|
||||
|
||||
bool String_AppendString(String* str, String* buffer) {
|
||||
bool String_AppendString(STRING_TRANSIENT String* str, String* buffer) {
|
||||
Int32 i;
|
||||
|
||||
for (i = 0; i < buffer->length; i++) {
|
||||
@ -145,7 +139,7 @@ bool String_AppendString(String* str, String* buffer) {
|
||||
}
|
||||
|
||||
|
||||
Int32 String_IndexOf(String* str, UInt8 c, Int32 offset) {
|
||||
Int32 String_IndexOf(STRING_TRANSIENT String* str, UInt8 c, Int32 offset) {
|
||||
Int32 i;
|
||||
for (i = offset; i < str->length; i++) {
|
||||
if (str->buffer[i] == c) return i;
|
||||
@ -153,7 +147,7 @@ Int32 String_IndexOf(String* str, UInt8 c, Int32 offset) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
Int32 String_LastIndexOf(String* str, UInt8 c) {
|
||||
Int32 String_LastIndexOf(STRING_TRANSIENT String* str, UInt8 c) {
|
||||
Int32 i;
|
||||
for (i = str->length - 1; i >= 0; i--) {
|
||||
if (str->buffer[i] == c) return i;
|
||||
@ -161,7 +155,149 @@ Int32 String_LastIndexOf(String* str, UInt8 c) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
UInt8 String_CharAt(String* str, Int32 offset) {
|
||||
UInt8 String_CharAt(STRING_TRANSIENT String* str, Int32 offset) {
|
||||
if (offset < 0 || offset >= str->length) return 0;
|
||||
return str->buffer[offset];
|
||||
}
|
||||
|
||||
|
||||
#define Convert_ControlCharsCount 32
|
||||
UInt16 Convert_ControlChars[Convert_ControlCharsCount] = {
|
||||
0x0000, 0x263A, 0x263B, 0x2665, 0x2666, 0x2663, 0x2660, 0x2022,
|
||||
0x25D8, 0x25CB, 0x25D9, 0x2642, 0x2640, 0x266A, 0x266B, 0x263C,
|
||||
0x25BA, 0x25C4, 0x2195, 0x203C, 0x00B6, 0x00A7, 0x25AC, 0x21A8,
|
||||
0x2191, 0x2193, 0x2192, 0x2190, 0x221F, 0x2194, 0x25B2, 0x25BC
|
||||
};
|
||||
|
||||
#define Convert_ExtendedCharsCount 129
|
||||
UInt16 Convert_ExtendedChars[Convert_ExtendedCharsCount] = { 0x2302,
|
||||
0x00C7, 0x00FC, 0x00E9, 0x00E2, 0x00E4, 0x00E0, 0x00E5, 0x00E7,
|
||||
0x00EA, 0x00EB, 0x00E8, 0x00EF, 0x00EE, 0x00EC, 0x00C4, 0x00C5,
|
||||
0x00C9, 0x00E6, 0x00C6, 0x00F4, 0x00F6, 0x00F2, 0x00FB, 0x00F9,
|
||||
0x00FF, 0x00D6, 0x00DC, 0x00A2, 0x00A3, 0x00A5, 0x20A7, 0x0192,
|
||||
0x00E1, 0x00ED, 0x00F3, 0x00FA, 0x00F1, 0x00D1, 0x00AA, 0x00BA,
|
||||
0x00BF, 0x2310, 0x00AC, 0x00BD, 0x00BC, 0x00A1, 0x00AB, 0x00BB,
|
||||
0x2591, 0x2592, 0x2593, 0x2502, 0x2524, 0x2561, 0x2562, 0x2556,
|
||||
0x2555, 0x2563, 0x2551, 0x2557, 0x255D, 0x255C, 0x255B, 0x2510,
|
||||
0x2514, 0x2534, 0x252C, 0x251C, 0x2500, 0x253C, 0x255E, 0x255F,
|
||||
0x255A, 0x2554, 0x2569, 0x2566, 0x2560, 0x2550, 0x256C, 0x2567,
|
||||
0x2568, 0x2564, 0x2565, 0x2559, 0x2558, 0x2552, 0x2553, 0x256B,
|
||||
0x256A, 0x2518, 0x250C, 0x2588, 0x2584, 0x258C, 0x2590, 0x2580,
|
||||
0x03B1, 0x00DF, 0x0393, 0x03C0, 0x03A3, 0x03C3, 0x00B5, 0x03C4,
|
||||
0x03A6, 0x0398, 0x03A9, 0x03B4, 0x221E, 0x03C6, 0x03B5, 0x2229,
|
||||
0x2261, 0x00B1, 0x2265, 0x2264, 0x2320, 0x2321, 0x00F7, 0x2248,
|
||||
0x00B0, 0x2219, 0x00B7, 0x221A, 0x207F, 0x00B2, 0x25A0, 0x00A0
|
||||
};
|
||||
|
||||
UInt16 Convert_CP437ToUnicode(UInt8 c) {
|
||||
if (c < 0x20) return Convert_ControlChars[c];
|
||||
if (c < 0x7F) return c;
|
||||
return Convert_ExtendedChars[c - 0x7F];
|
||||
}
|
||||
|
||||
UInt8 Convert_UnicodeToCP437(UInt16 c) {
|
||||
if (c >= 0x20 && c < 0x7F) return (UInt8)c;
|
||||
UInt32 i;
|
||||
|
||||
for (i = 0; i < Convert_ControlCharsCount; i++) {
|
||||
if (Convert_ControlChars[i] == c) return (UInt8)i;
|
||||
}
|
||||
for (i = 0; i < Convert_ExtendedCharsCount; i++) {
|
||||
if (Convert_ExtendedChars[i] == c) return (UInt8)(i + 0x7F);
|
||||
}
|
||||
return (UInt8)'?';
|
||||
}
|
||||
|
||||
bool Convert_TryParseInt32(STRING_TRANSIENT String* str, Int32* value) {
|
||||
Int32 sum = 0, i = 0;
|
||||
*value = 0;
|
||||
|
||||
/* Handle number signs */
|
||||
bool negate = false;
|
||||
if (str->length > 0 && str->buffer[0] == '-') { negate = true; i++; }
|
||||
if (str->length > 0 && str->buffer[0] == '+') { i++; }
|
||||
|
||||
/* TODO: CHECK THIS WORKS!!! */
|
||||
for (; i < str->length; i++) {
|
||||
UInt8 c = str->buffer[i];
|
||||
if (c < '0' || c > '9') return false;
|
||||
Int32 digit = c - '0';
|
||||
|
||||
/* Magnitude of largest negative integer cannot be expressed
|
||||
as a positive integer, so this case must be specially handled. */
|
||||
if (sum == (Int32)214748364 && digit == 8 && negate) {
|
||||
*value = Int32_MinValue;
|
||||
return true;
|
||||
}
|
||||
|
||||
/* Overflow handling */
|
||||
if (sum >= (Int32)214748364) {
|
||||
Int32 diff = sum - (Int32)214748364;
|
||||
diff *= 10; diff += digit;
|
||||
|
||||
/* Handle magnitude of max negative value specially,
|
||||
as it cannot be represented as a positive integer */
|
||||
if (diff == 8 && negate) {
|
||||
*value = Int32_MinValue;
|
||||
return true;
|
||||
}
|
||||
|
||||
/* Overflows max positive value */
|
||||
if (diff > 7) return false;
|
||||
}
|
||||
sum *= 10; sum += digit;
|
||||
}
|
||||
|
||||
if (negate) sum = -sum;
|
||||
*value = sum;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool Convert_TryParseUInt8(STRING_TRANSIENT String* str, UInt8* value) {
|
||||
*value = 0; Int32 tmp;
|
||||
if (!Convert_TryParseInt32(str, &tmp) || tmp < 0 || tmp > UInt8_MaxValue) return false;
|
||||
*value = (UInt8)tmp; return true;
|
||||
}
|
||||
|
||||
bool Convert_TryParseUInt16(STRING_TRANSIENT String* str, UInt16* value) {
|
||||
*value = 0; Int32 tmp;
|
||||
if (!Convert_TryParseInt32(str, &tmp) || tmp < 0 || tmp > UInt16_MaxValue) return false;
|
||||
*value = (UInt16)tmp; return true;
|
||||
}
|
||||
|
||||
bool Convert_TryParseReal32(STRING_TRANSIENT String* str, Real32* value) {
|
||||
Int32 i = 0;
|
||||
*value = 0.0f;
|
||||
bool foundDecimalPoint = false;
|
||||
Real32 whole = 0.0f, fract = 0.0f, divide = 10.0f;
|
||||
|
||||
/* Handle number signs */
|
||||
bool negate = false;
|
||||
if (str->length > 0 && str->buffer[0] == '-') { negate = true; i++; }
|
||||
if (str->length > 0 && str->buffer[0] == '+') { i++; }
|
||||
|
||||
/* TODO: CHECK THIS WORKS!!! */
|
||||
for (; i < str->length; i++) {
|
||||
UInt8 c = str->buffer[i];
|
||||
/* Floating point number can have . in it */
|
||||
if (c == '.' || c == ',') {
|
||||
if (foundDecimalPoint) return false;
|
||||
foundDecimalPoint = true;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (c < '0' || c > '9') return false;
|
||||
Int32 digit = c - '0';
|
||||
if (!foundDecimalPoint) {
|
||||
whole *= 10; whole += digit;
|
||||
}
|
||||
else {
|
||||
fract += digit / divide; divide *= 10;
|
||||
}
|
||||
}
|
||||
|
||||
Real32 sum = whole + fract;
|
||||
if (negate) sum = -sum;
|
||||
*value = sum;
|
||||
return true;
|
||||
}
|
@ -1,7 +1,10 @@
|
||||
#ifndef CS_STRING_H
|
||||
#define CS_STRING_H
|
||||
#include "Typedefs.h"
|
||||
#include "Compiler.h"
|
||||
/* Implements operations for a string.
|
||||
Also implements conversions betweens strings and numbers.
|
||||
Also implements converting code page 437 indices to/from unicode.
|
||||
Copyright 2017 ClassicalSharp | Licensed under BSD-3
|
||||
*/
|
||||
|
||||
@ -10,66 +13,61 @@
|
||||
typedef struct String_ {
|
||||
/* Pointer to raw characters. Size is capacity + 1, as buffer is null terminated. */
|
||||
UInt8* buffer;
|
||||
|
||||
/* Number of characters used. */
|
||||
UInt16 length;
|
||||
|
||||
/* Max number of characters that can be in buffer. */
|
||||
UInt16 capacity;
|
||||
} String;
|
||||
|
||||
/* Constructs a new string, pointing a buffer consisting purely of NULL characters. */
|
||||
String String_FromEmptyBuffer(UInt8* buffer, UInt16 capacity);
|
||||
|
||||
/* Constructs a new string, pointing a buffer consisting of arbitary data.
|
||||
NOTE: This method sets the bytes occupied by the string to NUL. */
|
||||
String String_FromRawBuffer(UInt8* buffer, UInt16 capacity);
|
||||
|
||||
/* Constructs a new string from a constant readonly string. */
|
||||
/* Constructs a new string from a constant readonly buffer. */
|
||||
String String_FromReadonly(const UInt8* buffer);
|
||||
|
||||
#define String_FromConstant(text) { text, (UInt16)(sizeof(text) - 1), (UInt16)(sizeof(text) - 1)};
|
||||
|
||||
|
||||
/* Makes an empty string that points to nowhere. */
|
||||
String String_MakeNull(void);
|
||||
/* Constructs a new string from a compile time string constant. */
|
||||
#define String_FromConstant(text) { text, (UInt16)(sizeof(text) - 1), (UInt16)(sizeof(text) - 1)};
|
||||
|
||||
/* Sets all characters in the given string to lowercase. */
|
||||
void String_MakeLowercase(String* str);
|
||||
|
||||
void String_MakeLowercase(STRING_TRANSIENT String* str);
|
||||
|
||||
/* Returns whether two strings have same contents. */
|
||||
bool String_Equals(String* a, String* b);
|
||||
|
||||
bool String_Equals(STRING_TRANSIENT String* a, STRING_TRANSIENT String* b);
|
||||
/* Returns whether two strings have same case-insensitive contents. */
|
||||
bool String_CaselessEquals(String* a, String* b);
|
||||
|
||||
bool String_CaselessEquals(STRING_TRANSIENT String* a, STRING_TRANSIENT String* b);
|
||||
|
||||
/* Attempts to append a character to the end of a string. */
|
||||
bool String_Append(String* str, UInt8 c);
|
||||
|
||||
bool String_Append(STRING_TRANSIENT String* str, UInt8 c);
|
||||
/* Attempts to append an integer value to the end of a string. */
|
||||
bool String_AppendInt32(String* str, Int32 num);
|
||||
|
||||
bool String_AppendInt32(STRING_TRANSIENT String* str, Int32 num);
|
||||
/* Attempts to append an integer value to the end of a string, padding left with 0. */
|
||||
bool String_AppendPaddedInt32(String* str, Int32 num, Int32 minDigits);
|
||||
|
||||
static Int32 String_MakeInt32(Int32 num, UInt8* numBuffer);
|
||||
|
||||
bool String_AppendPaddedInt32(STRING_TRANSIENT String* str, Int32 num, Int32 minDigits);
|
||||
/* Attempts to append a constant raw null-terminated string. */
|
||||
bool String_AppendConstant(String* str, const UInt8* buffer);
|
||||
|
||||
bool String_AppendConstant(STRING_TRANSIENT String* str, const UInt8* buffer);
|
||||
/* Attempts to append a string. */
|
||||
bool String_AppendString(String* str, String* buffer);
|
||||
|
||||
bool String_AppendString(STRING_TRANSIENT String* str, String* buffer);
|
||||
|
||||
/* Finds the first index of c in given string, -1 if not found. */
|
||||
Int32 String_IndexOf(String* str, UInt8 c, Int32 offset);
|
||||
|
||||
Int32 String_IndexOf(STRING_TRANSIENT String* str, UInt8 c, Int32 offset);
|
||||
/* Finds the last index of c in given string, -1 if not found. */
|
||||
Int32 String_LastIndexOf(String* str, UInt8 c);
|
||||
|
||||
Int32 String_LastIndexOf(STRING_TRANSIENT String* str, UInt8 c);
|
||||
/* Gets the character at the given index in the string. */
|
||||
UInt8 String_CharAt(String* str, Int32 offset);
|
||||
UInt8 String_CharAt(STRING_TRANSIENT String* str, Int32 offset);
|
||||
|
||||
/* Converts a code page 437 index into a unicode character. */
|
||||
UInt16 Convert_CP437ToUnicode(UInt8 c);
|
||||
/* Converts a unicode character into a code page 437 index. */
|
||||
UInt8 Convert_UnicodeToCP437(UInt16 c);
|
||||
|
||||
/* Attempts to parse the given string as a signed 32 bit integer.*/
|
||||
bool Convert_TryParseInt32(STRING_TRANSIENT String* str, Int32* value);
|
||||
/* Attempts to parse the given string as an unsigned 8 bit integer.*/
|
||||
bool Convert_TryParseUInt8(STRING_TRANSIENT String* str, UInt8* value);
|
||||
/* Attempts to parse the given string as an unsigned 8 bit integer.*/
|
||||
bool Convert_TryParseUInt16(STRING_TRANSIENT String* str, UInt16* value);
|
||||
/* Attempts to parse the given string as a 32 bit floating point number. */
|
||||
bool Convert_TryParseReal32(STRING_TRANSIENT String* str, Real32* value);
|
||||
#endif
|
@ -1,101 +0,0 @@
|
||||
#include "StringConvert.h"
|
||||
|
||||
UInt16 Convert_ControlChars[32] = {
|
||||
0x0000, 0x263A, 0x263B, 0x2665, 0x2666, 0x2663, 0x2660, 0x2022,
|
||||
0x25D8, 0x25CB, 0x25D9, 0x2642, 0x2640, 0x266A, 0x266B, 0x263C,
|
||||
0x25BA, 0x25C4, 0x2195, 0x203C, 0x00B6, 0x00A7, 0x25AC, 0x21A8,
|
||||
0x2191, 0x2193, 0x2192, 0x2190, 0x221F, 0x2194, 0x25B2, 0x25BC
|
||||
};
|
||||
|
||||
UInt16 Convert_ExtendedChars[129] = { 0x2302,
|
||||
0x00C7, 0x00FC, 0x00E9, 0x00E2, 0x00E4, 0x00E0, 0x00E5, 0x00E7,
|
||||
0x00EA, 0x00EB, 0x00E8, 0x00EF, 0x00EE, 0x00EC, 0x00C4, 0x00C5,
|
||||
0x00C9, 0x00E6, 0x00C6, 0x00F4, 0x00F6, 0x00F2, 0x00FB, 0x00F9,
|
||||
0x00FF, 0x00D6, 0x00DC, 0x00A2, 0x00A3, 0x00A5, 0x20A7, 0x0192,
|
||||
0x00E1, 0x00ED, 0x00F3, 0x00FA, 0x00F1, 0x00D1, 0x00AA, 0x00BA,
|
||||
0x00BF, 0x2310, 0x00AC, 0x00BD, 0x00BC, 0x00A1, 0x00AB, 0x00BB,
|
||||
0x2591, 0x2592, 0x2593, 0x2502, 0x2524, 0x2561, 0x2562, 0x2556,
|
||||
0x2555, 0x2563, 0x2551, 0x2557, 0x255D, 0x255C, 0x255B, 0x2510,
|
||||
0x2514, 0x2534, 0x252C, 0x251C, 0x2500, 0x253C, 0x255E, 0x255F,
|
||||
0x255A, 0x2554, 0x2569, 0x2566, 0x2560, 0x2550, 0x256C, 0x2567,
|
||||
0x2568, 0x2564, 0x2565, 0x2559, 0x2558, 0x2552, 0x2553, 0x256B,
|
||||
0x256A, 0x2518, 0x250C, 0x2588, 0x2584, 0x258C, 0x2590, 0x2580,
|
||||
0x03B1, 0x00DF, 0x0393, 0x03C0, 0x03A3, 0x03C3, 0x00B5, 0x03C4,
|
||||
0x03A6, 0x0398, 0x03A9, 0x03B4, 0x221E, 0x03C6, 0x03B5, 0x2229,
|
||||
0x2261, 0x00B1, 0x2265, 0x2264, 0x2320, 0x2321, 0x00F7, 0x2248,
|
||||
0x00B0, 0x2219, 0x00B7, 0x221A, 0x207F, 0x00B2, 0x25A0, 0x00A0
|
||||
};
|
||||
|
||||
UInt16 Convert_CP437ToUnicode(UInt8 c) {
|
||||
if (c < 0x20) return Convert_ControlChars[c];
|
||||
if (c < 0x7F) return c;
|
||||
return Convert_ExtendedChars[c - 0x7F];
|
||||
}
|
||||
|
||||
UInt8 Convert_UnicodeToCP437(UInt16 c) {
|
||||
if (c >= 0x20 && c < 0x7F) return (UInt8)c;
|
||||
UInt32 i;
|
||||
|
||||
for (i = 0; i < Convert_ControlCharsCount; i++) {
|
||||
if (Convert_ControlChars[i] == c) return (UInt8)i;
|
||||
}
|
||||
for (i = 0; i < Convert_ExtendedCharsCount; i++) {
|
||||
if (Convert_ExtendedChars[i] == c) return (UInt8)(i + 0x7F);
|
||||
}
|
||||
return (UInt8)'?';
|
||||
}
|
||||
|
||||
bool Convert_TryParseInt32(STRING_TRANSIENT String* str, Int32* value) {
|
||||
Int32 sum = 0, i = 0;
|
||||
*value = 0;
|
||||
|
||||
/* Handle negative numbers */
|
||||
bool negate = false;
|
||||
if (str->length > 0 && str->buffer[0] == '-') {
|
||||
negate = true; i++;
|
||||
}
|
||||
|
||||
/* TODO: CHECK THIS WORKS!!! */
|
||||
for (; i < str->length; i++) {
|
||||
UInt8 c = str->buffer[i];
|
||||
if (c < '0' || c > '9') return false;
|
||||
Int32 digit = c - '0';
|
||||
|
||||
/* Magnitude of largest negative integer cannot be expressed
|
||||
as a positive integer, so this case must be specially handled. */
|
||||
if (sum == (Int32)214748364 && digit == 8 && negate) {
|
||||
*value = Int32_MinValue;
|
||||
return true;
|
||||
}
|
||||
|
||||
/* Overflow handling */
|
||||
if (sum >= (Int32)214748364) {
|
||||
Int32 diff = sum - (Int32)214748364;
|
||||
diff *= 10; diff += digit;
|
||||
|
||||
/* Handle magnitude of max negative value specially,
|
||||
as it cannot be represented as a positive integer */
|
||||
if (diff == 8 && negate) {
|
||||
*value = Int32_MinValue;
|
||||
return true;
|
||||
}
|
||||
|
||||
/* Overflows max positive value */
|
||||
if (diff > 7) return false;
|
||||
}
|
||||
sum *= 10; sum += digit;
|
||||
}
|
||||
|
||||
if (negate) sum = -sum;
|
||||
*value = sum;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool Convert_TryParseUInt8(STRING_TRANSIENT String* str, UInt8* value) {
|
||||
*value = 0;
|
||||
Int32 tmp;
|
||||
if (!Convert_TryParseInt32(str, &tmp) || tmp < 0 || tmp > UInt8_MaxValue) return false;
|
||||
|
||||
*value = (UInt8)tmp;
|
||||
return true;
|
||||
}
|
@ -1,32 +0,0 @@
|
||||
#ifndef CS_STRINGCONVERT_H
|
||||
#define CS_STRINGCONVERT_H
|
||||
#include "Typedefs.h"
|
||||
#include "String.h"
|
||||
#include "Compiler.h"
|
||||
/* Implements conversions between code page 437 indices and unicode characters.
|
||||
Copyright 2014-2017 ClassicalSharp | Licensed under BSD-3
|
||||
*/
|
||||
|
||||
|
||||
#define Convert_ControlCharsCount 32
|
||||
/* Unicode values for the 32 code page 437 control indices. */
|
||||
UInt16 Convert_ControlChars[Convert_ControlCharsCount];
|
||||
|
||||
#define Convert_ExtendedCharsCount 129
|
||||
/* Unicode values for the 129 extended code page 437 indices. (including DELETE character)*/
|
||||
UInt16 Convert_ExtendedChars[Convert_ExtendedCharsCount];
|
||||
|
||||
|
||||
/* Converts a code page 437 index into a unicode character. */
|
||||
UInt16 Convert_CP437ToUnicode(UInt8 c);
|
||||
|
||||
/* Converts a unicode character into a code page 437 index. */
|
||||
UInt8 Convert_UnicodeToCP437(UInt16 c);
|
||||
|
||||
/* Attempts to parse the given string as a signed 32 bit integer.*/
|
||||
bool Convert_TryParseInt32(STRING_TRANSIENT String* str, Int32* value);
|
||||
|
||||
/* Attempts to parse the given string as an unsigned 8 bit integer.*/
|
||||
bool Convert_TryParseUInt8(STRING_TRANSIENT String* str, UInt8* value);
|
||||
|
||||
#endif
|
@ -43,6 +43,7 @@ typedef UInt8 TextureLoc;
|
||||
|
||||
#define UInt8_MaxValue ((UInt8)0xFF)
|
||||
#define Int16_MaxValue ((Int16)0x7FFF)
|
||||
#define UInt16_MaxValue ((UInt16)0xFFFF)
|
||||
#define Int32_MaxValue ((Int32)0x7FFFFFFFL)
|
||||
#define Int32_MinValue ((Int32)0xFFFFFFFFL)
|
||||
|
||||
|
@ -4,7 +4,7 @@
|
||||
#include "Mouse.h"
|
||||
#include "Key.h"
|
||||
#include "Events.h"
|
||||
#include "StringConvert.h"
|
||||
#include "String.h"
|
||||
|
||||
#define win_Style WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN
|
||||
#define win_StyleEx WS_EX_WINDOWEDGE | WS_EX_APPWINDOW
|
||||
|
Loading…
x
Reference in New Issue
Block a user