Application and simple simulation of common string functions and memory functions

string .h header file defines a variable type, a macro, several string functions and memory operation functions.

Variable type size_t
It is the result of the sizeof keyword. In C language, sizeof() is an operator to judge the length of data type or expression, not a function. It returns the number of memory bytes occupied by an object or type. The return type is size_t. That is, unsigned int
NULL
This macro is the value of a null pointer constant (void *)


The application of string function is relatively common. Here, several string functions and memory operation functions are selected for introduction and simple simulation implementation.

String function

Calculates the length of the string str and returns.

size_t simulate_strlen(const char* str)
{
	assert(src);
	int cnt = 0;
	for ( ;*(str++) != '\0';cnt++);
		return cnt;
}

Copy the source string to the destination string and return the address of the destination string.

char* simulate_strcpy(char* dest, const char* src)
{
	char* tmp = dest;
	assert(dest && src);
	while (*dest++ = *src++);
	return tmp;
}

Copy the first num bytes of the source string to the destination string, and return the address of the destination.
If the copied content is more than the content of the source string, use '\ 0' to supplement. Because strpy is more secure than cppy.

Compare the dictionary order size of the characters at the corresponding positions on str1 and str2 successively until the gap is found or '\ 0' is read. If str1 > str2, returns a number greater than 0. If str1== str2, returns 0. If str1 < str2, returns a number less than 0.

int stimulate_strcmp(const char* s1, const char* s2)
{
	assert(s1 && s2);
	while (*s1 == *s2)
	{
		if (*s1 == '\0')
			return 0;
		s1++; s2++;
	}
	return *s1 - *s2;
}

Compare the dictionary order size of the characters at the corresponding position of the first num bytes on str1 and str2 successively until the gap is found or '\ 0' is read. The return value is the same as strcmp.

After connecting the contents of the source string to the destination string, the last character of the destination string will be overwritten by the first character of the source string, and the '\ 0' in the source will be brought to the destination string.

char* stimulate_strcat(char* dest, const char* src)
{
	char* ret = dest;
	assert(dest && src);
	for (dest += strlen(dest); *src != '\0'; dest++)
	{
		*dest = *src;
		src++;
	}
	*dest = '\0';
	return ret;
}

After connecting the first num bytes of the source string to the destination string, the last character of the destination string will be overwritten by the first character of the source string, and the '\ 0' in the source will be taken to the destination string.

Find the content (substring) of str2 in str1. If found, return the starting address of the content in str1. If not found, return NULL pointer.

const char* stimulate_strstr(const char* str1,const char* str2)
{
	assert(str1 && str2);
	const char* s1 = str1; const char* s2 = str2;
	while (*s1 != '\0')
	{
		if (*s1 == *s2)
		{
			const char* tmp = s1;
			while (*s2 != '\0' && *s1 == *s2)
			{
				s1++;s2++;
			}
			if (*s2 == '\0')
				return tmp;
			else s1 = tmp;
		}
		s1++; s2 = str2;
	}
	return NULL;
}

Decompose str into a set of strings, and the contents in delimiters are separators. This function returns the first substring decomposed. If there is no retrievable string, it returns a null pointer.

The usage of strtok is shown in the figure. (the simulation cannot be realized.)

Some information is specified in C language, and the error code corresponds to the corresponding error information. strerror will return the starting address of the string of error information corresponding to the error code. The error code variable errno should be mentioned here. Errno is a global error code variable. When the calling function generates an error, the compiler will assign the error code generated this time to errno. Therefore, after the error is generated, you can use strerror(errno) to see what the reason is.


After introducing the string function and memory operation function, the operation object is not limited to string, but any data type. Therefore, when passing parameters, the type used is void *, and the return value is also void *, which is convenient for any type of operation.

Memory operation function

Copy the first num bytes of the source string to the destination string. Returns a pointer to destination.

void* stimulate_memcpy(void* dest,const void* src,size_t count)
{
	assert(dest && src);
	void* ret = dest;
	for (size_t i = 0; i < count;i++)
	{
		*(char*)dest = *(char*)src;
		dest = (char*)dest + 1;
		src = (char*)src + 1;
	}
	return ret;
}

Move the first num bytes of the source string to the destination string instead of copying. Compared with memcpy, it solves the problem that the content may overlap and lead to wrong results when copying. Returns a pointer to destination.

void* stimulate_memmove(void* dest, const void* src, size_t count)
{
	void* ret = dest;
	assert(dest && src);
	//Do not create space without creating additional space, so you can copy from the back to the front
	//1 2 3 4 5 6 7 8 9 10
	// However, if you want to put 3 4 5 6 7 into 1 2 3 4 5, it will still overlap, so you need to divide the situation
	//There are two ways to judge
	if (dest < src)
	{
		//From front to back
		while (count--)
		{
			*((char*)dest) = *((char*)src);
			(char*)dest = (char*)dest + 1;
			(char*)src = (char*)src + 1;
		}
	}
	else
	{
		//Copy from back to front
		while (count--)
			*((char*)dest + count) = *((char*)src + count);
	}
	//Second
	if (dest<src || (char*)dest > ((char*)src + count) )
	{
		//From front to back
		while (count--)
		{
			*((char*)dest) = *((char*)src);
			(char*)dest = (char*)dest + 1;
			(char*)src = (char*)src + 1;
		}
	}
	else
	{
		//Copy from back to front
		while (count--)
			*((char*)dest + count) = *((char*)src + count);
	}

	return ret;
}

Compare the dictionary order size of the characters at the corresponding position of the first num bytes on str1 and str2 successively until the gap is found or '\ 0' is read. If str1 > str2, returns a number greater than 0. If str1
==str2, returns 0. If STR1 < str2, returns a number less than 0.

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

Initialize the contents of the first num bytes in the space pointed to by ptr to the value. Can be used to initialize arrays or structures.

epilogue

You can better understand the function only by personally realizing the function of the function.

Keywords: C C++ Algorithm

Added by dajawu on Tue, 08 Mar 2022 07:09:19 +0200