MCPcopy Create free account
hub / github.com/cockroachdb/cockroachdb-parser / ParseBool

Function ParseBool

pkg/sql/sem/tree/datum.go:308–349  ·  view source on GitHub ↗

ParseBool parses and returns the boolean value represented by the provided string, or an error if parsing is unsuccessful. See https://github.com/postgres/postgres/blob/90627cf98a8e7d0531789391fd798c9bfcc3bc1a/src/backend/utils/adt/bool.c#L36

(s string)

Source from the content-addressed store, hash-verified

306// string, or an error if parsing is unsuccessful.
307// See https://github.com/postgres/postgres/blob/90627cf98a8e7d0531789391fd798c9bfcc3bc1a/src/backend/utils/adt/bool.c#L36
308func ParseBool(s string) (bool, error) {
309 s = strings.TrimSpace(s)
310 if len(s) >= 1 {
311 switch s[0] {
312 case 't', 'T':
313 if isCaseInsensitivePrefix(s, "true") {
314 return true, nil
315 }
316 case 'f', 'F':
317 if isCaseInsensitivePrefix(s, "false") {
318 return false, nil
319 }
320 case 'y', 'Y':
321 if isCaseInsensitivePrefix(s, "yes") {
322 return true, nil
323 }
324 case 'n', 'N':
325 if isCaseInsensitivePrefix(s, "no") {
326 return false, nil
327 }
328 case '1':
329 if s == "1" {
330 return true, nil
331 }
332 case '0':
333 if s == "0" {
334 return false, nil
335 }
336 case 'o', 'O':
337 // Just 'o' is ambiguous between 'on' and 'off'.
338 if len(s) > 1 {
339 if isCaseInsensitivePrefix(s, "on") {
340 return true, nil
341 }
342 if isCaseInsensitivePrefix(s, "off") {
343 return false, nil
344 }
345 }
346 }
347 }
348 return false, MakeParseError(s, types.Bool, pgerror.New(pgcode.InvalidTextRepresentation, "invalid bool value"))
349}
350
351// ParseDBool parses and returns the *DBool Datum value represented by the provided
352// string, or an error if parsing is unsuccessful.

Callers 2

ParseDBoolFunction · 0.85

Calls 3

NewFunction · 0.92
isCaseInsensitivePrefixFunction · 0.85
MakeParseErrorFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…