自己写的一段:

//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实现的更多相关文章

  1. php将中文符号全部替换为英文符号

    php将中文符号全部替换为英文符号 一.总结 一句话总结:可以用简单替换和规律替换 简单替换 str_replace() 规律替换 均相差 65248 方法一:简单替换(php代码) $val1=st ...

  2. 深入研究Clang(四) Clang编译器的简单分析

    作者:史宁宁(snsn1984) 首先我们确定下Clang编译器的详细内容和涵盖范围.之前在<LLVM每日谈之二十 Everything && Clang driver>中 ...

  3. 简单管理员权限与几个常用的PHP 常用函数,in_array(),explode(),implode(),join(),str_replace()

    先把今天要用的几个函数罗列出来: //explode()转换成数组,implode()转化成字符串 explode("分隔符",需要被分割的字符串或变量) $priv=" ...

  4. 【004: gcc 和 clang 的简单比较】

  5. 应用OpenMP的一个简单的设计模式

    小喵的唠叨话:最近很久没写博客了,一是因为之前写的LSoftmax后馈一直没有成功,所以在等作者的源码.二是最近没什么想写的东西.前两天,在预处理图片的时候,发现处理200w张图片,跑了一晚上也才处理 ...

  6. 基于Clang的Source to Source源代码转换(一)

    Clang中包含了非常多的关于抽象语法树(AST)的访问和操作的类和接口.我们程序开发人员可以直接通过继承其中的某些类,重写其中的关键成员方法,从而形成我们自己的对抽象语法树的操作. 那么,首先我们简 ...

  7. windows平台下基于VisualStudio的Clang安装和配置

    LLVM 是一个开源的编译器架构,它已经被成功应用到多个应用领域.Clang是 LLVM 的一个编译器前端,它目前支持 C, C++, Objective-C 以及 Objective-C++ 等编程 ...

  8. clang编译器简介

    本文部分内容引用: 中文维基百科. 结构化编译器前端--clang介绍. 什么是clang编译器? clang是LLVM编译器工具集的一个用于编译C.C++.Objective-C的前端.LLVM项目 ...

  9. 浅谈 block(1) – clang 改写后的 block 结构

    这几天为了巩固知识,从 iOS 的各个知识点开始学习,希望自己对每一个知识理解的更加深入的了解.这次来分享一下 block 的学习笔记. block 简介 block 被当做扩展特性而被加入 GCC ...

随机推荐

  1. PHP从mysql获取数据的方法

    <?php require "database.php"; //读取数据库中的数据 $sql_1 = "SELECT * FROM test_table" ...

  2. ListView显示不同行以及数据重用

    Handling ListViews with Multiple Row Types When you start writing Android Apps it isn’t long before ...

  3. IE6的连接数限制问题

    今天解决了一个bug.看似是UI的bug,最后发现IE的设置问题(严格来说,IE6这么做没有问题,因为HTTP协议的规范如此). 先描述一下问题: 有一个页面管理Job,选中一些Job可以Run,每次 ...

  4. 基于Xenomai和工控机的实时测控系统的研究

    http://www.docin.com/p-1006254643-f6.html

  5. javascript code snippet -- 保留小数点位数

    js1.5以上可以利用toFixed(x) ,可指定数字截取小数点后 x位 for example : //round "original" to two decimals var ...

  6. 接口测试总结<转>

    本文主要分为两个部分: 第一部分:主要从问题出发,引入接口测试的相关内容并与前端测试进行简单对比,总结两者之前的区别与联系.但该部分只交代了怎么做和如何做?并没有解释为什么要做? 第二部分:主要介绍为 ...

  7. PHP-递归扫描目录和删除目录

    (1) 通过递归扫描目录并打印 // php递归扫描目录 function scanMyDir($path){ // 打开目录 $dh = opendir($path); echo '<ul&g ...

  8. Smarty缓存的5个知识点

    (1)页面缓存:整个页面全局的缓存 需要4个步骤: ①开启缓存  $smarty->caching = true; ②设置缓存的生命周期  $smarty->cache_lifetime ...

  9. HTML与CSS基础知识补遗(一)

    开始从零基础系统地学习前端知识了,虽说html和css多少了解一些,但是学着还是能发现很多新大陆.... 一. HTML中head标签 1. <meta>标签: meta标签里是一些基础的 ...

  10. 彻底弄懂js循环中的闭包问题

    来源:http://www.108js.com/article/article1/10177.html?id=899 第一次接触这个问题还是在我刚开始学js的时候,当时就是一头雾水,时隔一年多了,突然 ...