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 ...
随机推荐
- tnsnames.ora配置注意(连接新的数据库)
文件地址D:\app\think\product\11.2.0\instantclient_11_2\network\admin\tnsnames.ora# tnsnames.ora Network ...
- 金蝶K/3 Cloud 界面解析过程
服务端 目前也就是iis服务器生成Json描述返回给不同的展现端最解析. 不同的展现端,可以有Silverlight.WPF.Html5.Winform 当然还有IOS和Android端做解析展现 对 ...
- 学霸网站-Beta版本发布说明
项目名称 学霸网站 项目版本 Beta 项目团队 ourteam 发布日期 2015-1-5 一.Alpha版本实现功能简介: 1.匿名提问 2.匿名回答 3.采纳功能 4.登录.注册失败后,用户名等 ...
- ADV数字的剪切
#include <iostream> using namespace std; #define SIZE 9 #define MAXLEN 6 int data[SIZE][MAXLEN ...
- .Net MVC+bootstrap Table学习
一.效果展示 二.使用方法 1).相关css和js的引用 <link href="~/Themes/Bootstrap/css/bootstrap.css" rel=&quo ...
- JAVA编程心得-Eclipse/MyEclipse 中文乱码解决办法
将别人的项目或JAVA文件导入到自己的Eclipse中时,常常会出现JAVA文件的中文注释变成乱码的情况,主要原因就是别人的IDE编码格式和自己的Eclipse编码格式不同.总结网上的建议和自己的体会 ...
- c# 筛选进程命令行,得其ProcessId(唯一标示符,简称pid),再通过pid结束进程
不说别的,上代码 部分using: using System.Diagnostics; using System.Management; 其中要引用System.Management 1.通过筛选Co ...
- jQuery学习-什么是jquery? Js与jquery之间的关系 Jquery选择器
1. 什么是jQuery以及学习的意义等 jQuery是一个js库 JS库是什么? 把常用的方法,进行封装,封装到一个单独的js文件当中,要用的时候直接调用. 学习jQuery主要学什么? 学习jQ ...
- 《Photon》
搭建客户端: using UnityEngine;using System.Collections;using ExitGames.Client.Photon; public class GameCl ...
- Java 第17章 继承
继承的概念 继承机制是面向对象程序设计不可缺少的关键概念,是实现软件可重用的根基, 是提高软件系统的可扩展性与可维护性的主要途径. 所谓继承是指一个类的定义可以基于另外一个已经存在的类,即子类基于父类 ...