All of lore.kernel.org
 help / color / mirror / Atom feed
From: Heinrich Schuchardt <xypron.glpk@gmx.de>
To: u-boot@lists.denx.de
Subject: [PATCH v4 02/31] lib: Add a function to convert a string to upper case
Date: Wed, 08 Apr 2020 04:28:52 +0000	[thread overview]
Message-ID: <18F5D670-A753-4BF5-84F8-DDCB2F993F6A@gmx.de> (raw)
In-Reply-To: <4A84FC6A-D9FD-4ACF-A922-26353E1F4970@gmx.de>

Am April 8, 2020 4:18:12 AM UTC schrieb Heinrich Schuchardt <xypron.glpk@gmx.de>:
>Am April 8, 2020 3:00:38 AM UTC schrieb Simon Glass <sjg@chromium.org>:
>>Add a helper function for this operation. Update the strtoul() tests
>to
>>check upper case as well.
>>
>>Update FAT writing to use this new function.
>>
>>Signed-off-by: Simon Glass <sjg@chromium.org>
>>---
>>
>>Changes in v4:
>>- Add a new patch to convert a string to upper case
>>
>>Changes in v3: None
>>Changes in v2: None
>>
>> fs/fat/fat_write.c | 13 ++-------
>> include/vsprintf.h | 12 ++++++++
>> lib/strto.c        |  8 +++++
>> test/str_ut.c      | 73
>++++++++++++++++++++++++++++++++++------------
>> 4 files changed, 77 insertions(+), 29 deletions(-)
>>
>>diff --git a/fs/fat/fat_write.c b/fs/fat/fat_write.c
>>index 4f96699e363..472c206f64c 100644
>>--- a/fs/fat/fat_write.c
>>+++ b/fs/fat/fat_write.c
>>@@ -10,6 +10,7 @@
>> #include <config.h>
>> #include <fat.h>
>> #include <malloc.h>
>>+#include <vsprintf.h>
>> #include <asm/byteorder.h>
>> #include <part.h>
>> #include <linux/ctype.h>
>>@@ -17,16 +18,6 @@
>> #include <linux/math64.h>
>> #include "fat.c"
>> 
>>-static void uppercase(char *str, int len)
>>-{
>>-	int i;
>>-
>>-	for (i = 0; i < len; i++) {
>>-		*str = toupper(*str);
>>-		str++;
>>-	}
>>-}

We should not use toupper() here. We have to consider the FAT charset defined by CONFIG_FAT_DEFAULT_CODEPAGE.

Best regards

Heinrich


