summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorS-H-GAMELINKS <gamelinks007@gmail.com>2022-11-15 13:21:45 +0900
committerNobuyoshi Nakada <nobu@ruby-lang.org>2022-12-02 01:31:27 +0900
commit1a64d45c67feed7355d6b53746801acb319394a4 (patch)
tree7de928b38b2fc079d91120f22d89b92ca6a3e110
parent7bc63d6f430422535f4dd460a98f3a90578e524c (diff)
Introduce encoding check macro
Notes
Notes: Merged: https://github.com/ruby/ruby/pull/6700
-rw-r--r--common.mk3
-rw-r--r--ext/ripper/depend1
-rw-r--r--internal/encoding.h3
-rw-r--r--io.c4
-rw-r--r--parse.y7
-rw-r--r--re.c3
-rw-r--r--strftime.c7
-rw-r--r--string.c2
8 files changed, 20 insertions, 10 deletions
diff --git a/common.mk b/common.mk
index 6d2ed3a0c7..15ccc9fdd6 100644
--- a/common.mk
+++ b/common.mk
@@ -10817,6 +10817,7 @@ parse.$(OBJEXT): $(top_srcdir)/internal/bits.h
parse.$(OBJEXT): $(top_srcdir)/internal/compile.h
parse.$(OBJEXT): $(top_srcdir)/internal/compilers.h
parse.$(OBJEXT): $(top_srcdir)/internal/complex.h
+parse.$(OBJEXT): $(top_srcdir)/internal/encoding.h
parse.$(OBJEXT): $(top_srcdir)/internal/error.h
parse.$(OBJEXT): $(top_srcdir)/internal/fixnum.h
parse.$(OBJEXT): $(top_srcdir)/internal/gc.h
@@ -12231,6 +12232,7 @@ re.$(OBJEXT): $(top_srcdir)/internal/array.h
re.$(OBJEXT): $(top_srcdir)/internal/bits.h
re.$(OBJEXT): $(top_srcdir)/internal/class.h
re.$(OBJEXT): $(top_srcdir)/internal/compilers.h
+re.$(OBJEXT): $(top_srcdir)/internal/encoding.h
re.$(OBJEXT): $(top_srcdir)/internal/gc.h
re.$(OBJEXT): $(top_srcdir)/internal/hash.h
re.$(OBJEXT): $(top_srcdir)/internal/imemo.h
@@ -14765,6 +14767,7 @@ st.$(OBJEXT): {$(VPATH)}st.h
st.$(OBJEXT): {$(VPATH)}subst.h
strftime.$(OBJEXT): $(hdrdir)/ruby/ruby.h
strftime.$(OBJEXT): $(top_srcdir)/internal/compilers.h
+strftime.$(OBJEXT): $(top_srcdir)/internal/encoding.h
strftime.$(OBJEXT): $(top_srcdir)/internal/serial.h
strftime.$(OBJEXT): $(top_srcdir)/internal/static_assert.h
strftime.$(OBJEXT): $(top_srcdir)/internal/string.h
diff --git a/ext/ripper/depend b/ext/ripper/depend
index 85520b032e..856283e177 100644
--- a/ext/ripper/depend
+++ b/ext/ripper/depend
@@ -231,6 +231,7 @@ ripper.o: $(top_srcdir)/internal/bits.h
ripper.o: $(top_srcdir)/internal/compile.h
ripper.o: $(top_srcdir)/internal/compilers.h
ripper.o: $(top_srcdir)/internal/complex.h
+ripper.o: $(top_srcdir)/internal/encoding.h
ripper.o: $(top_srcdir)/internal/error.h
ripper.o: $(top_srcdir)/internal/fixnum.h
ripper.o: $(top_srcdir)/internal/gc.h
diff --git a/internal/encoding.h b/internal/encoding.h
index 853426a58d..cba9230950 100644
--- a/internal/encoding.h
+++ b/internal/encoding.h
@@ -12,6 +12,9 @@
#include "ruby/encoding.h" /* for rb_encoding */
#define rb_enc_autoload_p(enc) (!rb_enc_mbmaxlen(enc))
+#define rb_is_usascii_enc(enc) (enc) == rb_usascii_encoding()
+#define rb_is_ascii8bit_enc(enc) (enc) == rb_ascii8bit_encoding()
+#define rb_is_locale_enc(enc) (enc) == rb_locale_encoding()
/* encoding.c */
ID rb_id_encoding(void);
diff --git a/io.c b/io.c
index e4caaaca81..3b40950aa0 100644
--- a/io.c
+++ b/io.c
@@ -1646,7 +1646,7 @@ make_writeconv(rb_io_t *fptr)
ecflags = fptr->encs.ecflags & ~ECONV_NEWLINE_DECORATOR_READ_MASK;
ecopts = fptr->encs.ecopts;
- if (!fptr->encs.enc || (fptr->encs.enc == rb_ascii8bit_encoding() && !fptr->encs.enc2)) {
+ if (!fptr->encs.enc || (rb_is_ascii8bit_enc(fptr->encs.enc) && !fptr->encs.enc2)) {
/* no encoding conversion */
fptr->writeconv_pre_ecflags = 0;
fptr->writeconv_pre_ecopts = Qnil;
@@ -6514,7 +6514,7 @@ rb_io_ext_int_to_encs(rb_encoding *ext, rb_encoding *intern, rb_encoding **enc,
ext = rb_default_external_encoding();
default_ext = 1;
}
- if (ext == rb_ascii8bit_encoding()) {
+ if (rb_is_ascii8bit_enc(ext)) {
/* If external is ASCII-8BIT, no transcoding */
intern = NULL;
}
diff --git a/parse.y b/parse.y
index c76fefcf6c..344f588e57 100644
--- a/parse.y
+++ b/parse.y
@@ -34,6 +34,7 @@ struct lex_context;
#include "internal/compile.h"
#include "internal/compilers.h"
#include "internal/complex.h"
+#include "internal/encoding.h"
#include "internal/error.h"
#include "internal/hash.h"
#include "internal/imemo.h"
@@ -6984,7 +6985,7 @@ parser_str_new(const char *ptr, long len, rb_encoding *enc, int func, rb_encodin
if (!(func & STR_FUNC_REGEXP) && rb_enc_asciicompat(enc)) {
if (is_ascii_string(str)) {
}
- else if (enc0 == rb_usascii_encoding() && enc != rb_utf8_encoding()) {
+ else if (rb_is_usascii_enc(enc0) && enc != rb_utf8_encoding()) {
rb_enc_associate(str, rb_ascii8bit_encoding());
}
}
@@ -8345,7 +8346,7 @@ here_document(struct parser_params *p, rb_strterm_heredoc_t *here)
int cr = ENC_CODERANGE_UNKNOWN;
rb_str_coderange_scan_restartable(p->lex.ptok, p->lex.pcur, enc, &cr);
if (cr != ENC_CODERANGE_7BIT &&
- p->enc == rb_usascii_encoding() &&
+ rb_is_usascii_enc(p->enc) &&
enc != rb_utf8_encoding()) {
enc = rb_ascii8bit_encoding();
}
@@ -13460,7 +13461,7 @@ rb_reg_fragment_setenc(struct parser_params* p, VALUE str, int options)
}
rb_enc_associate(str, rb_ascii8bit_encoding());
}
- else if (p->enc == rb_usascii_encoding()) {
+ else if (rb_is_usascii_enc(p->enc)) {
if (!is_ascii_string(str)) {
/* raise in re.c */
rb_enc_associate(str, rb_usascii_encoding());
diff --git a/re.c b/re.c
index b1c9dcd340..f6993371e1 100644
--- a/re.c
+++ b/re.c
@@ -16,6 +16,7 @@
#include "encindex.h"
#include "hrtime.h"
#include "internal.h"
+#include "internal/encoding.h"
#include "internal/hash.h"
#include "internal/imemo.h"
#include "internal/re.h"
@@ -2866,7 +2867,7 @@ unescape_nonascii(const char *p, const char *end, rb_encoding *enc,
case 'C': /* \C-X, \C-\M-X */
case 'M': /* \M-X, \M-\C-X, \M-\cX */
p = p-2;
- if (enc == rb_usascii_encoding()) {
+ if (rb_is_usascii_enc(enc)) {
const char *pbeg = p;
int byte = read_escaped_byte(&p, end, err);
if (byte == -1) return -1;
diff --git a/strftime.c b/strftime.c
index a8a688608e..33e7d3fdb8 100644
--- a/strftime.c
+++ b/strftime.c
@@ -66,6 +66,7 @@
#include <math.h>
#include "internal.h"
+#include "internal/encoding.h"
#include "internal/string.h"
#include "internal/vm.h"
#include "ruby/encoding.h"
@@ -270,9 +271,9 @@ rb_strftime_with_timespec(VALUE ftime, const char *format, size_t format_len,
}
if (enc &&
- (enc == rb_usascii_encoding() ||
- enc == rb_ascii8bit_encoding() ||
- enc == rb_locale_encoding())) {
+ (rb_is_usascii_enc(enc) ||
+ rb_is_ascii8bit_enc(enc) ||
+ rb_is_locale_enc(enc))) {
enc = NULL;
}
diff --git a/string.c b/string.c
index 20d92896c0..f966384d18 100644
--- a/string.c
+++ b/string.c
@@ -1096,7 +1096,7 @@ rb_str_conv_enc_opts(VALUE str, rb_encoding *from, rb_encoding *to, int ecflags,
if (!from) from = rb_enc_get(str);
if (from == to) return str;
if ((rb_enc_asciicompat(to) && is_enc_ascii_string(str, from)) ||
- to == rb_ascii8bit_encoding()) {
+ rb_is_ascii8bit_enc(to)) {
if (STR_ENC_GET(str) != to) {
str = rb_str_dup(str);
rb_enc_associate(str, to);