From d837f450572fa1a84b36fccb3d7404442ca03ca6 Mon Sep 17 00:00:00 2001 From: Lv Zheng Date: Tue, 12 Jul 2016 14:37:54 +0800 Subject: Clib: Add generic strpbrk() and strtok() to improve portability This patch implements the native string APIs of strpbrk()/strtok(). Unit tests have been done to these two functions in unix environment and no bugs have been found. Lv Zheng. Signed-off-by: Lv Zheng --- source/components/utilities/utclib.c | 87 ++++++++++++++++++++++++++++++++++++ source/include/acclib.h | 10 +++++ 2 files changed, 97 insertions(+) diff --git a/source/components/utilities/utclib.c b/source/components/utilities/utclib.c index d2f73642f..31241a992 100644 --- a/source/components/utilities/utclib.c +++ b/source/components/utilities/utclib.c @@ -356,6 +356,93 @@ strlen ( } +/******************************************************************************* + * + * FUNCTION: strpbrk + * + * PARAMETERS: String - Null terminated string + * Delimiters - Delimiters to match + * + * RETURN: The first occurance in the string of any of the bytes in the + * delimiters + * + * DESCRIPTION: Search a string for any of a set of the delimiters + * + ******************************************************************************/ + +char * +strpbrk ( + const char *String, + const char *Delimiters) +{ + const char *Delimiter; + + + for ( ; *String != '\0'; ++String) + { + for (Delimiter = Delimiters; *Delimiter != '\0'; Delimiter++) + { + if (*String == *Delimiter) + { + return (ACPI_CAST_PTR (char, String)); + } + } + } + + return (NULL); +} + + +/******************************************************************************* + * + * FUNCTION: strtok + * + * PARAMETERS: String - Null terminated string + * Delimiters - Delimiters to match + * + * RETURN: Pointer to the next token + * + * DESCRIPTION: Split string into tokens + * + ******************************************************************************/ + +char* +strtok ( + char *String, + const char *Delimiters) +{ + char *Begin = String; + static char *SavedPtr; + + + if (Begin == NULL) + { + if (SavedPtr == NULL) + { + return (NULL); + } + Begin = SavedPtr; + } + + SavedPtr = strpbrk (Begin, Delimiters); + while (SavedPtr == Begin) + { + *Begin++ = '\0'; + SavedPtr = strpbrk (Begin, Delimiters); + } + + if (SavedPtr) + { + *SavedPtr++ = '\0'; + return (Begin); + } + else + { + return (NULL); + } +} + + /******************************************************************************* * * FUNCTION: strcpy diff --git a/source/include/acclib.h b/source/include/acclib.h index 10667216a..bd7b564e0 100644 --- a/source/include/acclib.h +++ b/source/include/acclib.h @@ -175,6 +175,16 @@ strchr ( const char *String, int ch); +char * +strpbrk ( + const char *String, + const char *Delimiters); + +char * +strtok ( + char *String, + const char *Delimiters); + char * strcpy ( char *DstString, -- cgit v1.2.1