>>-
>> static int total_sector;
>> static int disk_write(__u32 block, __u32 nr_blocks, void *buf)
>> {
>>@@ -65,7 +56,7 @@ static void set_name(dir_entry *dirent, const char
>>*filename)
>> 		return;
>> 
>> 	strcpy(s_name, filename);
>>-	uppercase(s_name, len);
>>+	str_to_upper(s_name, s_name, len);
>> 
>> 	period = strchr(s_name, '.');
>> 	if (period == NULL) {
>>diff --git a/include/vsprintf.h b/include/vsprintf.h
>>index 56844dd2de8..d3f1292ae4d 100644
>>--- a/include/vsprintf.h
>>+++ b/include/vsprintf.h
>>@@ -222,4 +222,16 @@ bool str2long(const char *p, ulong *num);
>>  * @hz: Value to convert
>>  */
>> char *strmhz(char *buf, unsigned long hz);
>>+
>>+/**
>>+ * str_to_upper() - Convert a string to upper case
>>+ *
>>+ * This simply uses toupper() on each character of the string.
>>+ *
>>+ * @in: String to convert (must be large enough to hold the output
>>string)
>>+ * @out: Converting string
>>+ * @len: Number of characters to convert (-1 for all)
>>+ */
>>+void str_to_upper(const char *in, char *out, int len);
>
>Wouldn't we prefer size_t like in other string functions for len?
>
>
>>+
>> #endif
>>diff --git a/lib/strto.c b/lib/strto.c
>>index 55ff9f7437d..563892a52c0 100644
>>--- a/lib/strto.c
>>+++ b/lib/strto.c
>>@@ -163,3 +163,11 @@ long trailing_strtol(const char *str)
>> {
>> 	return trailing_strtoln(str, NULL);
>> }
>>+
>>+void str_to_upper(const char *in, char *out, int len)
>>+{
>>+	while ((len == -1 || len-- > 0) && *in)
>
>Why a special logic for -1? Just use SIZE_MAX when calling the
>function.
>
>>+		*out++ = toupper(*in++);
>>+	if (len && !*in)
>>+		*out = '\0';
>
>We shouldn't check len and *in twice in the loop.
>
>Please, add a test for
>
>str_to_upper("", SIZE_MAX) which your code is handling incorrectly. -
>You miss copying 0x00 here.
>
>Best regards
>
>Heinrich
>
>>+}
>>diff --git a/test/str_ut.c b/test/str_ut.c
>>index 0ce6b055792..09726b076fe 100644
>>--- a/test/str_ut.c
>>+++ b/test/str_ut.c
>>@@ -19,36 +19,73 @@ static const char str3[] = "0xbI'm sorry you're
>>alive.";
>> /* Declare a new str test */
>> #define STR_TEST(_name, _flags)		UNIT_TEST(_name, _flags, str_test)
>> 
>>+static int str_test_upper(struct unit_test_state *uts)
>>+{
>>+	char out[TEST_STR_SIZE];
>>+
>>+	/* Make sure it adds a terminator */
>>+	out[strlen(str1)] = 'a';
>>+	str_to_upper(str1, out, -1);
>>+	ut_asserteq_str("I'M SORRY I'M LATE.", out);
>>+
>>+	/* In-place operation */
>>+	strcpy(out, str2);
>>+	str_to_upper(out, out, -1);
>>+	ut_asserteq_str("1099ABNO, DON'T BOTHER APOLOGISING.", out);
>>+
>>+	/* Limited length */
>>+	str_to_upper(str1, out, 7);
>>+	ut_asserteq_str("I'M SORO, DON'T BOTHER APOLOGISING.", out);
>>+
>>+	/* In-place with limited length */
>>+	strcpy(out, str2);
>>+	str_to_upper(out, out, 7);
>>+	ut_asserteq_str("1099ABNo, don't bother apologising.", out);
>>+
>>+	return 0;
>>+}
>>+STR_TEST(str_test_upper, 0);
>>+
>>static int run_strtoul(struct unit_test_state *uts, const char *str,
>>int base,
>>-		       ulong expect_val, int expect_endp_offset)
>>+		       ulong expect_val, int expect_endp_offset, bool upper)
>> {
>>+	char out[TEST_STR_SIZE];
>> 	char *endp;
>> 	ulong val;
>> 
>>-	val = simple_strtoul(str, &endp, base);
>>+	strcpy(out, str);
>>+	if (upper)
>>+		str_to_upper(out, out, -1);
>>+
>>+	val = simple_strtoul(out, &endp, base);
>> 	ut_asserteq(expect_val, val);
>>-	ut_asserteq(expect_endp_offset, endp - str);
>>+	ut_asserteq(expect_endp_offset, endp - out);
>> 
>> 	return 0;
>> }
>> 
>> static int str_simple_strtoul(struct unit_test_state *uts)
>> {
>>-	/* Base 10 and base 16 */
>>-	ut_assertok(run_strtoul(uts, str2, 10, 1099, 4));
>>-	ut_assertok(run_strtoul(uts, str2, 16, 0x1099ab, 6));
>>-
>>-	/* Invalid string */
>>-	ut_assertok(run_strtoul(uts, str1, 10, 0, 0));
>>-
>>-	/* Base 0 */
>>-	ut_assertok(run_strtoul(uts, str1, 0, 0, 0));
>>-	ut_assertok(run_strtoul(uts, str2, 0, 1099, 4));
>>-	ut_assertok(run_strtoul(uts, str3, 0, 0xb, 3));
>>-
>>-	/* Base 2 */
>>-	ut_assertok(run_strtoul(uts, str1, 2, 0, 0));
>>-	ut_assertok(run_strtoul(uts, str2, 2, 2, 2));
>>+	int upper;
>>+
>>+	/* Check that it is case-insentive */
>>+	for (upper = 0; upper < 2; upper++) {
>>+		/* Base 10 and base 16 */
>>+		ut_assertok(run_strtoul(uts, str2, 10, 1099, 4, upper));
>>+		ut_assertok(run_strtoul(uts, str2, 16, 0x1099ab, 6, upper));
>>+
>>+		/* Invalid string */
>>+		ut_assertok(run_strtoul(uts, str1, 10, 0, 0, upper));
>>+
>>+		/* Base 0 */
>>+		ut_assertok(run_strtoul(uts, str1, 0, 0, 0, upper));
>>+		ut_assertok(run_strtoul(uts, str2, 0, 1099, 4, upper));
>>+		ut_assertok(run_strtoul(uts, str3, 0, 0xb, 3, upper));
>>+
>>+		/* Base 2 */
>>+		ut_assertok(run_strtoul(uts, str1, 2, 0, 0, upper));
>>+		ut_assertok(run_strtoul(uts, str2, 2, 2, 2, upper));
>>+	}
>> 
>> 	/* Check endp being NULL */
>> 	ut_asserteq(1099, simple_strtoul(str2, NULL, 0));

  reply	other threads:[~2020-04-08  4:28 UTC|newest]

Thread overview: 41+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-04-08  3:00 [PATCH v4 00/31] dm: Add programmatic generation of ACPI tables (part A) Simon Glass
2020-04-08  3:00 ` [PATCH v4 01/31] test: Add the beginnings of some string tests Simon Glass
2020-04-08  3:00 ` [PATCH v4 02/31] lib: Add a function to convert a string to upper case Simon Glass
2020-04-08  4:18   ` Heinrich Schuchardt
2020-04-08  4:28     ` Heinrich Schuchardt [this message]
2020-04-08 14:20       ` Simon Glass
2020-04-09  5:32         ` Heinrich Schuchardt
2020-04-08  3:00 ` [PATCH v4 03/31] cpu: Support querying the address width Simon Glass
2020-04-08  3:00 ` [PATCH v4 04/31] spi: Add SPI mode enums Simon Glass
2020-04-08  3:00 ` [PATCH v4 05/31] tpm: cr50: Release locality on exit Simon Glass
2020-04-08  3:00 ` [PATCH v4 06/31] tpm: cr50: Add a comment for cr50_priv Simon Glass
2020-04-08  3:00 ` [PATCH v4 07/31] tpm: cr50: Use the correct GPIO binding Simon Glass
2020-04-08  3:00 ` [PATCH v4 08/31] tpm: Don't cleanup unless an error happens Simon Glass
2020-04-08  3:00 ` [PATCH v4 09/31] dm: pci: Allow disabling auto-config for a device Simon Glass
2020-04-08  3:00 ` [PATCH v4 10/31] x86: Correct wording of coreboot source code Simon Glass
2020-04-08  3:00 ` [PATCH v4 11/31] x86: apl: Move p2sb ofdata reading to the correct method Simon Glass
2020-04-08  3:00 ` [PATCH v4 12/31] pci: Adjust dm_pci_read_bar32() to return errors correctly Simon Glass
2020-04-08  3:00 ` [PATCH v4 13/31] x86: apl: Add Global NVS table header Simon Glass
2020-04-08 17:14   ` Andy Shevchenko
2020-04-08  3:00 ` [PATCH v4 14/31] dm: core: Add basic ACPI support Simon Glass
2020-04-08  3:00 ` [PATCH v4 15/31] dts: Add a binding for hid-over-i2c Simon Glass
2020-04-08  3:00 ` [PATCH v4 16/31] acpi: Add a binding for ACPI settings in the device tree Simon Glass
2020-04-08  3:00 ` [PATCH v4 17/31] acpi: Add a simple sandbox test Simon Glass
2020-04-08  3:00 ` [PATCH v4 18/31] x86: Move acpi_s3.h to include/acpi/ Simon Glass
2020-04-08  3:00 ` [PATCH v4 19/31] x86: Move acpi_table header to main include/ directory Simon Glass
2020-04-08  3:00 ` [PATCH v4 20/31] acpi: Add an __ACPI__ preprocessor symbol Simon Glass
2020-04-08  3:00 ` [PATCH v4 21/31] acpi: Add a central location for table version numbers Simon Glass
2020-04-08 17:20   ` Andy Shevchenko
2020-04-08 19:32     ` Simon Glass
2020-04-08  3:00 ` [PATCH v4 22/31] acpi: Add support for DMAR Simon Glass
2020-04-08  3:00 ` [PATCH v4 23/31] test: Add hexdump.h to the unit test header Simon Glass
2020-04-08  3:01 ` [PATCH v4 24/31] acpi: Add a method to write tables for a device Simon Glass
2020-04-08  3:01 ` [PATCH v4 25/31] acpi: Convert part of acpi_table to use acpi_ctx Simon Glass
2020-04-08  3:01 ` [PATCH v4 26/31] x86: Allow devices to write ACPI tables Simon Glass
2020-04-08  3:01 ` [PATCH v4 27/31] acpi: Drop code for missing XSDT from acpi_write_rsdp() Simon Glass
2020-04-08  3:01 ` [PATCH v4 28/31] acpi: Move acpi_add_table() to generic code Simon Glass
2020-04-08  3:01 ` [PATCH v4 29/31] acpi: Put table-setup code in its own function Simon Glass
2020-04-08  3:01 ` [PATCH v4 30/31] acpi: Move the xsdt pointer to acpi_ctx Simon Glass
2020-04-08  3:01 ` [PATCH v4 31/31] acpi: Add an acpi command Simon Glass
2020-04-08 17:02 ` [PATCH v4 00/31] dm: Add programmatic generation of ACPI tables (part A) Andy Shevchenko
2020-04-08 22:22   ` Simon Glass

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=18F5D670-A753-4BF5-84F8-DDCB2F993F6A@gmx.de \
    --to=xypron.glpk@gmx.de \
    --cc=u-boot@lists.denx.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.