MCPcopy Create free account
hub / github.com/vektah/gqlparser / blockStringValue

Function blockStringValue

lexer/blockstring.go:12–48  ·  view source on GitHub ↗

blockStringValue produces the value of a block string from its parsed raw value, similar to Coffeescript's block string, Python's docstring trim or Ruby's strip_heredoc. This implements the GraphQL spec's BlockStringValue() static algorithm.

(raw string)

Source from the content-addressed store, hash-verified

10//
11// This implements the GraphQL spec's BlockStringValue() static algorithm.
12func blockStringValue(raw string) string {
13 lines := strings.Split(raw, "\n")
14
15 commonIndent := math.MaxInt32
16 for _, line := range lines {
17 indent := leadingWhitespace(line)
18 if indent < len(line) && indent < commonIndent {
19 commonIndent = indent
20 if commonIndent == 0 {
21 break
22 }
23 }
24 }
25
26 if commonIndent != math.MaxInt32 && len(lines) > 0 {
27 for i := 1; i < len(lines); i++ {
28 if len(lines[i]) < commonIndent {
29 lines[i] = ""
30 } else {
31 lines[i] = lines[i][commonIndent:]
32 }
33 }
34 }
35
36 start := 0
37 end := len(lines)
38
39 for start < end && leadingWhitespace(lines[start]) == math.MaxInt32 {
40 start++
41 }
42
43 for start < end && leadingWhitespace(lines[end-1]) == math.MaxInt32 {
44 end--
45 }
46
47 return strings.Join(lines[start:end], "\n")
48}
49
50func leadingWhitespace(str string) int {
51 for i, r := range str {

Callers 2

readBlockStringMethod · 0.85
TestBlockStringValueFunction · 0.85

Calls 1

leadingWhitespaceFunction · 0.85

Tested by 1

TestBlockStringValueFunction · 0.68

Used in the wild real call sites across dependent graphs

searching dependent graphs…