[PATCH] newlib: libc: Improved the readability of strcspn with minor optimization

Xiao Zeng zengxiao@eswincomputing.com
Sat Dec 16 04:45:47 GMT 2023


2023-12-15 18:28  Torbjorn SVENSSON <torbjorn.svensson@foss.st.com> wrote:
>
Hi Bob, I worked under the risc-v architecture and obtained the newlib library through
cross compilation. This is my first attempt to contribute code to newlib. 

>Hello Xiao,
>
>On 2023-12-15 09:31, Xiao Zeng wrote:
>> Signed-off-by: Xiao Zeng <zengxiao@eswincomputing.com>
>> ---
>>   newlib/libc/string/strcspn.c | 6 ++----
>>   1 file changed, 2 insertions(+), 4 deletions(-)
>>
>> diff --git a/newlib/libc/string/strcspn.c b/newlib/libc/string/strcspn.c
>> index abaa93ad6..8ac0bf10c 100644
>> --- a/newlib/libc/string/strcspn.c
>> +++ b/newlib/libc/string/strcspn.c
>> @@ -37,12 +37,10 @@ strcspn (const char *s1,
>>         for (c = s2; *c; c++)
>>   {
>>     if (*s1 == *c)
>> -	    break;
>> +	    goto end;
>>   }
>> -      if (*c)
>> -	break;
>>         s1++;
>>       }
>> -
>> +end:
>>     return s1 - s;
>>   }
>
>Just looking at this small snippet of code, I would say that the
>previous code and your suggestion won't do the same thing.
>
>Do you have unit tests that confirm that the behavior is identical with
>the current implementation and your suggested change? 
1 I must admit that I did not conduct a complete newlib regression test.
Anyway, I should apologize for this. After completing cross compilation, I tried:
-----------------
make check
-----------------

Received the following error message:
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Target is riscv64-unknown-elf
Host   is x86_64-pc-linux-gnu
...
The error code is TCL LOOKUP COMMAND newlib_target_compile
The info on the error is:
invalid command name "newlib_target_compile"
    while executing
"::tcl_unknown newlib_target_compile /home/user/Downloads/tools/tools.git/newlib.git/newlib/testsuite/newlib.iconv/iconvnm.c /home/user/Downloads/tools..."
    ("uplevel" body line 1)
    invoked from within
"uplevel 1 ::tcl_unknown $args"
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

I know the reason for the error: Target and Host do not match, which
is a common issue in cross compilation. I have looked up some
information, but I have not found a solution.

2 On the basis of the above, I searched for test cases of the strcspn
function in newlib:
-------------------------------------------------------------------------------------------------
newlib/libm/test/string.c:311:  check(strcspn("abcba", "qx") == 5); /* Whole string. */
newlib/libm/test/string.c:312:  check(strcspn("abcba", "cx") == 2); /* Partial. */
newlib/libm/test/string.c:313:  check(strcspn("abc", "abc") == 0);	/* None. */
newlib/libm/test/string.c:314:  check(strcspn("", "ab") == 0); /* Null string. */
newlib/libm/test/string.c:315:  check(strcspn("abc", "") == 3); /* Null search list. */
-------------------------------------------------------------------------------------------------

I simply believe that these 5 test cases are all the test cases related to
strcspn in newlib. After local testing, all these test cases can pass in my patch.
>
>When I run your suggestion, I get return value 0, but with the current
>implementation it's 3 for this call: strspn("129th", "1234567890"). 
3 It is worth noting that strcspn and strspn only differ by one letter 'c'. I also
often confuse them -:), maybe you are the same.

>
>Kind regards,
>Torbjörn
  
4 Perhaps someone could point out how to test newlib under the risc-v
architecture, and I would greatly appreciate it. Of course, there are also
great methods for testing newlib under other architectures.

5 By the way, it is not possible to compile the latest newlib on host (x86_64-pc-linux-gnu) 
(compilation quickly ended as if nothing had happened), even if ../configure
comes with the parameter -- with-newlib.


Thanks
Xiao Zeng



More information about the Newlib mailing list