首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >好的C字符串库

好的C字符串库
EN

Stack Overflow用户
提问于 2011-01-14 12:41:29
回答 3查看 29.5K关注 0票数 34

我最近受到了启发,开始了一个我一直想要编写代码的项目。我想用C语言实现,因为内存处理是这个应用程序的关键。我在C中寻找一个好的字符串实现,因为我知道我自己做可能会导致一些混乱的缓冲区溢出,并且我希望处理相当大量的字符串。

我找到了this的文章,给出了每一个的细节,但它们似乎都有很多缺点(不要误会我,这篇文章非常有帮助,但它仍然让我担心,即使我选择其中一个,我也不会使用我能得到的最好的)。我也不知道这篇文章是如何更新的,所以我现在请求。

我正在寻找的是可以包含大量字符的东西,并简化了在字符串中搜索的过程。如果它允许我以任何方式标记字符串,那就更好了。此外,它还应该有一些相当好的I/O性能。打印和格式化打印并不是最重要的。我知道我不应该期望一个库为我做所有的工作,但我只是想知道是否有一个记录良好的字符串函数可以节省我的时间和一些工作。

任何帮助都是非常感谢的。提前感谢!

编辑:我被问到我更喜欢的许可证。任何类型的开源许可证都可以,但最好是GPL (v2或v3)。

EDIt2:我找到了betterString (bstring)库,它看起来很不错。良好的文档,小而通用的函数数量,并且很容易与c字符串混合。有没有什么好的或者不好的故事?我读过的关于它的唯一缺点是它没有Unicode (同样,读过这篇文章,还没有见过它面对面),但其他一切似乎都很好。

EDIT3:还有,最好是纯C语言。

EN

回答 3

Stack Overflow用户

发布于 2011-01-14 17:48:58

我建议不要使用除mallocfreestrlenmemcpysnprintf之外的任何库。这些函数为您提供了在C中进行强大、安全和高效的字符串处理的所有工具。只要远离strcpystrcatstrncpystrncat,所有这些都会导致低效和可利用的bug。

既然你提到了搜索,无论你选择什么库,strchrstrstr几乎都是你想要使用的。strspnstrcspn也很有用。

票数 12
EN

Stack Overflow用户

发布于 2016-08-14 15:55:08

请查看milkstrings

示例代码:

代码语言:javascript
复制
int main(int argc, char * argv[]) {
  tXt s = "123,456,789" ;
  s = txtReplace(s,"123","321") ; // replace 123 by 321
  int num = atoi(txtEat(&s,',')) ; // pick the first number
  printf("num = %d s = %s \n",num,s) ;
  s = txtPrintf("%s,%d",s,num) ; // printf in new string
  printf("num = %d s = %s \n",num,s) ;
  s = txtConcat(s,"<-->",txtFlip(s),NULL) ; // concatenate some strings
  num = txtPos(s,"987") ; // find position of substring
  printf("num = %d s = %s \n",num,s) ;
  if (txtAnyError()) { //check for errors
    printf("%s\n",txtLastError()) ;
    return 1 ; }
  return 0 ;
  }
票数 2
EN

Stack Overflow用户

发布于 2017-06-02 03:52:21

我最近遇到了这个问题,需要附加数百万个字符的字符串。我最终做了我自己的。

它只是一个C字符数组,封装在一个类中,用于跟踪数组大小和分配的字节数。

与SDS和std::string相比,在以下基准测试下,性能提高了10倍

在…

https://github.com/pedro-vicente/table-string

基准测试

对于Visual Studio 2015,x86调试版本:

代码语言:javascript
复制
| API                   | Seconds           
| ----------------------|----| 
| SDS                   | 19 |  
| std::string           | 11 |  
| std::string (reserve) | 9  |  
| table_str_t           | 1  |  

clock_gettime_t timer;
const size_t nbr = 1000 * 1000 * 10;
const char* s = "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb";
size_t len = strlen(s);
timer.start();
table_str_t table(nbr *len);
for (size_t idx = 0; idx < nbr; ++idx)
{
  table.add(s, len);
}
timer.now("end table");
timer.stop();

EDIT通过在开始时分配字符串all (构造函数参数大小)来实现最高性能。如果使用总大小的一小部分,则性能会下降。具有100个分配的示例:

代码语言:javascript
复制
std::string benchmark append string of size 33, 10000000 times
end str:        11.0 seconds    11.0 total
std::string reserve benchmark append string of size 33, 10000000 times
end str reserve:        10.0 seconds    10.0 total
table string benchmark with pre-allocation of 330000000 elements
end table:      1.0 seconds     1.0 total
table string benchmark with pre-allocation of ONLY 3300000 elements, allocation is MADE 100 times...patience...
end table:      9.0 seconds     9.0 total
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4688041

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档