Switch lower/upper to wcstring

Also, these are different only in that one uses "towlower", the other
"towupper". So just make one function that both call.
This commit is contained in:
Fabian Homborg 2018-01-11 16:04:28 +01:00
parent 191ca21092
commit 0cfe722a93

View file

@ -1327,8 +1327,8 @@ static int string_trim(parser_t &parser, io_streams_t &streams, int argc, wchar_
return ntrim > 0 ? STATUS_CMD_OK : STATUS_CMD_ERROR; return ntrim > 0 ? STATUS_CMD_OK : STATUS_CMD_ERROR;
} }
/// Implementation of `string lower`. // A helper function for lower and upper.
static int string_lower(parser_t &parser, io_streams_t &streams, int argc, wchar_t **argv) { static int string_transform(parser_t &parser, io_streams_t &streams, int argc, wchar_t **argv, decltype(std::towlower) func) {
options_t opts; options_t opts;
opts.quiet_valid = true; opts.quiet_valid = true;
int optind; int optind;
@ -1337,10 +1337,10 @@ static int string_lower(parser_t &parser, io_streams_t &streams, int argc, wchar
int n_transformed = 0; int n_transformed = 0;
arg_iterator_t aiter(argv, optind, streams); arg_iterator_t aiter(argv, optind, streams);
while (const wchar_t *arg = aiter.next()) { while (auto arg = aiter.nextstr()) {
wcstring transformed(arg); wcstring transformed(*arg);
std::transform(transformed.begin(), transformed.end(), transformed.begin(), std::towlower); std::transform(transformed.begin(), transformed.end(), transformed.begin(), func);
if (wcscmp(transformed.c_str(), arg)) n_transformed++; if (transformed != *arg) n_transformed++;
if (!opts.quiet) { if (!opts.quiet) {
streams.out.append(transformed); streams.out.append(transformed);
streams.out.append(L'\n'); streams.out.append(L'\n');
@ -1350,27 +1350,14 @@ static int string_lower(parser_t &parser, io_streams_t &streams, int argc, wchar
return n_transformed > 0 ? STATUS_CMD_OK : STATUS_CMD_ERROR; return n_transformed > 0 ? STATUS_CMD_OK : STATUS_CMD_ERROR;
} }
/// Implementation of `string lower`.
static int string_lower(parser_t &parser, io_streams_t &streams, int argc, wchar_t **argv) {
return string_transform(parser, streams, argc, argv, std::towlower);
}
/// Implementation of `string upper`. /// Implementation of `string upper`.
static int string_upper(parser_t &parser, io_streams_t &streams, int argc, wchar_t **argv) { static int string_upper(parser_t &parser, io_streams_t &streams, int argc, wchar_t **argv) {
options_t opts; return string_transform(parser, streams, argc, argv, std::towupper);
opts.quiet_valid = true;
int optind;
int retval = parse_opts(&opts, &optind, 0, argc, argv, parser, streams);
if (retval != STATUS_CMD_OK) return retval;
int n_transformed = 0;
arg_iterator_t aiter(argv, optind, streams);
while (const wchar_t *arg = aiter.next()) {
wcstring transformed(arg);
std::transform(transformed.begin(), transformed.end(), transformed.begin(), std::towupper);
if (wcscmp(transformed.c_str(), arg)) n_transformed++;
if (!opts.quiet) {
streams.out.append(transformed);
streams.out.append(L'\n');
}
}
return n_transformed > 0 ? STATUS_CMD_OK : STATUS_CMD_ERROR;
} }
static const struct string_subcommand { static const struct string_subcommand {