strlen

Where is the implementation of strlen() in GCC?

拜拜、爱过 提交于 2019-11-30 04:50:37
Can anyone point me to the definition of strlen() in GCC? I've been grepping release 4.4.2 for about a half hour now (while Googling like crazy) and I can't seem to find where strlen() is actually implemented. You should be looking in glibc, not GCC -- it seems to be defined in strlen.c -- here's a link to strlen.c for glibc version 2.7 ... And here is a link to the glibc SVN repository online for strlen.c . The reason you should be looking at glibc and not gcc is: The GNU C library is used as the C library in the GNU system and most systems with the Linux kernel. I realize this question is

strlen in the C preprocessor?

六月ゝ 毕业季﹏ 提交于 2019-11-30 04:20:52
Is it possible to implement strlen() in the C preprocessor? Given: #define MYSTRING "bob" Is there some preprocessor macro, X , which would let me say: #define MYSTRING_LEN X(MYSTRING) It doesn't use the preprocessor, but sizeof is resolved at compile time. If your string is in an array, you can use that to determine its length at compile time: static const char string[] = "bob"; #define STRLEN(s) (sizeof(s)/sizeof(s[0])) Keep in mind the fact that STRLEN above will include the null terminator, unlike strlen() . Yes: #define MYSTRING_LEN(s) strlen(s) In most compilers, this will produce a

can I count on my compiler to optimize strlen on const char *?

老子叫甜甜 提交于 2019-11-30 01:06:00
问题 In my SAX xml parsing callback (XCode 4, LLVM), I am doing a lot of calls to this type of code: static const char* kFoo = "Bar"; void SaxCallBack(char* sax_string,.....) { if ( strcmp(sax_string, kFoo, strlen(kFoo) ) == 0) { } } Is it safe to assume that strlen(kFoo) is optimized by the compiler? (The Apple sample code had pre-calculated strlen(kFoo), but I think this is error prone for large numbers of constant strings.) Edit: Motivation for optimizing: parsing my SVG map on iPod touch 2G

汉字截断问题

孤人 提交于 2019-11-30 00:24:19
//判断汉字截断 #include <windows.h> #include <iostream> #include <string> using namespace std; #pragma comment(lib,"user32.lib") bool IsHanZiCorrupted( string &SeriesRecvString ) { DWORD len=strlen( SeriesRecvString.c_str() ); DWORD i=0; while( i<len) { if( IsDBCSLeadByte( SeriesRecvString[i] ) ) { i+=2; if( i> len ) { return true; } } else { ++i; } } return false; } int main () { //0x81:汉字内码扩展规范(GBK) 1stByte :0x81~0xFE; 2ndByte:0x40~0x7E; //if( (unsigned char)SeriesRecvString[i]>=0x81 ) 一定要转成无符号数才能正确比较 string str("c鑯d苦e命f三g国h无i双j楚k王l好m细n腰o宫p中q多r饿s死敌"); cout<<"str=\""<<str<<"\""<<endl; cout<<"len()=

随机生成字母加数字

半城伤御伤魂 提交于 2019-11-30 00:04:17
//$lenght 生成的字符串长度function character($length = 10, $alphabet = 'AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz0123456789'){ mt_srand(); // 重复字母表以防止生成长度溢出字母表长度 if ($length >= strlen($alphabet)) { $rate = intval($length / strlen($alphabet)) + 1; $alphabet = str_repeat($alphabet, $rate); } // 打乱顺序返回 return substr(str_shuffle($alphabet), 0, $length); } 来源: https://www.cnblogs.com/junyi-bk/p/11541091.html

Why does glibc's strlen need to be so complicated to run quickly?

a 夏天 提交于 2019-11-29 18:36:09
I was looking through the strlen code here and I was wondering if the optimizations used in the code are really needed? For example, why wouldn't something like the following work equally good or better? unsigned long strlen(char s[]) { unsigned long i; for (i = 0; s[i] != '\0'; i++) continue; return i; } Isn't simpler code better and/or easier for the compiler to optimize? The code of strlen on the page behind the link looks like this: /* Copyright (C) 1991, 1993, 1997, 2000, 2003 Free Software Foundation, Inc. This file is part of the GNU C Library. Written by Torbjorn Granlund (tege@sics.se

Limit input length of text that contains HTML tags

人走茶凉 提交于 2019-11-29 16:14:37
I have a php web sites in wich I can manages articles. On the Add a new article form, there is a rich-text-box (allows HTML input) that I'd like to limit the character input count. I check on the server side so using the strlen() ­Docs method. The problem is strlen seems to give a number that is way too big. I tried to use html_entity_decode() ­Docs to get the html tags out of the string but still the string length resulting seems to be wrong. html_entity_decode only decodes HTML entities, it doesn't ignore HTML tags. Try: strlen(strip_tags(html_entity_decode($string))); Or the multi-byte

KMP(应用)

南笙酒味 提交于 2019-11-29 14:07:42
KMP(应用) 【求解字符串的周期(连续重复串)】: POJ 2406 Power Strings KMP自配后,观察 F a i l [ s t r l e n ( S ) ] Fail[strlen(S)] F a i l [ s t r l e n ( S ) ] ,手动模拟,可以发现,如果存在最小单元,那么一定是 S [ 0 到 s t r l e n ( s ) − F a i l [ s t r l e n ( S ) ] ] S[0到strlen(s)-Fail[strlen(S)]] S [ 0 到 s t r l e n ( s ) − F a i l [ s t r l e n ( S ) ] ] 。如果该单元长度是总长度的因数则是合法最小单元,否则该字符串的周期为1. int Fail [ N ] , len ; char s [ N ] ; void pre ( ) { int j = - 1 ; Fail [ 0 ] = - 1 ; for ( int i = 1 ; i < len ; ++ i ) { if ( j >= 0 && s [ j + 1 ] != s [ i ] ) j = Fail [ j ] ; if ( s [ j + 1 ] == s [ i ] ) j ++ ; Fail [ i ] = j ; } } int main ( )

fscanf使用心得

依然范特西╮ 提交于 2019-11-29 12:32:55
  好久没碰C语言了。从现在开始,要开始刷题了。 (1) int fscanf( FILE* stream, const char* format, ... ); https://www.programiz.com/cpp-programming/library-function/cstdio/fscanf (2) strlen(). 头文件<string.h> char c[]={'p','r','\0'} strlen(c)为2,'\0'将不被包括。 来源: https://www.cnblogs.com/shencangzaiyunduan/p/11517859.html

字符串交换函数

半城伤御伤魂 提交于 2019-11-29 11:37:36
字符串内容交换在某些地方还是较为常见的。 void SwapPointer(char *p,char*q) { char temp; int n1=strlen(p); int n2=strlen(q); if(n1>n2) { for(int i=0;i<n2;i++) { temp=p[i]; p[i]=q[i]; q[i]=temp; } for(int i=n2;i<n1;i++) { q[i]=p[i]; p[i]='\0'; } } else { for(int i=0;i<n1;i++) { temp=p[i]; p[i]=q[i]; q[i]=temp; } for(int i=n1;i<n2;i++) { p[i]=q[i]; q[i]='\0'; } } } char s1[]="dddffff"; char s2[]="qweeeee"; 此种是可以的,将字符串内容赋值给s1,s2所指空间中,相当于副本,因而其内容是可以改变的。 char *s1="dddffff"; char *s2="qweeeee"; 以上就不行,将常量字符串的地址赋给s1和s2,其内容是受保护的。可用以下函数: void SwapStr(char **str1,char** str2) { char* temp=*str1; *str1=*str2; *str2=temp; }