From 15079ee79f85e63a2f578b9260871aefc9ffe66b Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Fri, 2 Aug 2019 19:37:03 +1000 Subject: [PATCH] Actually implement String_UNSAFE_SubstringAt as a function instead of a macro on top of String_UNSAFE_Substring This saves around 1ms when loading 431 fonts on my PC --- src/Chat.c | 2 +- src/String.c | 10 ++++++++++ src/String.h | 3 ++- 3 files changed, 13 insertions(+), 2 deletions(-) diff --git a/src/Chat.c b/src/Chat.c index 9dbd919a4..c8ace367c 100644 --- a/src/Chat.c +++ b/src/Chat.c @@ -303,7 +303,7 @@ static void Commands_Execute(const String* input) { offset = 1; } - text = String_UNSAFE_Substring(&text, offset, text.length - offset); + text = String_UNSAFE_SubstringAt(&text, offset); /* Check for only / or /client */ if (!text.length) { Commands_PrintDefault(); return; } diff --git a/src/String.c b/src/String.c index e2cc1deba..3ff91678e 100644 --- a/src/String.c +++ b/src/String.c @@ -61,6 +61,16 @@ String String_UNSAFE_Substring(STRING_REF const String* str, int offset, int len return String_Init(str->buffer + offset, length, length); } +String String_UNSAFE_SubstringAt(STRING_REF const String* str, int offset) { + String sub; + if (offset < 0 || offset > str->length) Logger_Abort("Sub offset out of range"); + + sub.buffer = str->buffer + offset; + sub.length = str->length - offset; + sub.capacity = str->length - offset; /* str->length to match String_UNSAFE_Substring */ + return sub; +} + int String_UNSAFE_Split(STRING_REF const String* str, char c, String* subs, int maxSubs) { int beg = 0, end, count, i; diff --git a/src/String.h b/src/String.h index 93090586c..1c0973ac5 100644 --- a/src/String.h +++ b/src/String.h @@ -67,7 +67,8 @@ CC_API void String_CopyToRaw(char* dst, int capacity, const String* src); /* UNSAFE: Returns a substring of the given string. (sub.buffer is within str.buffer + str.length) */ CC_API String String_UNSAFE_Substring(STRING_REF const String* str, int offset, int length); /* UNSAFE: Returns a substring of the given string. (sub.buffer is within str.buffer + str.length) */ -#define String_UNSAFE_SubstringAt(str, offset) (String_UNSAFE_Substring(str, offset, (str)->length - (offset))) +/* The substring returned is { str.buffer + offset, str.length - offset } */ +CC_API String String_UNSAFE_SubstringAt(STRING_REF const String* str, int offset); /* UNSAFE: Splits a string of the form [str1][c][str2][c][str3].. into substrings. */ /* e.g., "abc:id:xyz" becomes "abc","id","xyz" */ CC_API int String_UNSAFE_Split(STRING_REF const String* str, char c, String* subs, int maxSubs);