Fix extra \n being grabbed in return type parsing

This commit is contained in:
doyle 2019-08-24 16:00:09 +10:00
parent 95272e77ee
commit e2a3fc5bed
2 changed files with 53 additions and 38 deletions

View File

@ -596,8 +596,9 @@ struct Dqn_StringBuilder
isize string_len; isize string_len;
}; };
template <size_t N> DQN_HEADER_COPY_PROTOTYPE(
FILE_SCOPE char *Dqn_StringBuilder__GetWriteBufferAndUpdateUsage(Dqn_StringBuilder<N> *builder, usize size_required) template <size_t N> FILE_SCOPE char *,
Dqn_StringBuilder__GetWriteBufferAndUpdateUsage(Dqn_StringBuilder<N> *builder, usize size_required))
{ {
char *result = builder->fixed_mem + builder->fixed_mem_used; char *result = builder->fixed_mem + builder->fixed_mem_used;
usize space = Dqn_ArrayCount(builder->fixed_mem) - builder->fixed_mem_used; usize space = Dqn_ArrayCount(builder->fixed_mem) - builder->fixed_mem_used;
@ -650,8 +651,9 @@ FILE_SCOPE char *Dqn_StringBuilder__GetWriteBufferAndUpdateUsage(Dqn_StringBuild
return result; return result;
} }
template <usize N> DQN_HEADER_COPY_PROTOTYPE(
FILE_SCOPE void Dqn_StringBuilder__BuildOutput(Dqn_StringBuilder<N> const *builder, char *dest, isize dest_size) template <usize N> FILE_SCOPE void,
Dqn_StringBuilder__BuildOutput(Dqn_StringBuilder<N> const *builder, char *dest, isize dest_size))
{ {
// NOTE: No data appended to builder, just allocate am empty string. But // NOTE: No data appended to builder, just allocate am empty string. But
// always allocate, so we avoid adding making nullptr part of the possible // always allocate, so we avoid adding making nullptr part of the possible
@ -706,9 +708,10 @@ Dqn_StringBuilder_BuildInBuffer(Dqn_StringBuilder<N> const *builder, char *dest,
Dqn_StringBuilder__BuildOutput(builder, dest, dest_size); Dqn_StringBuilder__BuildOutput(builder, dest, dest_size);
} }
// len: Return the length of the allocated string including the null-terminator DQN_HEADER_COPY_PROTOTYPE_AND_COMMENT(
template <usize N> "len: Return the length of the allocated string including the null-terminator",
char *Dqn_StringBuilder_BuildFromMalloc(Dqn_StringBuilder<N> *builder, isize *len = nullptr) template <usize N>,
char *Dqn_StringBuilder_BuildFromMalloc(Dqn_StringBuilder<N> *builder, isize *len = nullptr))
{ {
isize len_w_null_terminator = Dqn_StringBuilder_BuildLen(builder); isize len_w_null_terminator = Dqn_StringBuilder_BuildLen(builder);
auto *result = static_cast<char *>(malloc(len_w_null_terminator)); auto *result = static_cast<char *>(malloc(len_w_null_terminator));
@ -717,8 +720,9 @@ char *Dqn_StringBuilder_BuildFromMalloc(Dqn_StringBuilder<N> *builder, isize *le
return result; return result;
} }
template <usize N> DQN_HEADER_COPY_PROTOTYPE(
char *Dqn_StringBuilder_BuildFromArena(Dqn_StringBuilder<N> *builder, Dqn_MemArena *arena, isize *len = nullptr) template <usize N>,
char *Dqn_StringBuilder_BuildFromArena(Dqn_StringBuilder<N> *builder, Dqn_MemArena *arena, isize *len = nullptr))
{ {
isize len_w_null_terminator = Dqn_StringBuilder_BuildLen(builder); isize len_w_null_terminator = Dqn_StringBuilder_BuildLen(builder);
char *result = MEM_ARENA_ALLOC_ARRAY(arena, char, len_w_null_terminator); char *result = MEM_ARENA_ALLOC_ARRAY(arena, char, len_w_null_terminator);
@ -727,8 +731,9 @@ char *Dqn_StringBuilder_BuildFromArena(Dqn_StringBuilder<N> *builder, Dqn_MemAre
return result; return result;
} }
template <usize N> DQN_HEADER_COPY_PROTOTYPE(
void Dqn_StringBuilder_VFmtAppend(Dqn_StringBuilder<N> *builder, char const *fmt, va_list va) template <usize N>,
void Dqn_StringBuilder_VFmtAppend(Dqn_StringBuilder<N> *builder, char const *fmt, va_list va))
{ {
if (!fmt) return; if (!fmt) return;
isize require = stbsp_vsnprintf(nullptr, 0, fmt, va) + 1; isize require = stbsp_vsnprintf(nullptr, 0, fmt, va) + 1;
@ -737,8 +742,9 @@ void Dqn_StringBuilder_VFmtAppend(Dqn_StringBuilder<N> *builder, char const *fmt
builder->string_len += (require - 1); // -1 to exclude null terminator builder->string_len += (require - 1); // -1 to exclude null terminator
} }
template <usize N> DQN_HEADER_COPY_PROTOTYPE(
void Dqn_StringBuilder_FmtAppend(Dqn_StringBuilder<N> *builder, char const *fmt, ...) template <usize N>,
void Dqn_StringBuilder_FmtAppend(Dqn_StringBuilder<N> *builder, char const *fmt, ...))
{ {
va_list va; va_list va;
va_start(va, fmt); va_start(va, fmt);
@ -746,8 +752,9 @@ void Dqn_StringBuilder_FmtAppend(Dqn_StringBuilder<N> *builder, char const *fmt,
va_end(va); va_end(va);
} }
template <usize N> DQN_HEADER_COPY_PROTOTYPE(
void Dqn_StringBuilder_Append(Dqn_StringBuilder<N> *builder, char const *str, isize len = -1) template <usize N>,
void Dqn_StringBuilder_Append(Dqn_StringBuilder<N> *builder, char const *str, isize len = -1))
{ {
if (!str) return; if (!str) return;
if (len == -1) len = (isize)strlen(str); if (len == -1) len = (isize)strlen(str);
@ -758,8 +765,9 @@ void Dqn_StringBuilder_Append(Dqn_StringBuilder<N> *builder, char const *str, is
buf[len] = 0; buf[len] = 0;
} }
template <usize N> DQN_HEADER_COPY_PROTOTYPE(
void Dqn_StringBuilder_AppendChar(Dqn_StringBuilder<N> *builder, char ch) template <usize N>,
void Dqn_StringBuilder_AppendChar(Dqn_StringBuilder<N> *builder, char ch))
{ {
char *buf = Dqn_StringBuilder__GetWriteBufferAndUpdateUsage(builder, 1 + 1 /*null terminator*/); char *buf = Dqn_StringBuilder__GetWriteBufferAndUpdateUsage(builder, 1 + 1 /*null terminator*/);
*buf++ = ch; *buf++ = ch;
@ -767,8 +775,6 @@ void Dqn_StringBuilder_AppendChar(Dqn_StringBuilder<N> *builder, char ch)
buf[1] = 0; buf[1] = 0;
} }
// ------------------------------------------------------------------------------------------------- // -------------------------------------------------------------------------------------------------
// //
// NOTE: (Memory) Slices // NOTE: (Memory) Slices
@ -794,8 +800,9 @@ struct Slice
}; };
#define SLICE_LITERAL(string) Slice<char const>(DQN_STR_AND_LEN(string)) #define SLICE_LITERAL(string) Slice<char const>(DQN_STR_AND_LEN(string))
template <typename T> DQN_HEADER_COPY_PROTOTYPE(
inline Slice<T> Slice_CopyNullTerminated(Dqn_MemArena *arena, T const *src, isize len) template <typename T>,
inline Slice<T> Slice_CopyNullTerminated(Dqn_MemArena *arena, T const *src, isize len))
{ {
Slice<T> result = {}; Slice<T> result = {};
result.len = len; result.len = len;
@ -805,15 +812,17 @@ inline Slice<T> Slice_CopyNullTerminated(Dqn_MemArena *arena, T const *src, isiz
return result; return result;
} }
template <typename T> DQN_HEADER_COPY_PROTOTYPE(
inline Slice<T> Slice_CopyNullTerminated(Dqn_MemArena *arena, Slice<T> const src) template <typename T>,
inline Slice<T> Slice_CopyNullTerminated(Dqn_MemArena *arena, Slice<T> const src))
{ {
Slice<T> result = Slice_CopyNullTerminated(arena, src.buf, src.len); Slice<T> result = Slice_CopyNullTerminated(arena, src.buf, src.len);
return result; return result;
} }
template <typename T> DQN_HEADER_COPY_PROTOTYPE(
inline Slice<T> Slice_Copy(Dqn_MemArena *arena, T const *src, isize len) template <typename T>,
inline Slice<T> Slice_Copy(Dqn_MemArena *arena, T const *src, isize len))
{ {
Slice<T> result = {}; Slice<T> result = {};
result.len = len; result.len = len;
@ -822,8 +831,9 @@ inline Slice<T> Slice_Copy(Dqn_MemArena *arena, T const *src, isize len)
return result; return result;
} }
template <typename T> DQN_HEADER_COPY_PROTOTYPE(
inline Slice<T> Slice_Copy(Dqn_MemArena *arena, Slice<T> const src) template <typename T>,
inline Slice<T> Slice_Copy(Dqn_MemArena *arena, Slice<T> const src))
{ {
Slice<T> result = Slice_Copy(arena, src.buf, src.len); Slice<T> result = Slice_Copy(arena, src.buf, src.len);
return result; return result;
@ -834,8 +844,9 @@ inline Slice<T> Slice_Copy(Dqn_MemArena *arena, Slice<T> const src)
// NOTE: Asprintf (Allocate Sprintf) // NOTE: Asprintf (Allocate Sprintf)
// //
// ------------------------------------------------------------------------------------------------- // -------------------------------------------------------------------------------------------------
template <typename T> DQN_HEADER_COPY_PROTOTYPE(
Slice<char> AsprintfSlice(T *arena, char const *fmt, va_list va) template <typename T> Slice<char>,
AsprintfSlice(T *arena, char const *fmt, va_list va))
{ {
Slice<char> result = {}; Slice<char> result = {};
result.len = stbsp_vsnprintf(nullptr, 0, fmt, va) + 1; result.len = stbsp_vsnprintf(nullptr, 0, fmt, va) + 1;
@ -845,8 +856,9 @@ Slice<char> AsprintfSlice(T *arena, char const *fmt, va_list va)
return result; return result;
} }
template <typename T> DQN_HEADER_COPY_PROTOTYPE(
Slice<char> AsprintfSlice(T *arena, char const *fmt, ...) template <typename T> Slice<char>,
AsprintfSlice(T *arena, char const *fmt, ...))
{ {
va_list va; va_list va;
va_start(va, fmt); va_start(va, fmt);
@ -855,8 +867,9 @@ Slice<char> AsprintfSlice(T *arena, char const *fmt, ...)
return result; return result;
} }
template <typename T> DQN_HEADER_COPY_PROTOTYPE(
char *Asprintf(T *arena, int *len, char const *fmt, ...) template <typename T> char *,
Asprintf(T *arena, int *len, char const *fmt, ...))
{ {
va_list va; va_list va;
va_start(va, fmt); va_start(va, fmt);
@ -866,8 +879,9 @@ char *Asprintf(T *arena, int *len, char const *fmt, ...)
return result.str; return result.str;
} }
template <typename T> DQN_HEADER_COPY_PROTOTYPE(
char *Asprintf(T *arena, char const *fmt, ...) template <typename T> char *,
Asprintf(T *arena, char const *fmt, ...))
{ {
va_list va; va_list va;
va_start(va, fmt); va_start(va, fmt);

View File

@ -473,7 +473,7 @@ char *Dqn_StrSkipWhitespace(char *buf)
char *ParseFunctionReturnType(char *ptr, isize *len) char *ParseFunctionReturnType(char *ptr, isize *len)
{ {
char *result = ptr; char *result = Dqn_StrSkipWhitespace(ptr);
isize result_len = 0; isize result_len = 0;
for (int scope = 0; ptr; ptr++) // NOTE: Parse the function return type for (int scope = 0; ptr; ptr++) // NOTE: Parse the function return type
{ {
@ -552,6 +552,7 @@ int main(char *argv[], int argc)
char constexpr HEADER_COPY_PROTOTYPE[] = "DQN_HEADER_COPY_PROTOTYPE"; char constexpr HEADER_COPY_PROTOTYPE[] = "DQN_HEADER_COPY_PROTOTYPE";
char constexpr HEADER_COPY_PROTOTYPE_AND_COMMENT[] = "DQN_HEADER_COPY_PROTOTYPE_AND_COMMENT"; char constexpr HEADER_COPY_PROTOTYPE_AND_COMMENT[] = "DQN_HEADER_COPY_PROTOTYPE_AND_COMMENT";
fprintf(stdout, "\n");
char *ptr = buf; char *ptr = buf;
char *ptr_end = buf + buf_size; char *ptr_end = buf + buf_size;
isize ptr_len = buf_size; isize ptr_len = buf_size;
@ -571,7 +572,7 @@ int main(char *argv[], int argc)
while (ptr[0] != ',') ptr++; while (ptr[0] != ',') ptr++;
ptr++; ptr++;
fprintf(stdout, "%.*s", (int)comment_len, comment); fprintf(stdout, "%.*s\n", (int)comment_len, comment);
} }
else else
{ {
@ -587,7 +588,7 @@ int main(char *argv[], int argc)
char *func_name = ParseFunctionNameAndParameters(ptr, &func_name_len); char *func_name = ParseFunctionNameAndParameters(ptr, &func_name_len);
ptr = func_name + func_name_len + 1; // Ptr is at macro closing paren, skip the paren ptr = func_name + func_name_len + 1; // Ptr is at macro closing paren, skip the paren
fprintf(stdout, "%.*s %.*s", (int)func_type_len, func_type, (int)func_name_len, func_name); fprintf(stdout, "%.*s %.*s\n", (int)func_type_len, func_type, (int)func_name_len, func_name);
} }
return 0; return 0;