Below is the rewriting of some string library functions in the C language. Now I will post the code to share with you,
If any errors are found, please give me more advice.
# Include <stdio. h>
# Include <stdlib. h>
# Include <assert. h>
//////////////////////////////////////// ////////////
// Convert character to uppercase.
Const char * toupper (char * s)
{
For (char * t = s; * t! = '/0'; t ++)
{
If (* t> = 'A' & * t <= 'Z ')
* T-= 'a'-'A ';
}
Return s;
}
//////////////////////////////////////// ////////////
// Copies characters between buffers.
//
// Function Description: memcpy () is used to copy the first n Bytes of memory content in src to the memory address in dest.
// Unlike strcpy (), memcpy () completely copies n Bytes and does not end with a string ending '/0'.
// Memcpy functions are the same as memmove, but the dest and source regions in memcpy cannot overlap. Otherwise, unknown results may occur.
//
// Return value description: returns the void * pointer to the dest.
// Additional description: the memory areas indicated by the pointer src and dest cannot overlap.
//
Void * memcpy (void * dest, const void * src, size_t count)
{
Assert (dest! = NULL) & (src! = NULL ));
Char * tmp_dest = (char *) dest;
Char * tmp_src = (char *) src;
While (count --) // does not judge whether there are overlapping areas
* Tmp_dest ++ = * tmp_src ++;
Return dest;
}
//////////////////////////////////////// ////////////
// Moves one buffer to another.
//
// Function Description: memmove is used to copy count characters from source to dest. If the target region and source region overlap,
// Memmove can ensure that the source string copies the bytes of the overlapping area to the target area before being overwritten.
// Return value description: returns the void * pointer to the dest.
Void * memmove (void * dest, const void * src, size_t count)
{
Assert (dest! = NULL) & (src! = NULL ));
Char * tmp_dest = (char *) dest;
Char * tmp_src = (char *) src;
If (tmp_dest + count <tmp_src | tmp_src + count <tmp_dest)
{// If no overlapping areas exist
While (count --)
* Tmp_dest ++ = * tmp_src;
}
Else
{// If overlap exists
Tmp_dest + = count-1;
Tmp_src + = count-1;
While (count --)
* -- Tmp_dest = * -- tmp_src;
}
Return dest;
}
//////////////////////////////////////// ////////////
// Sets buffers to a specified character.
Void * memset (void * src, int c, size_t count)
{
Assert (src! = NULL );
Char * tmpsrc = (char *) src;
While (count --)
* Tmpsrc ++ = (char) c;
Return src;
}
//////////////////////////////////////// ////////////
// Finds characters in a buffer.
Void * memchr (const void * src, int c, size_t count)
{
Assert (src! = NULL );
Char * tempsrc = (char *) src;
While (count & * tempsrc! = (Char) c)
{
Count --;
Tempsrc ++;
}
If (count! = 0)
Return tempsrc;
Else
Return NULL;
}
//////////////////////////////////////// ////////////
// Get the length of a string
Int strlen (const char * str)
{
Assert (str! = NULL );
Int length = 0;
While (* str ++! = '/0 ')
Length ++;
Return length;
}
//////////////////////////////////////// ////////////
// Copy a string
/* Void strcpy (char * strDest, const char * strSrc)
{
Assert (strSrc! = NULL) & (strDest! = NULL ));
While (* strSrc! = '/0 ')
* StrDest ++ = * strSrc ++;
* StrDest = '/0 ';
}*/
// Standard library functions
Char * strcpy (char * strDest, const char * strSrc)
{
Assert (strSrc! = NULL) & (strDest! = NULL ));
Char * address = strDest;
While (* address ++ = * strSrc ++ )! = '/0 ');
Return address;
}
//////////////////////////////////////// ////////////
// Append a string
Char * strcat (char * str1, const char * str2)
{
Assert (str1! = NULL) & (str2! = NULL ));
Char * temp1 = str1;
While (* temp1 ++! = '\ 0 ');
Temp --;
While (* temp1 ++ = * str2 ++ )! = '/0 ');
Return str1;
}
//////////////////////////////////////// ////////////
// Compare strings
Int strcmp (const char * str1, const char * str2)
{
While (* str1! = '/0' & * str2! = '/0' & * str1 = * str2)
{
Str1 ++;
Str2 ++;
}
If (* str1 = '/0 ')
{
If (* str2 = '/0 ')
Return 0;
Else
Return-1;
}
Else if (* str2 = '/0 ')
{
If (* str1 = '/0 ')
Return 0;
Else
Return 1;
}
Else
{
If (* str1 <* str2)
Return-1;
Else
Return 1;
}
}
//////////////////////////////////////// ////////////
// Find a character in a string.
Char * strchr (const char * str, int ch)
{
While (* str! = '/0' & * str! = (Char) ch)
Str ++;
If (* str = (char) ch)
Return (char *) str;
Else
Return NULL;
}
//////////////////////////////////////// ////////////
// Find a substring.
Char * strstr (const char * src, const char * strCharSet)
{
Char * tempStr = (char *) src;
While (* tempStr ++! = * StrCharSet )! = '/0 ');
Char * temp = -- tempStr;
If (* tempStr = '/0 ')
Return NULL;
Else
{
While (* tempStr = * strCharSet & * tempStr! = '/0' & * strCharSet! = '/0 ')
{
TempStr ++;
StrCharSet ++;
}
If (* strCharSet = '/0 ')
Return temp;
Else
Return NULL;
}
}