clang 简单的str_replace实现
自己写的一段:
//gool
char* str_replace(char* source, const char* find, const char* replace){ if (source == NULL || find == NULL || find == "")
return strdup(source); int matchCount = 0;
int nowIndex = 0;
int findLength = strlen(find);
int replaceLength = strlen(replace);
int sourceLength = strlen(source);
int resultLength = sourceLength;
char* result;
int i;
for ( i = 0; i < sourceLength; i++)
{
if (nowIndex < findLength && source[i] == find[nowIndex]){
nowIndex++;
matchCount++;
}
else
{
if (matchCount == findLength)
{
source[i - 1] = '\0';
nowIndex = 0;
matchCount = 0;
resultLength -= findLength - replaceLength;
}
}
} nowIndex = 0;
matchCount = 0; result = (char*)malloc(2 * (resultLength + 1)); for ( i = 0; i < sourceLength; i++)
{
if (source[i] == '\0'){
source[i] = find[0];
while (matchCount < replaceLength)
{
*(result + nowIndex + matchCount) = replace[matchCount];
matchCount++;
}
matchCount = 0;
nowIndex += replaceLength;
i += findLength -1;
}
else
{
*(result + nowIndex) = source[i];
nowIndex++;
}
}
*(result + nowIndex + 1) = '\0';
return result;
}
http://blog.csdn.net/glmmmm/article/details/9930969:
char *str_replace_2(const char *string, const char *substr, const char *replacement)
{
char *tok = NULL;
char *newstr = NULL;
char *oldstr = NULL; /* if either substr or replacement is NULL, duplicate string a let caller handle it */
if (substr == NULL || replacement == NULL)
return strdup(string); newstr = strdup(string);
while ((tok = strstr(newstr, substr)))
{
oldstr = newstr;
newstr = (char*)malloc(strlen(oldstr) - strlen(substr) + strlen(replacement) + 1);
/*failed to alloc mem, free old string and return NULL */
if (newstr == NULL)
{
free(oldstr);
return NULL;
}
memcpy(newstr, oldstr, tok - oldstr);
memcpy(newstr + (tok - oldstr), replacement, strlen(replacement));
memcpy(newstr + (tok - oldstr) + strlen(replacement), tok + strlen(substr), strlen(oldstr) - strlen(substr) - (tok - oldstr));
memset(newstr + strlen(oldstr) - strlen(substr) + strlen(replacement), 0, 1); free(oldstr);
} return newstr;
}
http://stackoverflow.com/questions/779875/what-is-the-function-to-replace-string-in-c:
char *str_replace_3(char *orig, char *rep, char *with) {
char *result; // the return string
char *ins; // the next insert point
char *tmp; // varies
int len_rep; // length of rep
int len_with; // length of with
int len_front; // distance between rep and end of last rep
int count; // number of replacements
if (!orig)
return NULL;
if (!rep)
rep = "";
len_rep = strlen(rep);
if (!with)
with = "";
len_with = strlen(with);
ins = orig;
for (count = 0; tmp = strstr(ins, rep); ++count) {
ins = tmp + len_rep;
}
// first time through the loop, all the variable are set correctly
// from here on,
// tmp points to the end of the result string
// ins points to the next occurrence of rep in orig
// orig points to the remainder of orig after "end of rep"
tmp = result = (char*)malloc(strlen(orig) + (len_with - len_rep) * count + 1);
if (!result)
return NULL;
while (count--) {
ins = strstr(orig, rep);
len_front = ins - orig;
tmp = strncpy(tmp, orig, len_front) + len_front;
tmp = strcpy(tmp, with) + len_with;
orig += len_front + len_rep; // move to next "end of rep"
}
strcpy(tmp, orig);
return result;
}
平均测速:1. 84 clock/100000,2. 195 clock/100000,3.89 clock/100000
clang 简单的str_replace实现的更多相关文章
- php将中文符号全部替换为英文符号
php将中文符号全部替换为英文符号 一.总结 一句话总结:可以用简单替换和规律替换 简单替换 str_replace() 规律替换 均相差 65248 方法一:简单替换(php代码) $val1=st ...
- 深入研究Clang(四) Clang编译器的简单分析
作者:史宁宁(snsn1984) 首先我们确定下Clang编译器的详细内容和涵盖范围.之前在<LLVM每日谈之二十 Everything && Clang driver>中 ...
- 简单管理员权限与几个常用的PHP 常用函数,in_array(),explode(),implode(),join(),str_replace()
先把今天要用的几个函数罗列出来: //explode()转换成数组,implode()转化成字符串 explode("分隔符",需要被分割的字符串或变量) $priv=" ...
- 【004: gcc 和 clang 的简单比较】
- 应用OpenMP的一个简单的设计模式
小喵的唠叨话:最近很久没写博客了,一是因为之前写的LSoftmax后馈一直没有成功,所以在等作者的源码.二是最近没什么想写的东西.前两天,在预处理图片的时候,发现处理200w张图片,跑了一晚上也才处理 ...
- 基于Clang的Source to Source源代码转换(一)
Clang中包含了非常多的关于抽象语法树(AST)的访问和操作的类和接口.我们程序开发人员可以直接通过继承其中的某些类,重写其中的关键成员方法,从而形成我们自己的对抽象语法树的操作. 那么,首先我们简 ...
- windows平台下基于VisualStudio的Clang安装和配置
LLVM 是一个开源的编译器架构,它已经被成功应用到多个应用领域.Clang是 LLVM 的一个编译器前端,它目前支持 C, C++, Objective-C 以及 Objective-C++ 等编程 ...
- clang编译器简介
本文部分内容引用: 中文维基百科. 结构化编译器前端--clang介绍. 什么是clang编译器? clang是LLVM编译器工具集的一个用于编译C.C++.Objective-C的前端.LLVM项目 ...
- 浅谈 block(1) – clang 改写后的 block 结构
这几天为了巩固知识,从 iOS 的各个知识点开始学习,希望自己对每一个知识理解的更加深入的了解.这次来分享一下 block 的学习笔记. block 简介 block 被当做扩展特性而被加入 GCC ...
随机推荐
- AngularJs自定义指令详解(10) - 执行次序
代码: <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8 ...
- Flashback for MySQL 5.7
实现原理 flashback的概念最早出现于Oracle数据库,用于快速恢复用户的误操作. flashback for MySQL用于恢复由DML语句引起的误操作,目前不支持DDL语句.例如下面的语句 ...
- poj1014(还需要改动)
#include <stdio.h> int n[6]; int main() { freopen("in.txt","r",stdin); int ...
- mysql 中文乱码解决方法
最近在.NET 项目中用EF连接mysql,插入中文数据时老是显示乱码,在创建表时都已将编码指定了,但是还是出现乱码,折腾了一阵子才发现在连接字符串里面也要加上指定编码 Character Set=u ...
- SQL 重置自增列的值 批量处理
Declare @IdentityTable sysname, @IdentityColumn sysname, @TotalRows int, @i int, @Iden int, @Sql var ...
- 多层嵌套ajax同步
方式一: $.ajax({ type : "post", url : "user/add", data : data, async : false, //必须为 ...
- UITextField使用详解
转iOS中UITextField使用详解 (1) //初始化textfield并设置位置及大小 UITextField *text = [[UITextField alloc]initWithFr ...
- Android应用:StatusBar状态栏、NavigationBar虚拟按键栏、ActionBar标题栏、Window屏幕内容区域等的宽高
一.屏幕中各种栏目以及屏幕的尺寸 当我们需要计算屏幕中一些元素的高度时,或许需要先获取到屏幕或者各种栏目的高度,下面这个类包含了Status bar状态栏,Navigation bar虚拟按键栏,Ac ...
- AngularJS是什么
先标明来源: https://code.angularjs.org/1.3.15/docs/guide/introduction 也就是官网针对1.3.15版的说明 What Is Angular? ...
- Mini projects #4 ---- Pong
课程全名:An Introduction to Interactive Programming in Python,来自 Rice University 授课教授:Joe Warren, Scott ...