8
8
*
9
9
*
10
10
* IDENTIFICATION
11
- * $PostgreSQL: pgsql/src/backend/utils/adt/bool.c,v 1.45 2009/01/01 17:23:49 momjian Exp $
11
+ * $PostgreSQL: pgsql/src/backend/utils/adt/bool.c,v 1.46 2009/03/09 14:34:34 petere Exp $
12
12
*
13
13
*-------------------------------------------------------------------------
14
14
*/
20
20
#include "libpq/pqformat.h"
21
21
#include "utils/builtins.h"
22
22
23
+ /*
24
+ * Try to interpret value as boolean value. Valid values are: true,
25
+ * false, yes, no, on, off, 1, 0; as well as unique prefixes thereof.
26
+ * If the string parses okay, return true, else false.
27
+ * If okay and result is not NULL, return the value in *result.
28
+ */
29
+ bool
30
+ parse_bool (const char * value , bool * result )
31
+ {
32
+ return parse_bool_with_len (value , strlen (value ), result );
33
+ }
34
+
35
+ bool
36
+ parse_bool_with_len (const char * value , size_t len , bool * result )
37
+ {
38
+ switch (* value )
39
+ {
40
+ case 't' :
41
+ case 'T' :
42
+ if (pg_strncasecmp (value , "true" , len ) == 0 )
43
+ {
44
+ if (result )
45
+ * result = true;
46
+ return true;
47
+ }
48
+ break ;
49
+ case 'f' :
50
+ case 'F' :
51
+ if (pg_strncasecmp (value , "false" , len ) == 0 )
52
+ {
53
+ if (result )
54
+ * result = false;
55
+ return true;
56
+ }
57
+ break ;
58
+ case 'y' :
59
+ case 'Y' :
60
+ if (pg_strncasecmp (value , "yes" , len ) == 0 )
61
+ {
62
+ if (result )
63
+ * result = true;
64
+ return true;
65
+ }
66
+ break ;
67
+ case 'n' :
68
+ case 'N' :
69
+ if (pg_strncasecmp (value , "no" , len ) == 0 )
70
+ {
71
+ if (result )
72
+ * result = false;
73
+ return true;
74
+ }
75
+ break ;
76
+ case 'o' :
77
+ case 'O' :
78
+ /* 'o' is not unique enough */
79
+ if (pg_strncasecmp (value , "on" , (len > 2 ? len : 2 )) == 0 )
80
+ {
81
+ if (result )
82
+ * result = true;
83
+ return true;
84
+ }
85
+ else if (pg_strncasecmp (value , "off" , (len > 2 ? len : 2 )) == 0 )
86
+ {
87
+ if (result )
88
+ * result = false;
89
+ return true;
90
+ }
91
+ break ;
92
+ case '1' :
93
+ if (len == 1 )
94
+ {
95
+ if (result )
96
+ * result = true;
97
+ return true;
98
+ }
99
+ break ;
100
+ case '0' :
101
+ if (len == 1 )
102
+ {
103
+ if (result )
104
+ * result = false;
105
+ return true;
106
+ }
107
+ break ;
108
+ default :
109
+ break ;
110
+ }
111
+
112
+ * result = false; /* suppress compiler warning */
113
+ return false;
114
+ }
115
+
23
116
/*****************************************************************************
24
117
* USER I/O ROUTINES *
25
118
*****************************************************************************/
26
119
27
120
/*
28
121
* boolin - converts "t" or "f" to 1 or 0
29
122
*
30
- * Check explicitly for "true/false" and TRUE/FALSE, 1/0, YES/NO.
31
- * Reject other values. - thomas 1997-10-05
123
+ * Check explicitly for "true/false" and TRUE/FALSE, 1/0, YES/NO, ON/OFF .
124
+ * Reject other values.
32
125
*
33
126
* In the switch statement, check the most-used possibilities first.
34
127
*/
@@ -38,6 +131,7 @@ boolin(PG_FUNCTION_ARGS)
38
131
const char * in_str = PG_GETARG_CSTRING (0 );
39
132
const char * str ;
40
133
size_t len ;
134
+ bool result ;
41
135
42
136
/*
43
137
* Skip leading and trailing whitespace
@@ -50,45 +144,8 @@ boolin(PG_FUNCTION_ARGS)
50
144
while (len > 0 && isspace ((unsigned char ) str [len - 1 ]))
51
145
len -- ;
52
146
53
- switch (* str )
54
- {
55
- case 't' :
56
- case 'T' :
57
- if (pg_strncasecmp (str , "true" , len ) == 0 )
58
- PG_RETURN_BOOL (true);
59
- break ;
60
-
61
- case 'f' :
62
- case 'F' :
63
- if (pg_strncasecmp (str , "false" , len ) == 0 )
64
- PG_RETURN_BOOL (false);
65
- break ;
66
-
67
- case 'y' :
68
- case 'Y' :
69
- if (pg_strncasecmp (str , "yes" , len ) == 0 )
70
- PG_RETURN_BOOL (true);
71
- break ;
72
-
73
- case '1' :
74
- if (pg_strncasecmp (str , "1" , len ) == 0 )
75
- PG_RETURN_BOOL (true);
76
- break ;
77
-
78
- case 'n' :
79
- case 'N' :
80
- if (pg_strncasecmp (str , "no" , len ) == 0 )
81
- PG_RETURN_BOOL (false);
82
- break ;
83
-
84
- case '0' :
85
- if (pg_strncasecmp (str , "0" , len ) == 0 )
86
- PG_RETURN_BOOL (false);
87
- break ;
88
-
89
- default :
90
- break ;
91
- }
147
+ if (parse_bool_with_len (str , len , & result ))
148
+ PG_RETURN_BOOL (result );
92
149
93
150
ereport (ERROR ,
94
151
(errcode (ERRCODE_INVALID_TEXT_REPRESENTATION ),
0 commit comments