Skip to content

Commit 55e5378

Browse files
committed
locale.c: Minor cleanup
This replaces an expression with what I think is an easier to understand macro, and eliminates a couple of temporary variables that just cluttered things up.
1 parent 2fcc0ca commit 55e5378

File tree

1 file changed

+15
-16
lines changed

1 file changed

+15
-16
lines changed

locale.c

+15-16
Original file line numberDiff line numberDiff line change
@@ -1413,14 +1413,16 @@ Perl__mem_collxfrm(pTHX_ const char *input_string,
14131413
/* _mem_collxfrm() is a bit like strxfrm() but with two important
14141414
* differences. First, it handles embedded NULs. Second, it allocates a bit
14151415
* more memory than needed for the transformed data itself. The real
1416-
* transformed data begins at offset sizeof(collationix). *xlen is set to
1416+
* transformed data begins at offset COLLXFRM_HDR_LEN. *xlen is set to
14171417
* the length of that, and doesn't include the collation index size.
14181418
* Please see sv_collxfrm() to see how this is used. */
14191419

1420+
#define COLLXFRM_HDR_LEN sizeof(PL_collation_ix)
1421+
14201422
char * s = (char *) input_string;
14211423
STRLEN s_strlen = strlen(input_string);
14221424
char *xbuf = NULL;
1423-
STRLEN xAlloc, xout; /* xalloc is a reserved word in VC */
1425+
STRLEN xAlloc; /* xalloc is a reserved word in VC */
14241426
bool first_time = TRUE; /* Cleared after first loop iteration */
14251427

14261428
PERL_ARGS_ASSERT__MEM_COLLXFRM;
@@ -1495,16 +1497,16 @@ Perl__mem_collxfrm(pTHX_ const char *input_string,
14951497
/* If something went wrong (which it shouldn't), just
14961498
* ignore this code point */
14971499
if ( x_len == 0
1498-
|| strlen(x + sizeof(PL_collation_ix)) < x_len)
1500+
|| strlen(x + COLLXFRM_HDR_LEN) < x_len)
14991501
{
15001502
continue;
15011503
}
15021504

15031505
/* If this character's transformation is lower than
15041506
* the current lowest, this one becomes the lowest */
15051507
if ( cur_min_x == NULL
1506-
|| strLT(x + sizeof(PL_collation_ix),
1507-
cur_min_x + sizeof(PL_collation_ix)))
1508+
|| strLT(x + COLLXFRM_HDR_LEN,
1509+
cur_min_x + COLLXFRM_HDR_LEN))
15081510
{
15091511
strcpy(PL_strxfrm_min_char, cur_source);
15101512
cur_min_x = x;
@@ -1629,8 +1631,8 @@ Perl__mem_collxfrm(pTHX_ const char *input_string,
16291631
/* If this character's transformation is higher than
16301632
* the current highest, this one becomes the highest */
16311633
if ( cur_max_x == NULL
1632-
|| strGT(x + sizeof(PL_collation_ix),
1633-
cur_max_x + sizeof(PL_collation_ix)))
1634+
|| strGT(x + COLLXFRM_HDR_LEN,
1635+
cur_max_x + COLLXFRM_HDR_LEN))
16341636
{
16351637
PL_strxfrm_max_cp = j;
16361638
cur_max_x = x;
@@ -1684,7 +1686,7 @@ Perl__mem_collxfrm(pTHX_ const char *input_string,
16841686
/* The first element in the output is the collation id, used by
16851687
* sv_collxfrm(); then comes the space for the transformed string. The
16861688
* equation should give us a good estimate as to how much is needed */
1687-
xAlloc = sizeof(PL_collation_ix)
1689+
xAlloc = COLLXFRM_HDR_LEN
16881690
+ PL_collxfrm_base
16891691
+ (PL_collxfrm_mult * ((utf8)
16901692
? utf8_length((U8 *) s, (U8 *) s + len)
@@ -1695,30 +1697,28 @@ Perl__mem_collxfrm(pTHX_ const char *input_string,
16951697

16961698
/* Store the collation id */
16971699
*(U32*)xbuf = PL_collation_ix;
1698-
xout = sizeof(PL_collation_ix);
16991700

17001701
/* Then the transformation of the input. We loop until successful, or we
17011702
* give up */
17021703
for (;;) {
1703-
STRLEN xused = strxfrm(xbuf + xout, s, xAlloc - xout);
1704+
*xlen = strxfrm(xbuf + COLLXFRM_HDR_LEN, s, xAlloc - COLLXFRM_HDR_LEN);
17041705

17051706
/* If the transformed string occupies less space than we told strxfrm()
17061707
* was available, it means it successfully transformed the whole
17071708
* string. */
1708-
if (xused < xAlloc - xout) {
1709-
xout += xused;
1709+
if (*xlen < xAlloc - COLLXFRM_HDR_LEN) {
17101710
break;
17111711
}
17121712

1713-
if (UNLIKELY(xused >= PERL_INT_MAX))
1713+
if (UNLIKELY(*xlen >= PERL_INT_MAX))
17141714
goto bad;
17151715

17161716
/* A well-behaved strxfrm() returns exactly how much space it needs
17171717
* (not including the trailing NUL) when it fails due to not enough
17181718
* space being provided. Assume that this is the case unless it's been
17191719
* proven otherwise */
17201720
if (LIKELY(PL_strxfrm_is_behaved) && first_time) {
1721-
xAlloc = xused + sizeof(PL_collation_ix) + 1;
1721+
xAlloc = *xlen + COLLXFRM_HDR_LEN + 1;
17221722
}
17231723
else { /* Here, either:
17241724
* 1) The strxfrm() has previously shown bad behavior; or
@@ -1742,10 +1742,9 @@ Perl__mem_collxfrm(pTHX_ const char *input_string,
17421742
first_time = FALSE;
17431743
}
17441744

1745-
*xlen = xout - sizeof(PL_collation_ix);
17461745

17471746
/* Free up unneeded space; retain ehough for trailing NUL */
1748-
Renew(xbuf, xout + 1, char);
1747+
Renew(xbuf, COLLXFRM_HDR_LEN + *xlen + 1, char);
17491748

17501749
if (s != input_string) {
17511750
Safefree(s);

0 commit comments

Comments
 (0)