Skip to content

Commit 6a18255

Browse files
ParadoxV5vuvova
authored andcommitted
Rename my_snprintf’s %uE to %iE
… and delete `%uU` (just use `%d` for that) The follow-up #3360 discovered `%M` usages that suggest that it was designed for `errno` and similar **signed** `int` (“errno_t”) variables. Besides convenience, if the old `%M` read a `signed int`, so should the new version to maintain compatibility. I only added `%iE` (no `%dE`) to keep the new suffix mechanics away from the popular `%d`. Beïng synonyms (originally), this decision on preserving `%d` also saves the need for `%iI`/`%dD`.
1 parent 8911774 commit 6a18255

File tree

3 files changed

+34
-42
lines changed

3 files changed

+34
-42
lines changed

include/mysql/service_my_snprintf.h

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -67,21 +67,22 @@
6767
<precision> bytes without terminating on any '\0's in the sequence.
6868
The default <precision> when it's unspecified is not defined.
6969
70-
Format 'uE'
71-
treats the argument as an errno number. It prints this number, a space,
72-
then its corresponding error message in double quotes. In other words:
73-
printf("%uE", n) === printf("%d \"%sT\"", n, strerror(n))
74-
7570
Format 'sT'
7671
replaces the end of the printed string with "..." if it was truncated.
7772
78-
Format 'sS' and 'uU'
79-
are synonyms of 's' and 'u' respectively. They are escapes that avoid
73+
Format 'sS'
74+
is a synonym for 's'. It's an escape that avoid
8075
consuming the following plain char as one of the above extension suffixes.
81-
Example: "Data size: %uUEiB"
76+
Example: "Data Class: %sSType"
77+
78+
Format 'iE'
79+
treats the argument as an errno number. It prints this number, a space,
80+
then its corresponding error message in double quotes. In other words:
81+
printf("%iE", n) === printf("%i \"%sT\"", n, strerror(n))
82+
Format 'dE' has no effect. Therefore, to escape '%iE', use '%dE' instead.
8283
8384
Unrecognized and multiple suffixes are not parsed;
84-
for example, both "%sTQ" and "%uQ" will print a literal 'Q'.
85+
for example, both "%sTQ" and "%iQ" will suffix with a literal 'Q'.
8586
*/
8687

8788
#ifdef __cplusplus

strings/my_vsnprintf.c

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -586,16 +586,12 @@ static char *process_args(CHARSET_INFO *cs, char *to, char *end,
586586
/* Integer parameter */
587587
longlong larg= args_arr[print_arr[i].arg_idx].longlong_arg;
588588
my_bool suffix_e= arg_type == 'M';
589-
if (arg_type == 'u')
590-
switch (*print_arr[idx].begin) // look at the start of the next chunk
591-
{
592-
case 'E':
593-
suffix_e= TRUE;
594-
// fall-through
595-
case 'U': // escape
596-
++print_arr[idx].begin; // roll forward to consume the char
597-
break;
598-
}
589+
// look at the start of the next chunk
590+
if (arg_type == 'i' && *print_arr[i].begin == 'E')
591+
{
592+
suffix_e= TRUE;
593+
++print_arr[i].begin; // roll forward to consume the char
594+
}
599595
if (suffix_e)
600596
{
601597
const char *real_end;
@@ -807,16 +803,11 @@ size_t my_vsnprintf_ex(CHARSET_INFO *cs, char *to, size_t n,
807803
larg= va_arg(ap, int);
808804
else
809805
larg= va_arg(ap, uint);
810-
if (arg_type == 'u')
811-
switch (fmt[1]) // look-ahead
812-
{
813-
case 'E':
814-
suffix_e= TRUE;
815-
// fall-through
816-
case 'U': // escape
817-
++fmt;
818-
break;
819-
}
806+
if (arg_type == 'i' && fmt[1] == 'E') // look-ahead
807+
{
808+
suffix_e= TRUE;
809+
++fmt;
810+
}
820811
if (suffix_e)
821812
{
822813
const char *real_end= MY_MIN(to + width, end);

unittest/mysys/my_vsnprintf-t.c

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -113,9 +113,6 @@ int main(void)
113113

114114
test1("MariaDB extension escape sS works",
115115
"MariaDB extension escape sS %sS", "works");
116-
test1("MariaDB extension escape uU 2",
117-
"MariaDB extension escape uU %uU", 2);
118-
119116
test1("MariaDB extension sQ works: `abcd` `op``q`",
120117
"MariaDB extension sQ works: %sQ %.4sQ", "abcd", "op`qrst");
121118

@@ -131,21 +128,24 @@ int main(void)
131128
}
132129

133130
{
134-
// Test that %uE works
131+
// Test that %iE works
135132
const char *results[]=
136133
{
137-
"MariaDB extension uE works: 1 \"Operation not permitted\"", // Linux
138-
"MariaDB extension uE works: 1 \"Not owner\"", // Solaris
134+
"MariaDB extension iE works: 1 \"Operation not permitted\"", // Linux
135+
"MariaDB extension iE works: 1 \"Not owner\"", // Solaris
139136
NullS
140137
};
141-
test_many(results, "MariaDB extension uE works: %uE", 1);
138+
test_many(results, "MariaDB extension iE works: %iE", 1);
142139
}
143-
test1("uE with 0 errno: 0 \"Internal error/check (Not system error)\"",
144-
"uE with 0 errno: %uE", 0);
145-
test1("uE with width: <0 \"Internal error...>",
146-
"uE with width: <%.20uE>", 0);
147-
test_w_len("uE with small buf: 0 \"..",
148-
25, "uE with small buf: %uE", 0);
140+
test1("iE with 0 errno: 0 \"Internal error/check (Not system error)\"",
141+
"iE with 0 errno: %iE", 0);
142+
test1("iE with width: <0 \"Internal error...>",
143+
"iE with width: <%.20iE>", 0);
144+
test_w_len("iE with small buf: 0 \"..",
145+
25, "iE with small buf: %iE", 0);
146+
147+
test1("MariaDB extension dE DOESN'T work: 0E",
148+
"MariaDB extension dE DOESN'T work: %dE", 0);
149149

150150
test1("MariaDB extension sT works: <abcd> <op...>",
151151
"MariaDB extension sT %sT: <%.5sT> <%.5sT>", "works", "abcd", "opqrst");

0 commit comments

Comments
 (0)