Add more stuff to C string API

This commit is contained in:
UnknownShadow200 2017-05-12 19:46:33 +10:00
parent ff692478b3
commit 90181ed4db
3 changed files with 183 additions and 140 deletions

View File

@ -107,7 +107,7 @@ void Block_ResetProps(BlockID block) {
Int32 Block_FindID(String* name) {
Int32 block;
for (block = BlockID_Air; block < Block_Count; block++) {
if (String_CaselessEquals(&Block_Name[block], name)) return i;
if (String_CaselessEquals(&Block_Name[block], name)) return block;
}
return -1;
}

View File

@ -15,7 +15,6 @@ String String_FromRawBuffer(UInt8* buffer, UInt16 capacity) {
for (Int32 i = 0; i < capacity + 1; i++) {
buffer[i] = 0;
}
return str;
}
String String_FromConstant(const UInt8* buffer) {
@ -76,6 +75,41 @@ bool String_Append(String* str, UInt8 c) {
return true;
}
bool String_AppendNum(String* str, Int64 num) {
UInt8 numBuffer[20];
Int32 numLen = MakeNum(num, numBuffer);
for (Int32 i = numLen - 1; i >= 0; i--) {
if (!String_Append(str, numBuffer[i])) return false;
}
return true;
}
bool String_AppendPaddedNum(String* str, Int64 num, Int32 minDigits) {
UInt8 numBuffer[20];
for (Int32 i = 0; i < minDigits; i++) {
numBuffer[i] = '0';
}
Int32 numLen = MakeNum(num, numBuffer);
if (numLen < minDigits) numLen = minDigits;
for (Int32 i = numLen - 1; i >= 0; i--) {
if (!String_Append(str, numBuffer[i])) return false;
}
return true;
}
static Int32 String_MakeNum(Int64 num, UInt8* numBuffer) {
Int32 len = 0;
do {
numBuffer[len] = (char)('0' + (num % 10)); num /= 10;
len++;
} while (num > 0);
return len;
}
Int32 String_IndexOf(String* str, UInt8 c, Int32 offset) {
for (Int32 i = offset; i < str->length; i++) {

View File

@ -2,7 +2,7 @@
#define CS_STRING_H
#include "Typedefs.h"
/* Implements operations for a string.
Copyright 2017 ClassicalSharp | Licensed under BSD-3
Copyright 2017 ClassicalSharp | Licensed under BSD-3
*/
#define String_BufferSize(n) (n + 1)
@ -42,6 +42,15 @@ bool String_CaselessEquals(String* a, String* b);
/* Attempts to append a character to the end of a string. */
bool String_Append(String* str, UInt8 c);
/* Attempts to append an integer value to the end of a string. */
bool String_AppendNum(String* str, Int64 num);
/* Attempts to append an integer value to the end of a string, padding left with 0. */
bool String_AppendPaddedNum(String* str, Int64 num, Int32 minDigits);
static Int32 String_MakeNum(Int64 num, UInt8* numBuffer);
/* Finds the first index of c in given string, -1 if not found. */
int String_IndexOf(String* str, UInt8 c, Int32 offset);