Skip to content

Commit c9f5552

Browse files
committed
enc_mbclen_needed: The condition when exact mbclen is needed
1 parent 31cdf7a commit c9f5552

1 file changed

Lines changed: 24 additions & 18 deletions

File tree

file.c

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3622,6 +3622,8 @@ static const char file_alt_separator[] = {FILE_ALT_SEPARATOR, '\0'};
36223622
# define isADS(x) 0
36233623
#endif
36243624

3625+
#define enc_mbclen_needed(enc) (!rb_str_encindex_fastpath(rb_enc_to_index(enc)))
3626+
36253627
#define Next(p, e, mb_enc, enc) ((p) + ((mb_enc) ? rb_enc_mbclen((p), (e), (enc)) : 1))
36263628
#define Inc(p, e, mb_enc, enc) ((p) = Next((p), (e), (mb_enc), (enc)))
36273629

@@ -3709,7 +3711,7 @@ enc_path_next(const char *s, const char *e, bool mb_enc, rb_encoding *enc)
37093711
char *
37103712
rb_enc_path_next(const char *s, const char *e, rb_encoding *enc)
37113713
{
3712-
return enc_path_next(s, e, true, enc);
3714+
return enc_path_next(s, e, enc_mbclen_needed(enc), enc);
37133715
}
37143716

