From d0574a973568679d9cf8c49595e797ab00d20aae Mon Sep 17 00:00:00 2001 From: "warren%netscape.com" Date: Sat, 6 Feb 1999 04:18:15 +0000 Subject: [PATCH] Added strtok implementation. --- base/src/nsCRT.cpp | 40 +++++++++++++++++++++++++++++++++++++++- xpcom/ds/nsCRT.cpp | 40 +++++++++++++++++++++++++++++++++++++++- 2 files changed, 78 insertions(+), 2 deletions(-) diff --git a/base/src/nsCRT.cpp b/base/src/nsCRT.cpp index d25c937a78b..2518c8e0a25 100644 --- a/base/src/nsCRT.cpp +++ b/base/src/nsCRT.cpp @@ -138,11 +138,49 @@ PRUnichar nsCRT::ToLower(PRUnichar aChar) return TOLOWER(aChar); } +//////////////////////////////////////////////////////////////////////////////// +// My lovely strtok routine + +#define IS_DELIM(m, c) ((m)[(c) >> 3] & (1 << ((c) & 7))) +#define SET_DELIM(m, c) ((m)[(c) >> 3] |= (1 << ((c) & 7))) +#define DELIM_TABLE_SIZE 32 + char* nsCRT::strtok(char* str, const char* delims, char* *newStr) { - return NULL; + NS_ASSERTION(str, "Unlike regular strtok, the first argument cannot be null."); + + char delimTable[DELIM_TABLE_SIZE]; + PRUint32 i; + char* result; + + for (i = 0; i < DELIM_TABLE_SIZE; i++) + delimTable[i] = '\0'; + + do { + SET_DELIM(delimTable, *delims); + } while (*delims++); + + // skip to beginning + while (*str && IS_DELIM(delimTable, *str)) { + str++; + } + result = str; + + // fix up the end of the token + while (*str) { + if (IS_DELIM(delimTable, *str)) { + *str++ = '\0'; + break; + } + str++; + } + *newStr = str; + + return str == result ? NULL : result; } +//////////////////////////////////////////////////////////////////////////////// + PRUint32 nsCRT::strlen(const PRUnichar* s) { PRUint32 len = 0; diff --git a/xpcom/ds/nsCRT.cpp b/xpcom/ds/nsCRT.cpp index d25c937a78b..2518c8e0a25 100644 --- a/xpcom/ds/nsCRT.cpp +++ b/xpcom/ds/nsCRT.cpp @@ -138,11 +138,49 @@ PRUnichar nsCRT::ToLower(PRUnichar aChar) return TOLOWER(aChar); } +//////////////////////////////////////////////////////////////////////////////// +// My lovely strtok routine + +#define IS_DELIM(m, c) ((m)[(c) >> 3] & (1 << ((c) & 7))) +#define SET_DELIM(m, c) ((m)[(c) >> 3] |= (1 << ((c) & 7))) +#define DELIM_TABLE_SIZE 32 + char* nsCRT::strtok(char* str, const char* delims, char* *newStr) { - return NULL; + NS_ASSERTION(str, "Unlike regular strtok, the first argument cannot be null."); + + char delimTable[DELIM_TABLE_SIZE]; + PRUint32 i; + char* result; + + for (i = 0; i < DELIM_TABLE_SIZE; i++) + delimTable[i] = '\0'; + + do { + SET_DELIM(delimTable, *delims); + } while (*delims++); + + // skip to beginning + while (*str && IS_DELIM(delimTable, *str)) { + str++; + } + result = str; + + // fix up the end of the token + while (*str) { + if (IS_DELIM(delimTable, *str)) { + *str++ = '\0'; + break; + } + str++; + } + *newStr = str; + + return str == result ? NULL : result; } +//////////////////////////////////////////////////////////////////////////////// + PRUint32 nsCRT::strlen(const PRUnichar* s) { PRUint32 len = 0;