Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:5649 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 64790 invoked by uid 1010); 21 Nov 2003 20:45:55 -0000 Delivered-To: ezmlm-scan-internals@lists.php.net Delivered-To: ezmlm-internals@lists.php.net Received: (qmail 64756 invoked from network); 21 Nov 2003 20:45:54 -0000 Received: from unknown (HELO mail-1.nethere.net) (66.63.128.70) by pb1.pair.com with SMTP; 21 Nov 2003 20:45:54 -0000 Received: (qmail 13156 invoked from network); 21 Nov 2003 20:45:52 -0000 Received: from ppp-207-167-100-199.sndg-pm4-2.dialup.nethere.net (localhost.localdomain [207.167.100.199]) by mail-1.nethere.net with SMTP; 21 Nov 2003 20:45:52 -0000 (envelope-sender ) Organization: The Coeus Group To: internals@lists.php.net Date: Fri, 21 Nov 2003 12:42:19 -0800 User-Agent: KMail/1.4.3 X-I-See: Faces X-Presso: Yes X-DeCSS-Usage: cat title-key scrambled.vob | decss > clear.vob X-DeCSS-Line-1: #define m(i)(x[i]^s[i+84])<< X-DeCSS-Line-2: unsigned char x[5],y,s[2048];main(n){for(read(0,x,5);read(0,s,n=2048);write(1,s,n))if(s[y=s[13]%8+20]/16%4==1){int i=m(1)17^256+m(0)8,k=m(2)0,j=m(4)17^m(3)9^k*2-k%8^8,a=0,c=26;for(s[y]-=16;--c;j*=2)a=a*2^i&1,i=i/2^j&1<<24;for(j=127;++jy)c+=y=i^i/8^i>>4^i>>12,i=i>>8^y<<17,a^=a>>14,y=a^a*8^a<<6,a=a>>8^y<<9,k=s[j],k="7Wo~'G_\216"[k&7]+2^"cr3sfw6v;*k+>/n."[k>>4]*2^k*257/8,s[j]=k^(k&k*2&34)*6^c+~y;}} MIME-Version: 1.0 Message-ID: <200311210525.59473.evan@coeus-group.com> Content-Type: Multipart/Mixed; boundary="------------Boundary-00=_JUYP82W7BQFNR975SEQ4" Subject: [PATCH] proposal for new function (str_type) From: evan@coeus-group.com (Evan Nemerson) --------------Boundary-00=_JUYP82W7BQFNR975SEQ4 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 8bit Hi everyone, I've attached a patch which adds a function that basically wraps the ctype.h functions. It pretty much just lets you check to see if a string contains only a certain set of characters. I think it would be very nice for input validation, particularly for those who fear regular expressions. Here's the prototype: bool str_type(str subject, int type) type is a bitmask created from a bunch of new constants (STR_TYPE_ALNUM, STR_TYPE_ALPHA, STR_TYPE_CNTRL, STR_TYPE_DIGIT, STR_TYPE_GRAPH, STR_TYPE_LOWER, STR_TYPE_PRINT, STR_TYPE_PUNCT, STR_TYPE_SPACE, STR_TYPE_UPPER, STR_TYPE_XDIGIT) If anyone is interested, there's a more exotic version at http://www.coeus-group.com/en/uploads/php_str_type.2.diff which allows as many parameters as you want, each of which can be either one of the above constants or a bitmask. It's not as clean as the attached version, and the attached version is definitely more similar to other functions, but I thought this way would be nice for people who are intimidated by bitmasks. If the attachment doesn't agree with anyone, it's also at http://www.coeus-group.com/en/uploads/php_str_type.1.diff -- Evan Nemerson evan@coeus-group.com -- "The greatest mistake is to imagine that the human being is an autonomous individual. The secret freedom which you can supposedly enjoy under a despotic government is nonsense, because your thoughts are never entirely your own. Philosophers, writers, artists, even scientists, not only need encouragement and an audience, they need constant stimulation from other people. It is almost impossible to think without talking. If Defoe had really lived on a desert island, he could not have written Robinson Crusoe, nor would he have wanted to. Take away freedom of speech, and the creative faculties dry up." -George Orwell --------------Boundary-00=_JUYP82W7BQFNR975SEQ4 Content-Type: text/x-diff; charset="us-ascii"; name="php_str_type.1.diff" Content-Transfer-Encoding: 7bit Content-Disposition: inline; filename="php_str_type.1.diff" Index: ext/standard/basic_functions.c =================================================================== RCS file: /repository/php-src/ext/standard/basic_functions.c,v retrieving revision 1.642 diff -u -r1.642 basic_functions.c --- ext/standard/basic_functions.c 19 Nov 2003 21:10:29 -0000 1.642 +++ ext/standard/basic_functions.c 21 Nov 2003 11:54:53 -0000 @@ -378,6 +378,7 @@ PHP_FE(str_split, NULL) PHP_FE(strpbrk, NULL) PHP_FE(substr_compare, NULL) + PHP_FE(str_type, NULL) #ifdef HAVE_STRCOLL PHP_FE(strcoll, NULL) Index: ext/standard/string.c =================================================================== RCS file: /repository/php-src/ext/standard/string.c,v retrieving revision 1.400 diff -u -r1.400 string.c --- ext/standard/string.c 30 Oct 2003 00:49:33 -0000 1.400 +++ ext/standard/string.c 21 Nov 2003 11:55:04 -0000 @@ -23,6 +23,7 @@ /* Synced with php 3.0 revision 1.193 1999-06-16 [ssb] */ #include +#include #include "php.h" #include "reg.h" #include "php_rand.h" @@ -64,6 +65,19 @@ #define STR_STRSPN 0 #define STR_STRCSPN 1 +#define STR_TYPE_ALNUM 0x01 +#define STR_TYPE_ALPHA 0x02 +#define STR_TYPE_CNTRL 0x04 +#define STR_TYPE_DIGIT 0x08 +#define STR_TYPE_GRAPH 0x10 +#define STR_TYPE_LOWER 0x20 +#define STR_TYPE_PRINT 0x40 +#define STR_TYPE_PUNCT 0x80 +#define STR_TYPE_SPACE 0x100 +#define STR_TYPE_UPPER 0x200 +#define STR_TYPE_XDIGIT 0x400 + + /* {{{ register_string_constants */ void register_string_constants(INIT_FUNC_ARGS) @@ -97,6 +111,18 @@ REGISTER_LONG_CONSTANT("LC_MESSAGES", LC_MESSAGES, CONST_CS | CONST_PERSISTENT); # endif #endif + + REGISTER_LONG_CONSTANT("STR_TYPE_ALNUM", STR_TYPE_ALNUM, CONST_CS | CONST_PERSISTENT); + REGISTER_LONG_CONSTANT("STR_TYPE_ALPHA", STR_TYPE_ALPHA, CONST_CS | CONST_PERSISTENT); + REGISTER_LONG_CONSTANT("STR_TYPE_CNTRL", STR_TYPE_CNTRL, CONST_CS | CONST_PERSISTENT); + REGISTER_LONG_CONSTANT("STR_TYPE_DIGIT", STR_TYPE_DIGIT, CONST_CS | CONST_PERSISTENT); + REGISTER_LONG_CONSTANT("STR_TYPE_GRAPH", STR_TYPE_GRAPH, CONST_CS | CONST_PERSISTENT); + REGISTER_LONG_CONSTANT("STR_TYPE_LOWER", STR_TYPE_LOWER, CONST_CS | CONST_PERSISTENT); + REGISTER_LONG_CONSTANT("STR_TYPE_PRINT", STR_TYPE_PRINT, CONST_CS | CONST_PERSISTENT); + REGISTER_LONG_CONSTANT("STR_TYPE_PUNCT", STR_TYPE_PUNCT, CONST_CS | CONST_PERSISTENT); + REGISTER_LONG_CONSTANT("STR_TYPE_SPACE", STR_TYPE_SPACE, CONST_CS | CONST_PERSISTENT); + REGISTER_LONG_CONSTANT("STR_TYPE_UPPER", STR_TYPE_UPPER, CONST_CS | CONST_PERSISTENT); + REGISTER_LONG_CONSTANT("STR_TYPE_XDIGIT", STR_TYPE_XDIGIT, CONST_CS | CONST_PERSISTENT); } /* }}} */ @@ -4691,6 +4717,50 @@ } /* }}} */ +/* {{{ proto bool str_type(string str, int type) + Find out if a string is composed entirely of the specified type(s) of characters. */ +PHP_FUNCTION(str_type) +{ + char *str; + int str_l, types, count, res; + + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sl", &str, &str_l, &types) == FAILURE) { + RETURN_FALSE; + } + + for ( count=0 ; count