Remove on RSTRING_END dependency from parser

This commit is contained in:
Nobuyoshi Nakada 2024-09-28 00:20:09 +09:00 committed by Nobuyoshi Nakada
parent d1324170b6
commit 7e19904c88
Notes: git 2024-09-27 16:59:52 +00:00
4 changed files with 10 additions and 13 deletions

19
parse.y
View File

@ -709,10 +709,11 @@ after_pop_stack(int len, struct parser_params *p)
#define VALID_SYMNAME_P(s, l, enc, type) (rb_enc_symname_type(s, l, enc, (1U<<(type))) == (int)(type)) #define VALID_SYMNAME_P(s, l, enc, type) (rb_enc_symname_type(s, l, enc, (1U<<(type))) == (int)(type))
#ifndef RIPPER #ifndef RIPPER
static inline bool static inline int
end_with_newline_p(struct parser_params *p, VALUE str) char_at_end(struct parser_params *p, VALUE str, int when_empty)
{ {
return RSTRING_LEN(str) > 0 && RSTRING_END(str)[-1] == '\n'; long len = RSTRING_LEN(str);
return len > 0 ? (unsigned char)RSTRING_PTR(str)[len-1] : when_empty;
} }
#endif #endif
@ -2002,10 +2003,10 @@ parser_memhash(const void *ptr, long len)
((ptrvar) = str->ptr, \ ((ptrvar) = str->ptr, \
(lenvar) = str->len) (lenvar) = str->len)
static inline bool static inline int
parser_string_end_with_newline_p(struct parser_params *p, rb_parser_string_t *str) parser_string_char_at_end(struct parser_params *p, rb_parser_string_t *str, int when_empty)
{ {
return PARSER_STRING_LEN(str) > 0 && PARSER_STRING_END(str)[-1] == '\n'; return PARSER_STRING_LEN(str) > 0 ? (unsigned char)PARSER_STRING_END(str)[-1] : when_empty;
} }
static rb_parser_string_t * static rb_parser_string_t *
@ -7354,7 +7355,7 @@ ruby_show_error_line(struct parser_params *p, VALUE errbuf, const YYLTYPE *yyllo
} }
if (RTEST(errbuf)) { if (RTEST(errbuf)) {
mesg = rb_attr_get(errbuf, idMesg); mesg = rb_attr_get(errbuf, idMesg);
if (RSTRING_LEN(mesg) > 0 && *(RSTRING_END(mesg)-1) != '\n') if (char_at_end(p, mesg, '\n') != '\n')
rb_str_cat_cstr(mesg, "\n"); rb_str_cat_cstr(mesg, "\n");
} }
else { else {
@ -7750,7 +7751,7 @@ parser_add_delayed_token(struct parser_params *p, const char *tok, const char *e
if (tok < end) { if (tok < end) {
if (has_delayed_token(p)) { if (has_delayed_token(p)) {
bool next_line = parser_string_end_with_newline_p(p, p->delayed.token); bool next_line = parser_string_char_at_end(p, p->delayed.token, 0) == '\n';
int end_line = (next_line ? 1 : 0) + p->delayed.end_line; int end_line = (next_line ? 1 : 0) + p->delayed.end_line;
int end_col = (next_line ? 0 : p->delayed.end_col); int end_col = (next_line ? 0 : p->delayed.end_col);
if (end_line != p->ruby_sourceline || end_col != tok - p->lex.pbeg) { if (end_line != p->ruby_sourceline || end_col != tok - p->lex.pbeg) {
@ -15961,7 +15962,7 @@ rb_parser_printf(struct parser_params *p, const char *fmt, ...)
va_start(ap, fmt); va_start(ap, fmt);
rb_str_vcatf(mesg, fmt, ap); rb_str_vcatf(mesg, fmt, ap);
va_end(ap); va_end(ap);
if (end_with_newline_p(p, mesg)) { if (char_at_end(p, mesg, 0) == '\n') {
rb_io_write(p->debug_output, mesg); rb_io_write(p->debug_output, mesg);
p->debug_buffer = Qnil; p->debug_buffer = Qnil;
} }

View File

@ -373,7 +373,6 @@ static const rb_parser_config_t rb_global_parser_config = {
.str_vcatf = rb_str_vcatf, .str_vcatf = rb_str_vcatf,
.rb_sprintf = rb_sprintf, .rb_sprintf = rb_sprintf,
.rstring_ptr = RSTRING_PTR, .rstring_ptr = RSTRING_PTR,
.rstring_end = RSTRING_END,
.rstring_len = RSTRING_LEN, .rstring_len = RSTRING_LEN,
.obj_as_string = rb_obj_as_string, .obj_as_string = rb_obj_as_string,

View File

@ -1227,7 +1227,6 @@ typedef struct rb_parser_config_struct {
RBIMPL_ATTR_FORMAT(RBIMPL_PRINTF_FORMAT, 1, 2) RBIMPL_ATTR_FORMAT(RBIMPL_PRINTF_FORMAT, 1, 2)
VALUE (*rb_sprintf)(const char *format, ...); VALUE (*rb_sprintf)(const char *format, ...);
char *(*rstring_ptr)(VALUE str); char *(*rstring_ptr)(VALUE str);
char *(*rstring_end)(VALUE str);
long (*rstring_len)(VALUE str); long (*rstring_len)(VALUE str);
VALUE (*obj_as_string)(VALUE); VALUE (*obj_as_string)(VALUE);

View File

@ -126,8 +126,6 @@
#define rb_sprintf p->config->rb_sprintf #define rb_sprintf p->config->rb_sprintf
#undef RSTRING_PTR #undef RSTRING_PTR
#define RSTRING_PTR p->config->rstring_ptr #define RSTRING_PTR p->config->rstring_ptr
#undef RSTRING_END
#define RSTRING_END p->config->rstring_end
#undef RSTRING_LEN #undef RSTRING_LEN
#define RSTRING_LEN p->config->rstring_len #define RSTRING_LEN p->config->rstring_len
#define rb_obj_as_string p->config->obj_as_string #define rb_obj_as_string p->config->obj_as_string