大家好,欢迎来到IT知识分享网。
供参考学习~
stringUtil.h
#ifndef _STRINGUTIL_H #define _STRINGUTIL_H #define true 1 #define false 0 typedef char* String; typedef char Array_t; typedef unsigned char Bool; typedef struct { char* (*addExtra)(char*, char*); char* (*add)(char*, char*); char* (*newString)(int, ...); void (*delString)(char*); int (*split)(char*, char*, Array_t*); int (*splitExtra)(char*, char*, Array_t*); void (*delArray)(Array_t, int); char* (*toUpper)(char*); char* (*toLower)(char*); Bool (*startWith)(char*, char*); Bool (*endWith)(char*, char*); char* (*join)(Array_t, int); char* (*strip)(char*, char*); }STRINGUTIL; extern STRINGUTIL StringUtil; void stringUtilTest(void); #endif
stringUtil.c
#include "stdio.h" #include "stdlib.h" #include "string.h" #include "ctype.h" #include "stdarg.h" #include "stringUtil.h" static char* addExtra(char* p1, char* p2); static char* add(char* p1, char* p2); static char* newString(int num, ...); static void delString(char* p); static int split(char* buff, char* separator, Array_t* result); static int splitExtra(char* buff, char* separator, Array_t* result); static void delArray(Array_t p, int n); static char* toUpper(char* ptr); static char* toLower(char* ptr); static Bool startWith(char* src, char* str); static Bool endWith(char* src, char* str); static char* join(Array_t ptr, int n); static char* strip(char* ptr, char* separator); STRINGUTIL StringUtil = {.add=add, .addExtra=addExtra, .newString=newString, .delString=delString, .split=split, .splitExtra=splitExtra, .delArray=delArray, .toUpper=toUpper, .toLower=toLower, .startWith=startWith, .endWith=endWith, .join=join, .strip=strip }; / * @description: 字符串合并 * @param {p1} 字符串1 * @param {p2} 字符串2 * @return {*} 合并后的字符串指针p1 * @attention 会释放p1,所以调用时等号左边要有,不能省略,否则用的是已经释放的旧地址。如:str = stringUtil.add(p1,p2) */ static char* addExtra(char* p1, char* p2) { char* ptr = NULL; ptr = (char*)realloc(p1, strlen(p1)+strlen(p2)+1); if (ptr != NULL) { strcat(ptr, p2); } return ptr; } / * @description: 字符串合并 * @param {p1} 字符串1 * @param {p2} 字符串2 * @return {*} 合并后的字符串指针p1 * @attention 会创建一个新的字符串返回 */ static char* add(char* p1, char* p2) { char* ptr = NULL; ptr = (char*)calloc(strlen(p1)+strlen(p2)+1, 1); if (ptr != NULL) { strcat(ptr, p1); strcat(ptr, p2); } return ptr; } / * @description: 创建字符串 * @param {num} 字符串数组的个数 * @param {...} 多个字符串数组 * @return {*} 创建完的字符串指针 * @attention 需要调用delString()手动释放ptr */ static char* newString(int num, ...) { char *arg = NULL; va_list ap; int length = 0; va_start(ap, num); for (int i = 0; i < num; i++) { arg = va_arg(ap, char*); length += strlen(arg); } // va_end(ap); char* ptr = (char*)calloc(length+1, sizeof(char)); if (ptr != NULL) { va_start(ap, num); for (int i = 0; i < num; i++) { arg = va_arg(ap, char*); strcat(ptr, arg); } va_end(ap); }else { printf("malloc failed\r\n"); } return ptr; } / * @description: 释放一维指针的内存 * @param {p} 一维指针 * @return {*} 无 */ static void delString(char* p) { free(p); p = NULL; } / * @description: 释放二维指针的内存 * @param {p} 二维指针 * @param {n} 第一维的数量 * @return {*} 无 * @attention 使用本函数可释放调用split()函数后的二维指针的内存 */ static void delArray(Array_t p, int n) { for(int i=0; i
免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://haidsoft.com/45993.html