Simulation of common string functions and memory functions

Posted by IRON FART on Sun, 27 Feb 2022 09:36:45 +0100

@[TOC] (directory)

catalogue

Purpose of this chapter

strlen Simulation Implementation

Implementation of strcmp simulation

Implementation of strcat simulation

strcpy Simulation Implementation

Implementation of STR simulation

Implementation of strncmp simulation

Implementation of strncat simulation

Implementation of strncpy simulation

Introducing strtok

Introducing strerror

Simulation Implementation of memcpy

Simulation Implementation of memmove

Analog implementation of memset

Simulation Implementation of memcmp

         last

Purpose of this chapter:

Simulate the implementation of string functions: strlen, strcmp, strcat, strcpy, strstr, strncmp, strncat, strncpy

Introduce the use of strtok and strerror

Simulated memory functions: memcpy, memmove, memset, memcmp

strlen Simulation Implementation

size_t my_strlen(const char* str)
{
	assert(str);
	if (*str == '\0')
	{
		return 0;
	}
	return 1 + strlen(str + 1);
}

Implementation of strcmp simulation

int my_strcmp(const char* str1,const char* str2)
{
	assert(str1 && str2);
	while (*str1 == *str2)
	{
		if (*str1 == '\0')
		{
			return 0;
		}
		str1++;
		str2++;
	}
	return *str1 - *str2;
}

Implementation of strcat simulation

char* my_strcat(char* dest,const char* sour)
{
	char* ret = dest;
	assert(dest && sour);
	while (*dest)
	{
		dest++;
	}
	while (*dest++ = *sour++)
	{
		;
	}
	return ret;
}

In short:

Find the '\ 0' of the target string, then copy the source string (including the end flag of the source string), and return the starting address of the target string.

strcpy Simulation Implementation

char* my_strcpy(char* dest,const char* sour)
{
	char* ret = dest;
	assert(dest && sour);
	while (*dest++=*sour++)
	{
		;
	}
	return ret;
}

Implementation of STR simulation

char* my_strstr(char* str,const char* substr)
{
	char* cur = str;
	assert(str && substr);
	while (*cur)
	{
		char* s1 = cur;
		char* s2 = (char*)substr;
		while (*s1 == *s2 && *s2)
		{
			s1++;
			s2++;
		}
		if (*s2 == '\0')
		{
			return cur;
		}
		cur++;
	}
	return NULL;
}

Implementation of strncmp simulation

int my_strncmp(const char* str1,const char* str2,int n)
{
	assert(str1 && str2);
	while (*str1 == *str2 && n > 0)
	{
		n--;
		str1++;
		str2++;
	}
	if (n == 0)
	{
		return 0;
	}
	return *str1 - *str2;
}

Implementation of strncat simulation

char* my_strncat(char* dest,const char* sour,int n)
{
	char* ret = dest;
	assert(dest && sour);
	while (*dest)
	{
		dest++;
	}
	while (n--)
	{
		*dest++ = *sour++;
	}
	return ret;
}

Implementation of strncpy simulation

char* my_strncpy(char* dest,const char* sour,int n)
{
	char* ret = dest;
	assert(dest && sour);
	while (n--)
	{
		*dest++ = *sour++;
	}
	return ret;
}

Introducing strtok

The strtok function can achieve the effect of splitting strings with some symbols

If you want to start from“ hello@world.name #Extract "hello", "world", "name" and "apple" strings from "apple", and you can use strtok, where“ hello@world.name ”For the destination string, "@. #" is a set of delimiters.

The first parameter strToken is the string to be split, and the second parameter strdelay is the set of splitters.

About the return value of strtok

All these functions return a pointer to the next token found in strToken. When no more tokens are found, they return NULL. Each call modifies strToken by replacing each delimiter encountered with a NULL character.

The strtok function finds the next tag in str, ends it with \ 0, and returns a pointer to this tag. Therefore, the strtok function will change the string to be manipulated. Therefore, the string segmented by the strtok function is generally the content of temporary copy
And can be modified.)
The first parameter of the strtok function is not NULL. The function will find the first tag in str and the strtok function will save it in the string
Position in the.
The first parameter of strtok function is NULL. The function will start at the position saved in the same string to find the next index
Remember.
A NULL pointer is returned if there are no more tags in the string.

The following is from“ hello@world.name #"Apple" separates the code of "hello", "world", "name" and "apple"

Introducing strerror

This function is relatively simple to use. It prints the corresponding error information according to the passed error code and returns the character pointer.

The error code can be 0, 1, 2, 3, 4, 5, 6, etc.

For example:

 

In practice, it is used together with errno (a global variable) in most cases

Errno, a global variable, will automatically change its error code with program errors, so we usually use strerror (errno) to print error messages.

Note the reference of the header file. The header file of errno is errno H , and strerror's header file is string h

Usage scenario:

Simulation Implementation of memcpy

void* my_memcpy(void* dest, const void* sour,int num)
{
	assert(dest && sour);
    void* ret= dest;
	while (num--)
	{
		*(char*)dest = *(char*)sour;
		dest = (char*)dest + 1;
		sour = (char*)sour + 1;
	}
    return ret;
}

Simulation Implementation of memmove

void* my_memcpy(void* dest, const void* sour,int num)
{
	assert(dest && sour);
    void ret = dest;
	if (dest < sour)
	{
		while (num--)
		{
			*(char*)dest = *(char*)sour;
			dest = (char*)dest + 1;
			sour = (char*)sour + 1;
		}
	}
	else
	{
		while (num--)
		{
			*((char*)dest+num) = *((char*)sour+num);
		}
	}
    return ret;
}

In the same point, the functions of memmove and memcpy are basically the same, and both can copy memory in bytes.

Difference: memmove() can copy overlapping memory

The memcpy () c standard does not require it to copy overlapping memory. vs2019memcpy() can copy overlapping memory.

Analog implementation of memset

void* my_memset(void* dest, int c, size_t count)
{
	assert(dest);
	void* ret = dest;
	while (count--)
	{
		*(char*)dest = (char)c;
		dest = (char*)dest + 1;
	}
	return ret;
}

Simulation Implementation of memcmp

int my_memcmp(const void* str1, const void* str2,int num)
{
    assert(str1 && str2);
	while (*(char*)str1 == *(char*)str2 && num>0)
	{
		str1 = (char*)str1 + 1;
		str2 = (char*)str2 + 1;
		num--;
	}
	if (num == 0)
	{
		return 0;
	}
	else if (*(char*)str1 > *(char*)str2)
	{
		return 1;
	}
	else
	{
		return -1;
	}
}

last

If you have any questions about this chapter, please communicate with me.

If there are mistakes in this chapter, you are welcome to point them out. I am very grateful.

If you feel fruitful, you are welcome to praise and comment. Thank you.

Topics: C C++ Back-end