summaryrefslogtreecommitdiff
path: root/complex.c
diff options
context:
space:
mode:
authorNobuyoshi Nakada <nobu@ruby-lang.org>2023-12-11 15:48:51 +0900
committerNobuyoshi Nakada <nobu@ruby-lang.org>2023-12-11 16:51:46 +0900
commit655aacc43a2ae263c9b71429232853d16df028d5 (patch)
tree76f2fbded370333d265ac169216ced9b247ac27c /complex.c
parent980255cb7bfc9ea1ed105ff641ec44b751deced6 (diff)
Use dedicated functions to check terminators
Diffstat (limited to 'complex.c')
-rw-r--r--complex.c34
1 files changed, 7 insertions, 27 deletions
diff --git a/complex.c b/complex.c
index 62c383b3f7..88b4e02177 100644
--- a/complex.c
+++ b/complex.c
@@ -24,6 +24,7 @@
#include "internal/numeric.h"
#include "internal/object.h"
#include "internal/rational.h"
+#include "internal/string.h"
#include "ruby_assert.h"
#define ZERO INT2FIX(0)
@@ -2101,23 +2102,14 @@ string_to_c_strict(VALUE self, int raise)
rb_must_asciicompat(self);
- s = RSTRING_PTR(self);
-
- if (!s || memchr(s, '\0', RSTRING_LEN(self))) {
- if (!raise) return Qnil;
- rb_raise(rb_eArgError, "string contains null byte");
+ if (raise) {
+ s = StringValueCStr(self);
}
-
- if (s && s[RSTRING_LEN(self)]) {
- rb_str_modify(self);
- s = RSTRING_PTR(self);
- s[RSTRING_LEN(self)] = '\0';
+ else if (!(s = rb_str_to_cstr(self))) {
+ return Qnil;
}
- if (!s)
- s = (char *)"";
-
- if (!parse_comp(s, 1, &num)) {
+ if (!parse_comp(s, TRUE, &num)) {
if (!raise) return Qnil;
rb_raise(rb_eArgError, "invalid value for convert(): %+"PRIsVALUE,
self);
@@ -2158,23 +2150,11 @@ string_to_c_strict(VALUE self, int raise)
static VALUE
string_to_c(VALUE self)
{
- char *s;
VALUE num;
rb_must_asciicompat(self);
- s = RSTRING_PTR(self);
-
- 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);
+ (void)parse_comp(rb_str_fill_terminator(self, 1), FALSE, &num);
return num;
}