重复造*系列--字符串处理(C语言)

时间:2022-04-27 06:01:10

这些字符代码是以前写的,源于很久很久以前的一个VC++项目,在当时的部门编程比赛里因为用了项目代码的xsplit函数,万万没想到,那个做了几年的项目里面居然有坑。。xsplit函数居然不能split连续2个空格,囧,领导说,你要是用ruby你就满分了,让我与满分失之交臂,当时没有人得满分,因此记忆深刻;

后来又是boost C++库流行,这个我就不说了,用过的都说好,但是也有些小麻烦,就是用的多了,编译就特别慢,那个时候还不知道用incredbuild,于是乎就在代码上下功夫了。

做了一些常用的字符操作,基本上python string的函数基本实现了,放在博客里,也可以温故知新。

xstring.h

 #ifndef XSTRING
#define XSTRING typedef struct xstring {
char *str;
struct xstring *next;
} xstring; //////////////////////////////////////////////////////////////////////////
void* allocate(size_t size); #ifdef USE_STD_MALLOC
#define dellocate(ptr) free(ptr);
#else
#define dellocate(ptr) mem_ret(ptr);
#endif //////////////////////////////////////////////////////////////////////////
xstring* xstring_new(size_t size);
void xstring_delete(xstring **head);
int xstring_size(xstring *head); //////////////////////////////////////////////////////////////////////////
size_t count(char* src, char* dst);
char* replace(char *src, char *old_val, char *new_val);
xstring* split(char *str, char *delimter);
char* strip(char *str);
char* lstrip(char *str);
char* rstrip(char *str);
int start_with(char *str, char *sym);
int end_with(char *str, char *sym);
char* uniq_seq_repeat_chars(char *str); #endif

xstring.c

 #include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <assert.h>
#include "xstring.h"
#include "mem_pool.h" void* allocate(size_t size) {
#ifdef USE_STD_MALLOC
return malloc(size);
#else
return mem_get(size);
#endif
} xstring* xstring_new(size_t size) {
xstring *s = (xstring *)allocate(sizeof(xstring));
if (!s) return NULL; s->str = (char *)allocate(size+);
if (!s->str) {
dellocate(s);
return NULL;
} s->next = NULL;
return s;
} void xstring_delete(xstring** head) {
xstring *curr = *head;
xstring *next; while(curr) {
next = curr->next;
if (curr->str) dellocate(curr->str);
dellocate(curr);
curr = next;
}
*head = NULL;
return;
} int xstring_size(xstring* head) {
int size = ;
while (head) {
size++;
head = head->next;
}
return size;
} static void string_copy(char *dst, char *src, int len) {
if (!dst || !src) return;
strncpy(dst, src, len);
dst[len] = '\0';
return;
} static void substring_copy(xstring **curr, xstring *next, char *str, int len) {
string_copy(next->str, str, len);
(*curr)->next = next;
(*curr) = (*curr)->next;
} xstring* split(char* str, char *delimter) {
char *delimt, *buffer;
int i = ;
int len, match, cnt;
xstring *head = NULL;
xstring *next, *curr; if (NULL == str || NULL == delimter) return NULL; delimt = delimter;
len = strlen(delimter);
buffer = str;
match = ;
cnt = ; while (*buffer != '\0') {
if (*buffer == *delimt) {
delimt++;
match++;
} else if (*buffer != *delimt) {
delimt = delimter;
} if (match == len) {
if (NULL == head && cnt > ) {
head = xstring_new(cnt);
if (!head) return NULL;
string_copy(head->str, str+i-cnt, cnt+-len);
curr = head;
} else if (cnt > ){
next = xstring_new(cnt);
if (!next) return NULL;
substring_copy(&curr, next, str+i-cnt, cnt+-len);
}
cnt = ;
match = ;
delimt = delimter;
} else {
cnt++;
}
i++;
buffer++;
} if (cnt > ) {
next = xstring_new(cnt);
if (!next) return NULL;
substring_copy(&curr, next, str+i-cnt, cnt);
}
return head;
} size_t count(char* src, char* dst) {
size_t cnt = ;
char *buf = src;
char *tmp = dst;
int sum = ;
size_t len = strlen(dst); while (*buf != '\0') {
if (*buf == *tmp) {
sum++;
tmp++;
} else {
tmp = dst;
sum = ;
} if (sum == len) {
tmp = dst;
sum = ;
cnt++;
}
buf++;
}
return cnt;
} char* replace(char *src, char *old_val, char *new_val) {
if (!src || !old_val || !new_val) return NULL;
size_t cnt = count(src, old_val);
if ( == cnt) return NULL; size_t o_len = strlen(old_val);
size_t n_len = strlen(new_val);
size_t len = strlen(src) + cnt * (n_len - o_len) + ; char *new_str = (char *)allocate(len);
if (!new_str) return NULL; char *str = new_str;
char *buf = src;
char *tmp = old_val;
int sum = ; while (*buf != '\0') {
if (*buf == *tmp) {
sum++;
tmp++;
} else if (sum > ){
tmp = old_val;
sum = ;
} if (sum == ) *new_str++ = *buf;
if (sum == o_len) {
tmp = old_val;
sum = ;
for (size_t i=; i<n_len; i++) *new_str++ = new_val[i];
}
buf++;
} for(; sum>; sum--) *new_str++ = *(buf-sum);
*(str+len-) = '\0'; return str;
} char* lstrip(char *str) {
if (NULL == str) return NULL; char *tmp = str;
int i = ; while (isspace(*tmp++)) i++;
int len = strlen(str) - i; char *s = (char *)allocate(len + );
if (NULL == s) return NULL; string_copy(s, str+i, len);
return s;
} char* rstrip(char *str) {
if (NULL == str) return NULL;
char *tmp = str;
int len = strlen(str) - ; while (isspace(*(str+len))) len--; char *s = (char *)allocate(len + );
if (NULL == s) return NULL; string_copy(s, str, len+);
return s;
} char* strip(char *str) {
if (NULL == str) return NULL; char *tmp = str;
int len = strlen(str)-;
int i = ; while(isspace(*tmp++)) i++;
while(isspace(*(str+len))) len--; char *s = (char *)allocate(len-i+);
if (NULL == s) return NULL; string_copy(s, str+i, len-i+);
return s;
} int start_with(char *str, char *sym) {
if (!str || !sym || == strlen(sym)) return -;
return strncmp(str, sym, strlen(sym));
} int end_with(char *str, char *sym) {
if (!str || !sym) return -;
int l_len = strlen(str);
int r_len = strlen(sym); if (l_len < r_len ) return -;
return strncmp(str+l_len-r_len, sym, r_len);
} int max(int *in_arr, int len) {
assert(NULL != in_arr);
int m = in_arr[];
for(int i=; i<len; i++)
if (m < in_arr[i]) m = in_arr[i];
return m;
} char *uniq_seq_repeat_chars(char *str) {
if (NULL == str) return NULL; int rec[] = {};
char *s = (char *)allocate(strlen(str)+);
if (NULL == s) return NULL;
char *s1 = s; for (int i=; i<(int)strlen(str); i++) {
char ch = str[i];
if (rec[ch] == || != i-rec[ch]) {
*s = ch;
s++;
}
rec[ch] = i;
}
*s = '\0';
return s1;
}