37153717
#if defined(DOSISH_UNC) || defined(DOSISH_DRIVE_LETTER)
@@ -3743,15 +3745,15 @@ enc_path_skip_prefix(const char *path, const char *end, bool mb_enc, rb_encoding
37433745
char *
37443746
rb_enc_path_skip_prefix(const char *path, const char *end, rb_encoding *enc)
37453747
{
3746-
return enc_path_skip_prefix(path, end, true, enc);
3748+
return enc_path_skip_prefix(path, end, enc_mbclen_needed(enc), enc);
37473749
}
37483750

37493751
static inline char *
37503752
skipprefixroot(const char *path, const char *end, rb_encoding *enc)
37513753
{
37523754
#if defined(DOSISH_UNC) || defined(DOSISH_DRIVE_LETTER)
3753-
char *p = skipprefix(path, end, true, enc);
3754-
while (isdirsep(*p)) p++;
3755+
char *p = skipprefix(path, end, enc_mbclen_needed(enc), enc);
3756+
while (p < end && isdirsep(*p)) p++;
37553757
return p;
37563758
#else
37573759
return skiproot(path, end);
@@ -3779,7 +3781,7 @@ enc_path_last_separator(const char *path, const char *end, bool mb_enc, rb_encod
37793781
char *
37803782
rb_enc_path_last_separator(const char *path, const char *end, rb_encoding *enc)
37813783
{
3782-
return enc_path_last_separator(path, end, true, enc);
3784+
return enc_path_last_separator(path, end, enc_mbclen_needed(enc), enc);
37833785
}
37843786

37853787
static inline char *
@@ -3827,7 +3829,7 @@ char *
38273829
rb_enc_path_end(const char *path, const char *end, rb_encoding *enc)
38283830
{
38293831
if (path < end && isdirsep(*path)) path++;
3830-
return chompdirsep(path, end, true, enc);
3832+
return chompdirsep(path, end, enc_mbclen_needed(enc), enc);
38313833
}
38323834

38333835
static rb_encoding *
@@ -3848,6 +3850,7 @@ fs_enc_check(VALUE path1, VALUE path2)
38483850
static char *
38493851
ntfs_tail(const char *path, const char *end, rb_encoding *enc)
38503852
{
3853+
bool mb_enc = enc_mbclen_needed(enc);
38513854
while (path < end && *path == '.') path++;
38523855
while (path < end && !isADS(*path)) {
38533856
if (istrailinggarbage(*path)) {
@@ -3862,7 +3865,7 @@ ntfs_tail(const char *path, const char *end, rb_encoding *enc)
38623865
if (isADS(*path)) path++;
38633866
}
38643867
else {
3865-
Inc(path, end, true, enc);
3868+
Inc(path, end, mb_enc, enc);
38663869
}
38673870
}
38683871
return (char *)path;
@@ -3924,7 +3927,8 @@ copy_home_path(VALUE result, const char *dir)
39243927
rb_enc_associate_index(result, encidx);
39253928
#if defined DOSISH || defined __CYGWIN__
39263929
enc = rb_enc_from_index(encidx);
3927-
for (bend = (p = buf) + dirlen; p < bend; Inc(p, bend, true, enc)) {
3930+
bool mb_enc = enc_mbclen_needed(enc);
3931+
for (bend = (p = buf) + dirlen; p < bend; Inc(p, bend, mb_enc, enc)) {
39283932
if (*p == '\\') {
39293933
*p = '/';
39303934
}
@@ -4079,9 +4083,9 @@ rb_file_expand_path_internal(VALUE fname, VALUE dname, int abs_mode, int long_na
40794083
s = StringValuePtr(fname);
40804084
fend = s + RSTRING_LEN(fname);
40814085
enc = rb_str_enc_get(fname);
4082-
bool mb_enc = !rb_str_encindex_fastpath(rb_enc_to_index(enc));
4086+
bool mb_enc = enc_mbclen_needed(enc);
40834087
if (!mb_enc && RTEST(dname)) {
4084-
mb_enc = !rb_str_encindex_fastpath(rb_enc_to_index(rb_str_enc_get(dname)));
4088+
mb_enc = enc_mbclen_needed(rb_str_enc_get(dname));
40854089
}
40864090

40874091
BUFINIT();
@@ -4152,7 +4156,7 @@ rb_file_expand_path_internal(VALUE fname, VALUE dname, int abs_mode, int long_na
41524156
rb_enc_associate(result, enc = fs_enc_check(result, fname));
41534157
p = pend;
41544158
}
4155-
p = chompdirsep(skiproot(buf, p), p, true, enc);
4159+
p = chompdirsep(skiproot(buf, p), p, mb_enc, enc);
41564160
s += 2;
41574161
}
41584162
}
@@ -4173,7 +4177,7 @@ rb_file_expand_path_internal(VALUE fname, VALUE dname, int abs_mode, int long_na
41734177
if (s < fend && isdirsep(*s)) {
41744178
/* specified full path, but not drive letter nor UNC */
41754179
/* we need to get the drive letter or UNC share name */
4176-
p = skipprefix(buf, p, true, enc);
4180+
p = skipprefix(buf, p, mb_enc, enc);
41774181
}
41784182
else
41794183
#endif /* defined DOSISH || defined __CYGWIN__ */
@@ -4579,9 +4583,10 @@ realpath_rec(long *prefixlenp, VALUE *resolvedp, const char *unresolved, VALUE f
45794583
}
45804584
else if (testnamelen == 2 && testname[0] == '.' && testname[1] == '.') {
45814585
if (*prefixlenp < RSTRING_LEN(*resolvedp)) {
4586+
bool mb_enc = enc_mbclen_needed(enc);
45824587
const char *resolved_str = RSTRING_PTR(*resolvedp);
45834588
const char *resolved_names = resolved_str + *prefixlenp;
4584-
const char *lastsep = strrdirsep(resolved_names, resolved_str + RSTRING_LEN(*resolvedp), true, enc);
4589+
const char *lastsep = strrdirsep(resolved_names, resolved_str + RSTRING_LEN(*resolvedp), mb_enc, enc);
45854590
long len = lastsep ? lastsep - resolved_names : 0;
45864591
rb_str_resize(*resolvedp, *prefixlenp + len);
45874592
}
@@ -4721,7 +4726,8 @@ rb_check_realpath_emulate(VALUE basedir, VALUE path, rb_encoding *origenc, enum
47214726
root_found:
47224727
RSTRING_GETMEM(resolved, prefixptr, prefixlen);
47234728
pend = prefixptr + prefixlen;
4724-
ptr = chompdirsep(prefixptr, pend, true, enc);
4729+
bool mb_enc = enc_mbclen_needed(enc);
4730+
ptr = chompdirsep(prefixptr, pend, mb_enc, enc);
47254731
if (ptr < pend) {
47264732
prefixlen = ++ptr - prefixptr;
47274733
rb_str_set_len(resolved, prefixlen);
@@ -4731,7 +4737,7 @@ rb_check_realpath_emulate(VALUE basedir, VALUE path, rb_encoding *origenc, enum
47314737
if (*prefixptr == FILE_ALT_SEPARATOR) {
47324738
*prefixptr = '/';
47334739
}
4734-
Inc(prefixptr, pend, true, enc);
4740+
Inc(prefixptr, pend, mb_enc, enc);
47354741
}
47364742
#endif
47374743

@@ -5059,7 +5065,7 @@ enc_find_basename(const char *name, long *baselen, long *alllen, bool mb_enc, rb
50595065
const char *
50605066
ruby_enc_find_basename(const char *name, long *baselen, long *alllen, rb_encoding *enc)
50615067
{
5062-
return enc_find_basename(name, baselen, alllen, true, enc);
5068+
return enc_find_basename(name, baselen, alllen, enc_mbclen_needed(enc), enc);
50635069
}
50645070

50655071
/*
@@ -5124,7 +5130,7 @@ rb_file_s_basename(int argc, VALUE *argv, VALUE _)
51245130
return rb_enc_str_new(0, 0, enc);
51255131
}
51265132

5127-
bool mb_enc = !rb_str_encindex_fastpath(rb_enc_to_index(enc));
5133+
bool mb_enc = enc_mbclen_needed(enc);
51285134
p = enc_find_basename(name, &f, &n, mb_enc, enc);
51295135
if (n >= 0) {
51305136
if (!fp) {
@@ -5303,7 +5309,7 @@ enc_find_extname(const char *name, long *len, bool mb_enc, rb_encoding *enc)
53035309
const char *
53045310
ruby_enc_find_extname(const char *name, long *len, rb_encoding *enc)
53055311
{
5306-
return enc_find_extname(name, len, true, enc);
5312+
return enc_find_extname(name, len, enc_mbclen_needed(enc), enc);
53075313
}
53085314

53095315
/*

0 commit comments

Comments
 (0)