mirror of https://github.com/mgba-emu/mgba.git
Add phony strndup implementation for when we do not have one
This commit is contained in:
parent
38762449ad
commit
3cd0b50bce
|
@ -1,5 +1,17 @@
|
||||||
#include "parser.h"
|
#include "parser.h"
|
||||||
|
|
||||||
|
static inline char* _strndup(const char* start, size_t len) {
|
||||||
|
#ifdef HAVE_STRNDUP
|
||||||
|
return strndup(start, len);
|
||||||
|
#else
|
||||||
|
// This is suboptimal, but anything recent should have strndup
|
||||||
|
char* out = malloc((len + 1) * sizeof(char));
|
||||||
|
strncpy(out, start, len);
|
||||||
|
out[len] = '\0';
|
||||||
|
return out;
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
static struct LexVector* _lexOperator(struct LexVector* lv, char operator) {
|
static struct LexVector* _lexOperator(struct LexVector* lv, char operator) {
|
||||||
struct LexVector* lvNext = malloc(sizeof(struct LexVector));
|
struct LexVector* lvNext = malloc(sizeof(struct LexVector));
|
||||||
lvNext->token.type = TOKEN_OPERATOR_TYPE;
|
lvNext->token.type = TOKEN_OPERATOR_TYPE;
|
||||||
|
@ -95,13 +107,13 @@ size_t lexExpression(struct LexVector* lv, const char* string, size_t length) {
|
||||||
case '*':
|
case '*':
|
||||||
case '/':
|
case '/':
|
||||||
lv->token.type = TOKEN_IDENTIFIER_TYPE;
|
lv->token.type = TOKEN_IDENTIFIER_TYPE;
|
||||||
lv->token.identifierValue = strndup(tokenStart, string - tokenStart - 1);
|
lv->token.identifierValue = _strndup(tokenStart, string - tokenStart - 1);
|
||||||
lv = _lexOperator(lv, token);
|
lv = _lexOperator(lv, token);
|
||||||
state = LEX_ROOT;
|
state = LEX_ROOT;
|
||||||
break;
|
break;
|
||||||
case ')':
|
case ')':
|
||||||
lv->token.type = TOKEN_IDENTIFIER_TYPE;
|
lv->token.type = TOKEN_IDENTIFIER_TYPE;
|
||||||
lv->token.identifierValue = strndup(tokenStart, string - tokenStart - 1);
|
lv->token.identifierValue = _strndup(tokenStart, string - tokenStart - 1);
|
||||||
state = LEX_EXPECT_OPERATOR;
|
state = LEX_EXPECT_OPERATOR;
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
|
@ -240,7 +252,7 @@ size_t lexExpression(struct LexVector* lv, const char* string, size_t length) {
|
||||||
break;
|
break;
|
||||||
case LEX_EXPECT_IDENTIFIER:
|
case LEX_EXPECT_IDENTIFIER:
|
||||||
lv->token.type = TOKEN_IDENTIFIER_TYPE;
|
lv->token.type = TOKEN_IDENTIFIER_TYPE;
|
||||||
lv->token.identifierValue = strndup(tokenStart, string - tokenStart);
|
lv->token.identifierValue = _strndup(tokenStart, string - tokenStart);
|
||||||
break;
|
break;
|
||||||
case LEX_EXPECT_OPERATOR:
|
case LEX_EXPECT_OPERATOR:
|
||||||
lvNext = malloc(sizeof(struct LexVector));
|
lvNext = malloc(sizeof(struct LexVector));
|
||||||
|
|
Loading…
Reference in New Issue