Use dedicated functions to check terminators

This commit is contained in:
Nobuyoshi Nakada 2023-12-11 15:48:51 +09:00
parent 980255cb7b
commit 655aacc43a
2 changed files with 9 additions and 28 deletions

View File

@ -3460,6 +3460,7 @@ complex.$(OBJEXT): $(top_srcdir)/internal/object.h
complex.$(OBJEXT): $(top_srcdir)/internal/rational.h complex.$(OBJEXT): $(top_srcdir)/internal/rational.h
complex.$(OBJEXT): $(top_srcdir)/internal/serial.h complex.$(OBJEXT): $(top_srcdir)/internal/serial.h
complex.$(OBJEXT): $(top_srcdir)/internal/static_assert.h complex.$(OBJEXT): $(top_srcdir)/internal/static_assert.h
complex.$(OBJEXT): $(top_srcdir)/internal/string.h
complex.$(OBJEXT): $(top_srcdir)/internal/variable.h complex.$(OBJEXT): $(top_srcdir)/internal/variable.h
complex.$(OBJEXT): $(top_srcdir)/internal/vm.h complex.$(OBJEXT): $(top_srcdir)/internal/vm.h
complex.$(OBJEXT): $(top_srcdir)/internal/warnings.h complex.$(OBJEXT): $(top_srcdir)/internal/warnings.h

View File

@ -24,6 +24,7 @@
#include "internal/numeric.h" #include "internal/numeric.h"
#include "internal/object.h" #include "internal/object.h"
#include "internal/rational.h" #include "internal/rational.h"
#include "internal/string.h"
#include "ruby_assert.h" #include "ruby_assert.h"
#define ZERO INT2FIX(0) #define ZERO INT2FIX(0)
@ -2101,23 +2102,14 @@ string_to_c_strict(VALUE self, int raise)
rb_must_asciicompat(self); rb_must_asciicompat(self);
s = RSTRING_PTR(self); if (raise) {
s = StringValueCStr(self);
if (!s || memchr(s, '\0', RSTRING_LEN(self))) { }
if (!raise) return Qnil; else if (!(s = rb_str_to_cstr(self))) {
rb_raise(rb_eArgError, "string contains null byte"); return Qnil;
} }
if (s && s[RSTRING_LEN(self)]) { if (!parse_comp(s, TRUE, &num)) {
rb_str_modify(self);
s = RSTRING_PTR(self);
s[RSTRING_LEN(self)] = '\0';
}
if (!s)
s = (char *)"";
if (!parse_comp(s, 1, &num)) {
if (!raise) return Qnil; if (!raise) return Qnil;
rb_raise(rb_eArgError, "invalid value for convert(): %+"PRIsVALUE, rb_raise(rb_eArgError, "invalid value for convert(): %+"PRIsVALUE,
self); self);
@ -2158,23 +2150,11 @@ string_to_c_strict(VALUE self, int raise)
static VALUE static VALUE
string_to_c(VALUE self) string_to_c(VALUE self)
{ {
char *s;
VALUE num; VALUE num;
rb_must_asciicompat(self); rb_must_asciicompat(self);
s = RSTRING_PTR(self); (void)parse_comp(rb_str_fill_terminator(self, 1), FALSE, &num);
if (s && s[RSTRING_LEN(self)]) {
rb_str_modify(self);
s = RSTRING_PTR(self);
s[RSTRING_LEN(self)] = '\0';
}
if (!s)
s = (char *)"";
(void)parse_comp(s, 0, &num);
return num; return num;
} }