1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
|
/***************************************************************
*
* Copyright (C) 1990-2010, Condor Team, Computer Sciences Department,
* University of Wisconsin-Madison, WI.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you
* may not use this file except in compliance with the License. You may
* obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
***************************************************************/
#include "condor_common.h"
#include "str_isxxx.h"
bool
str_isint( const char *s )
{
if ( NULL == s ) {
return false;
}
while( *s ) {
if ( !isdigit(*s) ) {
return false;
}
s++;
}
return true;
}
bool
str_isreal( const char *s, bool strict )
{
if ( NULL == s ) {
return false;
}
bool dot = false;
const char *p = s;
while( *p ) {
bool isdot = ( '.' == *p );
if ( isdot ) {
if ( dot ) {
return false;
}
// Strict mode: can't start with a '.'
else if ( strict && (s == p) ) {
return false;
}
dot = true;
}
else if ( !isdigit(*p) ) {
return false;
}
p++;
// Strict mode: can't end with a '.'
if ( strict && isdot && ('\0' == *p) ) {
return false;
}
}
return true;
}
bool
str_isalpha( const char *s )
{
if ( NULL == s ) {
return false;
}
while( *s ) {
if ( !isalpha(*s) ) {
return false;
}
s++;
}
return true;
}
bool
str_isalnum( const char *s )
{
if ( NULL == s ) {
return false;
}
while( *s ) {
if ( !isalnum(*s) ) {
return false;
}
s++;
}
return true;
}
#if STR_ISXXX_TEST
struct TEST
{
const char *s;
bool is_int;
bool is_real;
bool is_strict;
bool is_alpha;
bool is_alnum;
};
static TEST tests [] =
{
// string, int real strict alpha alnum
{ "1", true, true, true, false, true },
{ "9", true, true, true, false, true },
{ "123", true, true, true, false, true },
{ "1.2", false, true, true, false, false },
{ "1.23", false, true, true, false, false },
{ "12.3", false, true, true, false, false },
{ "12.34", false, true, true, false, false },
{ "12.", false, true, false, false, false },
{ ".12", false, true, false, false, false },
{ "12.3.", false, false, false, false, false },
{ ".12.3", false, false, false, false, false },
{ "1a", false, false, false, false, true },
{ "a1", false, false, false, false, true },
{ "1a2", false, false, false, false, true },
{ "12a", false, false, false, false, true },
{ "12,", false, false, false, false, false },
{ ",12", false, false, false, false, false },
{ "12 ", false, false, false, false, false },
{ " 12", false, false, false, false, false },
{ "abc", false, false, false, true, true },
{ "abc123", false, false, false, false, true },
{ "a1b2", false, false, false, false, true },
{ "abc", false, false, false, true, true },
{ "a b", false, false, false, false, false },
{ NULL, false, false, false, false, false }
};
#define TF(_v_) ( (_v_) ? 'T' : 'F' )
int main( int argc, const char *argv[] )
{
(void) argc;
(void) argv;
int failures = 0;
const TEST *t = tests;
do {
bool is_int = str_isint( t->s );
bool is_real = str_isreal( t->s );
bool is_strict = str_isreal( t->s, true );
bool is_alpha = str_isalpha( t->s );
bool is_alnum = str_isalnum( t->s );
bool ok = ( ( is_int == t->is_int ) &&
( is_real == t->is_real ) &&
( is_strict == t->is_strict ) &&
( is_alpha == t->is_alpha ) &&
( is_alnum == t->is_alnum ) );
printf( "%10s: expect=%c:%c:%c:%c:%c result=%c:%c:%c:%c:%c [%s]\n",
t->s,
TF(t->is_int), TF(t->is_real), TF(t->is_strict),
TF(t->is_alpha), TF(t->is_alnum),
TF(is_int), TF(is_real), TF(is_strict), TF(is_alpha),
TF(is_alnum),
ok ? "OK" : "FAILURE" );
if ( !ok ) {
failures++;
}
t++;
} while( t->s );
if ( failures ) {
printf( "%d tests failed\n", failures );
exit( 0 );
}
printf( "All tests passed\n" );
return 0;
}
#endif
|