Skip to content

Commit 4066284

Browse files
authored
C: Return hb_string_T from token_to_string function (#781)
This PR changes the return type of the token to string method so that it returns an `hb_string_T` instead of a raw `char *`.
1 parent b13a6a9 commit 4066284

File tree

3 files changed

+7
-6
lines changed

3 files changed

+7
-6
lines changed

src/herb.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,9 @@ void herb_lex_to_buffer(const char* source, hb_buffer_T* output) {
6161
for (size_t i = 0; i < hb_array_size(tokens); i++) {
6262
token_T* token = hb_array_get(tokens, i);
6363

64-
char* type = token_to_string(token);
65-
hb_buffer_append(output, type);
66-
free(type);
64+
hb_string_T type = token_to_string(token);
65+
hb_buffer_append_string(output, type);
66+
free(type.data);
6767

6868
hb_buffer_append(output, "\n");
6969
}

src/include/token.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@
44
#include "lexer_struct.h"
55
#include "position.h"
66
#include "token_struct.h"
7+
#include "util/hb_string.h"
78

89
token_T* token_init(const char* value, token_type_T type, lexer_T* lexer);
9-
char* token_to_string(const token_T* token);
10+
hb_string_T token_to_string(const token_T* token);
1011
const char* token_type_to_string(token_type_T type);
1112

1213
token_T* token_copy(token_T* token);

src/token.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ const char* token_type_to_string(const token_type_T type) {
8383
return "Unknown token_type_T";
8484
}
8585

86-
char* token_to_string(const token_T* token) {
86+
hb_string_T token_to_string(const token_T* token) {
8787
const char* type_string = token_type_to_string(token->type);
8888
const char* template = "#<Herb::Token type=\"%s\" value=\"%.*s\" range=[%u, %u] start=(%u:%u) end=(%u:%u)>";
8989

@@ -112,7 +112,7 @@ char* token_to_string(const token_T* token) {
112112

113113
free(escaped.data);
114114

115-
return string;
115+
return hb_string(string);
116116
}
117117

118118
token_T* token_copy(token_T* token) {

0 commit comments

Comments
 (0)