📄 strings.c
字号:
/* Copyright (c) 1995-2000 Microsoft Corporation. All rights reserved. */#include <corecrt.h>#if !defined(PPC) && !defined(x86) // in the crt on PPC and x86/****************************************************************************memchrThe memchr function looks for the first occurance of cCharacter in the firstiLength bytes of pBuffer and stops after finding cCharacter or after checkingiLength bytes.Arguments: pBuffer: pointer to buffer cCharacter: character to look for iLength: number of charactersReturns:If successful, memchr returns a pointer to the first location of cCharacterin pBuffer. Otherwise, it returns NULL.****************************************************************************/void * memchr(const void *pBuffer, int cCharacter, size_t iLength) { while ((iLength != 0) && (*((char *)pBuffer) != (char)cCharacter)) { ((char *) pBuffer) += 1; iLength -= 1; } return((iLength != 0) ? (void *)pBuffer : (void *)0);}#endifint _tolower(int);int _toupper(int);/****wchar_t *wcscat(dst, src) - concatenate (append) one wchar_t string to another**Purpose:* Concatenates src onto the end of dest. Assumes enough* space in dest.**Entry:* wchar_t *dst - wchar_t string to which "src" is to be appended* const wchar_t *src - wchar_t string to be appended to the end of "dst"**Exit:* The address of "dst"**Exceptions:********************************************************************************/wchar_t * wcscat(wchar_t *dst, const wchar_t *src) { wchar_t * cp = dst; while( *cp ) cp++; /* find end of dst */ while( *cp++ = *src++ ) ; /* Copy src to end of dst */ return( dst ); /* return dst */}/****wchar_t *wcscpy(dst, src) - copy one wchar_t string over another**Purpose:* Copies the wchar_t string src into the spot specified by* dest; assumes enough room.**Entry:* wchar_t * dst - wchar_t string over which "src" is to be copied* const wchar_t * src - wchar_t string to be copied over "dst"**Exit:* The address of "dst"**Exceptions:*******************************************************************************/wchar_t * wcscpy(wchar_t *dst, const wchar_t *src) { wchar_t * cp = dst; while( *cp++ = *src++ ) ; /* Copy src over dst */ return( dst );}/****wchar_t *wcschr(string, c) - search a string for a wchar_t character**Purpose:* Searches a wchar_t string for a given wchar_t character,* which may be the null character L'\0'.**Entry:* wchar_t *string - wchar_t string to search in* wchar_t c - wchar_t character to search for**Exit:* returns pointer to the first occurence of c in string* returns NULL if c does not occur in string**Exceptions:********************************************************************************/wchar_t * wcschr (const wchar_t * string, wchar_t ch) { while (*string && *string != (wchar_t)ch) string++; if (*string == (wchar_t)ch) return((wchar_t *)string); return(NULL);}/****wcscmp - compare two wchar_t strings,* returning less than, equal to, or greater than**Purpose:* wcscmp compares two wide-character strings and returns an integer* to indicate whether the first is less than the second, the two are* equal, or whether the first is greater than the second.** Comparison is done wchar_t by wchar_t on an UNSIGNED basis, which is to* say that Null wchar_t(0) is less than any other character.**Entry:* const wchar_t * src - string for left-hand side of comparison* const wchar_t * dst - string for right-hand side of comparison**Exit:* returns -1 if src < dst* returns 0 if src == dst* returns +1 if src > dst**Exceptions:********************************************************************************/int wcscmp (const wchar_t *src, const wchar_t *dst) { int ret; while((*src == *dst) && *dst) ++src, ++dst; ret = *src - *dst; return (ret > 0 ? 1 : ret < 0 ? -1 : 0);}/****size_t wcscspn(string, control) - search for init substring w/o control wchars**Purpose:* returns the index of the first character in string that belongs* to the set of characters specified by control. This is equivalent* to the length of the length of the initial substring of string* composed entirely of characters not in control. Null chars not* considered (wide-character strings).**Entry:* wchar_t *string - string to search* wchar_t *control - set of characters not allowed in init substring**Exit:* returns the index of the first wchar_t in string* that is in the set of characters specified by control.**Exceptions:********************************************************************************/size_t wcscspn (const wchar_t * string, const wchar_t * control) { wchar_t *str = (wchar_t *) string; wchar_t *wcset; /* 1st char in control string stops search */ while (*str) { for (wcset = (wchar_t *)control; *wcset; wcset++) if (*wcset == *str) return str - string; str++; } return str - string;}/****wchar_t *_wcsdup(string) - duplicate string into malloc'd memory**Purpose:* Allocates enough storage via malloc() for a copy of the* string, copies the string into the new memory, and returns* a pointer to it (wide-character).**Entry:* wchar_t *string - string to copy into new memory**Exit:* returns a pointer to the newly allocated storage with the* string in it.** returns NULL if enough memory could not be allocated, or* string was NULL.**Uses:**Exceptions:********************************************************************************/wchar_t * _wcsdup (const wchar_t * string) { wchar_t *memory; if (!string) return(NULL); if (memory = (wchar_t *) LocalAlloc(0, (wcslen(string)+1) * sizeof(wchar_t))) return(wcscpy(memory,string)); return(NULL);}char * _strdup (const char * string) { char *memory; if (!string) return(NULL); if (memory = (char *) LocalAlloc(0, strlen(string)+1)) return(strcpy(memory,string)); return(NULL);}int _stricmp(const char *str1, const char *str2) { int ch1, ch2; for (;*str1 && *str2; str1++, str2++) { ch1 = _tolower(*str1); ch2 = _tolower(*str2); if (ch1 != ch2) return ch1 - ch2; } // Check last character. return _tolower(*str1) - _tolower(*str2);}int _strnicmp(const char *psz1, const char *psz2, size_t cb) { int ch1, ch2 = 0; // Zero for case cb = 0. while (cb--) { ch1 = _tolower(*(psz1++)); ch2 = _tolower(*(psz2++)); if (!ch1 || (ch1 != ch2)) break; } return (ch1 - ch2);}/****wcslen - return the length of a null-terminated wide-character string**Purpose:* Finds the length in wchar_t's of the given string, not including* the final null wchar_t (wide-characters).**Entry:* const wchar_t * wcs - string whose length is to be computed**Exit:* length of the string "wcs", exclusive of the final null wchar_t**Exceptions:********************************************************************************/size_t wcslen (const wchar_t * wcs) { const wchar_t *eos = wcs; while( *eos++ ) ; return( (size_t)(eos - wcs - 1) );}/****wchar_t *wcsncat(front, back, count) - append count chars of back onto front**Purpose:* Appends at most count characters of the string back onto the* end of front, and ALWAYS terminates with a null character.* If count is greater than the length of back, the length of back* is used instead. (Unlike wcsncpy, this routine does not pad out* to count characters).**Entry:* wchar_t *front - string to append onto* wchar_t *back - string to append* size_t count - count of max characters to append**Exit:* returns a pointer to string appended onto (front).**Uses:**Exceptions:********************************************************************************/wchar_t * wcsncat(wchar_t * front, const wchar_t * back, size_t count) { wchar_t *start = front; while (*front++) ; front--; while (count--) if (!(*front++ = *back++)) return(start); *front = L'\0'; return(start);}/****int wcsncmp(first, last, count) - compare first count chars of wchar_t strings**Purpose:* Compares two strings for lexical order. The comparison stops* after: (1) a difference between the strings is found, (2) the end* of the strings is reached, or (3) count characters have been* compared (wide-character strings).**Entry:* wchar_t *first, *last - strings to compare* size_t count - maximum number of characters to compare**Exit:* returns <0 if first < last* returns 0 if first == last* returns >0 if first > last**Exceptions:********************************************************************************/int wcsncmp(const wchar_t * first, const wchar_t * last, size_t count) { if (!count) return(0); while (--count && *first && *first == *last) { first++; last++; } return((int)(*first - *last));}/****wchar_t *wcsncpy(dest, source, count) - copy at most n wide characters**Purpose:* Copies count characters from the source string to the* destination. If count is less than the length of source,* NO NULL CHARACTER is put onto the end of the copied string.* If count is greater than the length of sources, dest is padded* with null characters to length count (wide-characters).***Entry:* wchar_t *dest - pointer to destination* wchar_t *source - source string for copy* size_t count - max number of characters to copy**Exit:* returns dest**Exceptions:********************************************************************************/wchar_t * wcsncpy(wchar_t * dest, const wchar_t * source, size_t count) { wchar_t *start = dest; while (count && (*dest++ = *source++)) /* copy string */ count--; if (count) /* pad out with zeroes */ while (--count) *dest++ = L'\0'; return(start);}/****wchar_t *_wcsnset(string, val, count) - set at most count characters to val**Purpose:* Sets the first count characters of string the character value.* If the length of string is less than count, the length of* string is used in place of n (wide-characters).**Entry:* wchar_t *string - string to set characters in* wchar_t val - character to fill with* size_t count - count of characters to fill**Exit:* returns string, now filled with count copies of val.**Exceptions:********************************************************************************/wchar_t * _wcsnset (wchar_t * string, wchar_t val, size_t count) { wchar_t *start = string; while (count-- && *string) *string++ = val; return(start);}char * _strnset (char * string, int val, size_t count) { char *start = string; while (count-- && *string) *string++ = val; return(start);}/****wchar_t *wcspbrk(string, control) - scans string for a character from control**Purpose:* Returns pointer to the first wide-character in* a wide-character string in the control string.**Entry:* wchar_t *string - string to search in* wchar_t *control - string containing characters to search for**Exit:* returns a pointer to the first character from control found* in string.* returns NULL if string and control have no characters in common.**Exceptions:********************************************************************************/wchar_t * wcspbrk(const wchar_t * string, const wchar_t * control) { wchar_t *wcset; /* 1st char in control string stops search */ while (*string) { for (wcset = (wchar_t *) control; *wcset; wcset++) if (*wcset == *string) return (wchar_t *) string; string++; } return NULL;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -