#include <string.h>
#include <ctype.h>
#include "l_str.h"
const char * L_BLANKS = " \t\r\n\x8";
// support \r\n line
char * getaline(char * d, char * s)
{
if (d == NULL || s == NULL){
if (d) *d = '\0'; return NULL;
}
char * p = strchr(s, '\n');
if (p == NULL)
p = strchr(s, '\r');
if (p == NULL)
p = s + strlen(s);
// # p -> ('\n' | '\r' | '\0')
int l = p - s;
if (d != s)
memmove(d, s, l + 1);
// # d[l] = ('\n' | '\r' | '\0')
if (d[l]){
// # p -> ('\n' | '\r')
if (d[l-1] == '\r') {
d[l-1] = 0;
}else
d[l] = 0;
p++;
// # p -> new line
}
return p;
}
char * getaline(char * d, char * s, char c)
{
if (d == NULL || s == NULL){
if (d) *d = '\0'; return NULL;
}
while (*s != c && *s) *d++ = *s++;
if (*s == c) s++;
*d = '\0';
return s;
}
char * getaword(char *d, char *s, char c)
{
if (!d || !s){
if (d) *d = '\0'; return NULL;
}
char * p = strchr(s, c);
if (p == NULL)
p = s + strlen(s);
else
p ++;
int l = p - s;
if (l > 0 && s[l-1] == c)
l --;
if (d != s)
memmove(d, s, l);
d[l] = '\0';
return p;
}
// +++
char * getaword(char * d, char * s, const char * seps)
{
if (!d || !s){
if (d) *d = '\0'; return 0;
}
while (*s && strchr(seps, *s)) s++; // skip seperator chars
if (*s){
do {
*d++ = *s++;
} while (*s && strchr(seps, *s) == NULL);
while (*s && strchr(seps, *s)) s++; // skip seperator chars
}
*d = '\0';
return s;
}
// +++
char * getaword(char * d, char * s, const char * seps, const char * ends)
{
if (!d || !s){
if (d) *d = '\0'; return 0;
}
while (*s && strchr(seps, *s)) s++; // skip seperator chars
if (*s){
do {
*d++ = *s++;
} while (*s && strchr(seps, *s) == NULL && strchr(ends, *s) == NULL);
while (*s && strchr(seps, *s) && strchr(ends, *s) == NULL) s++; // skip seperator chars
}
*d = '\0';
return s;
}
// +++
char * skipchs (char * s, char * chars)
{
if (!s) return 0;
while (*s && strchr(chars, *s)) s++;
return s;
}
// +++
char * skipblanks(char * s)
{
while (ISBLANKCH(*s)) s++;
return s;
}
// +++
int trimstr(char * d)
{
if (d == NULL) return -1;
char *r = d;
int i =0;
for (i = 0; *d; d++){
if (!ISBLANKCH(*d))
r[i++] = *d;
else if (i && !ISBLANKCH(r[i-1])){
r[i++] = ' ';
}
}
if (i && r[i-1] == ' ') i--;
r[i] = '\x0';
return i+1;
}
//
int trimstr(char * d, char * s)
{
if (d == NULL) return -1;
if (s == NULL){
*d = '\0'; return 0;
}
bool instring = false; char ch;
int i = 0;
for (i = 0; *s; s++){
if (instring || !ISBLANKCH(*s)){
d[i++] = *s;
if (*s == '\'' || *s == '"'){
if (instring){
if (ch == *s)
instring = false;
}else{
instring = true; ch = *s;
}
}
}else{
if (i){
if (!ISBLANKCH(d[i-1]))
d[i++] = ' ';
if (*s == '\n')
d[i-1] = '\n';
}
}
}
if (i && d[i-1] == ' ') i--;
d[i] = '\x0';
return i+1;
}
int trim_head(char * d, char * s)
{
s = skipblanks(s);
return strmove(d, s);
}
int trim_tail(char * d, char * s)
{
char * p = s + strlen(s) - 1;
while (p >= s && ISBLANKCH(*p)) p--;
int l = int(p + 1 - s);
if (l > 0 && d != s)
memmove(d, s, l);
d[l] ='\0';
return l;
}
int trim_all(char * d, char * s)
{
if (s == 0 || *s == '\0') {
*d = '\0'; return 0;
}
int l;
s = skipblanks(s);
if (*s == '\0'){
l = 0;
}else{
char * e = s + strlen(s) - 1;
while (e >= s && ISBLANKCH(*e)) e--;
l = int(e + 1 - s);
if (l > 0 && d != s)
memmove(d, s, l);
}
d[l] = '\0';
return l;
}
int trim_all(char * d, char * s, char *blanks)
{
if (s == 0 || *s == '\0') {
*d = '\0'; return 0;
}
s = skipchs(s, blanks);
char * e = s + strlen(s) - 1;
while (e >= s && strchr(blanks, *e)) e--;
int l = int(e + 1 - s);
if (l > 0 && d != s)
memmove(d, s, l);
d[l] = '\0';
return l;
}
int trim_all(char * d, char * s, char blank)
{
if (s == 0 || *s == '\0') {
*d = '\0'; return 0;
}
while (*s == blank) s++;
char * e = s + strlen(s) - 1;
while (e >= s && *e == blank) e--;
int l = int(e + 1 - s);
if (l > 0 && d != s)
memmove(d, s, l);
d[l] = '\0';
return l;
}
int trim_all(char * d)
{
return trim_all(d, d);
}
// ++
int endcmp(const char * d, const char * e)
{
if (d == NULL && e == NULL)
return 0;
if ((d == NULL && e != NULL) || (d != NULL && e == NULL))
return -1;
int l0 = strlen(d);
int l1 = strlen(e);
if (l0 < l1)
return -2;
return strcmp(d+l0-l1,e);
}
// ++
int padr(char * d, char * s, int len)
{
int i;
if (d == NULL || s == NULL) return -1;
if (d == s)
i = strlen(s);
else
for (i = 0; i < len && s[i]; i++)
d[i] = s[i];
for (; i < len; i++)
d[i] = ' ';
d[i] = '\0';
return len;
}
// ++
int bcdtoint(char * d, int len)
{
int n = 0;
for (int i = 0; i < len; i += 2)
n = n * 100 + ((d[0] & 0xf0) >> 4) * 10 + (d[0] & 0x0f);
if (len % 2)
n /= 10;
return n;
}
// ++
int strmove(char * d, char * s)
{
if (d == NULL || s == NULL)
return -1;
int l = strlen(s);
if (d != s)
memmove(d, s, l+1);
return l;
}
// ++
int strreplace(char * d, char * s0, char * s1)
{
if (d == NULL || s0 == NULL || s1 == NULL) return -1;
if (*s0 == '\0') return 0;
int l0 = strlen(s0), l1 = strlen(s1);
char * p, * q;
p = q = d;
while (*q && (p = strstr(q, s0)) != NULL){
strmove(p+l1, p+l0);
memcpy(p, s1, l1);
q = p+l1;
}
return 0;
}
int strreplace(char * s0, int l0, char * s1, int l1)
{
if (l1 <= 0) l1 = strlen(s1);
strmove(s0+l1, s0+l0);
memcpy(s0, s1, l1);
return 0;
}
int strinsert(char * d, char * s, int l)
{
if (!d || l == 0 || !s || (l < 0 && *s == '\0')) return 0;
if (l < 0)
l = strlen(s);
strmove(d+l, d);
memcpy(d, s, l);
return l;
}
int countlines(const char * s, char c)
{
if (s == 0 || *s == '\0') return 0;
int i = 0;
for (i = 0; *s; s++)
if (*s == c) i++;
if (i && s[-1] != c)
++i;
return i;
}
int countwords(const char * s, const char * seps)
{
if (s == 0 || *s == '\0') return 0;
if (seps == 0) return -1;
if (*seps == '\0') return 1;
int i = 0;
for (i = 0; *s;){
while (*s && strchr(seps, *s)) s++; // skip seperator chars
if (*s){
i++;
while (*s && strchr(seps, *s) == NULL) s++;
}
}
return i;
}
int splitstr(char * d, char splitchar) // 返回字符串数目
{
char *p; int i;
for (p = d, i = 0; *p; d = p, i++){
if ((p = strchr(d, splitchar)) == 0){
p = d + strlen(d);
++i;
break;
}
*p++ = '\0';
}
if (p[-1] != '\0')
p[1] = '\0';
return i;
}
int splitstr(char * d, char * splitstr)
{
char * p, * q; int i, l0;
l0 = strlen(splitstr);
if (l0 < 1)
return 1;
for (q = p = d, i = 0; *p; d = p, i++){
if ((p = strstr(d, splitstr)) == 0){
q += strmove(q, d) + 1;
p = d + strlen(d);
++i;
break;
}
*p = '\0'; p += l0;
q += strmove(q, d) + 1;
}
*q = '\0';
return i;
}
// 将字符串s(最大长度maxlen)中以p开始的一个子串取到d中,返回下一子串位置,如果最后,则返回0
char * getaword(char * d, char * s, const char *p, int maxlen)
{
if (p >= s) {
return 0;
}
strcpy(d, p);
return (char *)(p + strlen(p) + 1);
}
bool checkmask(const char * data, const char * mask)
{
int i = 0;
for (i = 0; mask[i] && mask[i] != '*'; i++) {
int ch = mask[i], d = data[i];
if (ch == '@') {
if (!isupper(d)) return false;
}else if (ch == '$') {
if (!islower(d)) return false;
}else if (ch == '#'){
if (!isdigit(d)) return false;
}else if (c
没有合适的资源?快使用搜索试试~ 我知道了~
c字符串解析处理功能函数集
共2个文件
h:1个
cpp:1个
3星 · 超过75%的资源 需积分: 0 37 下载量 100 浏览量
2008-09-19
23:40:22
上传
评论
收藏 4KB RAR 举报
温馨提示
提供一些c string操作的功能 充分考虑易用,地址空间的节约等问题. 其中,所有的d为目标地址,s为源地址,d==s时,执行也正确
资源详情
资源评论
资源推荐
收起资源包目录
l_str.rar (2个子文件)
l_str.cpp 8KB
l_str.h 4KB
共 2 条
- 1
liangar
- 粉丝: 2
- 资源: 4
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论1