diff options
author | Raghuram Subramani <raghus2247@gmail.com> | 2025-06-12 17:38:21 +0530 |
---|---|---|
committer | Raghuram Subramani <raghus2247@gmail.com> | 2025-06-12 17:38:21 +0530 |
commit | 68ae30b661af90965832a0c025af3efef59fda6a (patch) | |
tree | 3840bfc92a8fcfacbfa107f39c01ac0595b7ddcd | |
parent | 4d58194414dad9a6d7d23b488892f0d9224d46ca (diff) |
token: asprintf instead of complicated length calculations
-rw-r--r-- | src/token.cc | 19 |
1 files changed, 3 insertions, 16 deletions
diff --git a/src/token.cc b/src/token.cc index 6e6a3a8..ff59123 100644 --- a/src/token.cc +++ b/src/token.cc @@ -15,23 +15,10 @@ Token::init(token_type_e type, char *lexeme, unsigned int line) char * Token::to_string(void) { - unsigned int line_length = snprintf(NULL, 0, "%ul", m_line) - 1; - unsigned int token_type_length = strlen(TOKEN_STRING[m_type]); + if (m_string == NULL) + asprintf(&m_string, "%d: %s - %s", m_line, TOKEN_STRING[m_type], m_lexeme); - unsigned int final_size - = line_length + 2 + token_type_length + 3 + strlen(m_lexeme) + 1; - - char *result = (char *) calloc(1, final_size); - snprintf(result, - final_size, - "%d: %s - %s", - m_line, - TOKEN_STRING[m_type], - m_lexeme); - - m_string = result; - - return result; + return m_string; } void |