Skip to content

Commit

Permalink
utilize strings.Builder for whsp condensation and stack stringification
Browse files Browse the repository at this point in the history
  • Loading branch information
Jesse Coretta authored and Jesse Coretta committed Aug 11, 2024
1 parent b857be1 commit 352afc3
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 46 deletions.
31 changes: 14 additions & 17 deletions misc.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ const (
randIDSize = 24
)

func newStringBuilder() strings.Builder {
return strings.Builder{}
}

func bool2str(b bool) string {
if b {
return `true`
Expand Down Expand Up @@ -139,38 +143,31 @@ This function, when combined with the act of replacing
all newline (ASCII #10, "\n") characters with a single
space, can help with the conversion of a multi-line or
indented "block value" into a single line value more
cleanly. In particular, this will be necessary during
the parsing (marshaling) of text rules into proper ACI
type instances.
cleanly.
*/
func condenseWHSP(b string) (a string) {
// remove leading and trailing
// WHSP/HTAB characters ...
func condenseWHSP(b string) string {
b = trimS(b)

var last bool // previous char was WHSP or HTAB.
var builder strings.Builder

for i := 0; i < len(b); i++ {
c := rune(b[i])
switch c {

// match either whsp OR horizontal tab
case rune(9), rune(32):
case rune(9), rune(32): // match either WHSP or horizontal tab
if !last {
last = !last
a += string(rune(32)) // only add whsp (not htab) for consistency
last = true
builder.WriteRune(rune(32)) // Add WHSP
}

// match all other chars ...
default:
default: // match all other characters
if last {
last = !last
last = false
}
a += string(c)
builder.WriteRune(c)
}
}

return
return builder.String()
}

/*
Expand Down
45 changes: 16 additions & 29 deletions stack.go
Original file line number Diff line number Diff line change
Expand Up @@ -1768,62 +1768,49 @@ processing of a request for string representation of the receiver.
*/
func (r stack) assembleStringStack(str []string, ot string, oc stackType) string {
// Padding char (or lack thereof)
pad := padValue(!r.positive(nspad), ``)
pad := padValue(!r.positive(nspad), "")

builder := newStringBuilder()

var fstr []string
if r.positive(lonce) {
// We're here because Lead-Once was requested,
// so just place the type symbol/word at the
// beginning and don't use it as a join value.
if oc != list {
fstr = append(fstr, ot)
builder.WriteString(ot)
}
for _, val := range str {
builder.WriteString(val)
}

// Append previous content as-is.
fstr = append(fstr, str...)
} else {
// We're here because the user wants a symbol
// or word to appear between every stack val
// OR because the user is stringing a List.
if oc == list {
// Since we're dealing with a simple
// list-style stack, use pad char as
// the join value.
var joinChar string
if ljc := r.getListDelimiter(); len(ljc) > 0 {
fstr = append(fstr, join(str, ljc))
joinChar = ljc
} else {
fstr = append(fstr, join(str, pad))
joinChar = pad
}
builder.WriteString(join(str, joinChar))
} else {
// Use the outerType as the join
// char (e.g.: '&&', '||', 'AND',
// et al).
var tjn string
var char string
if len(r.getSymbol()) > 0 {
if !r.positive(nspad) {
char = string(rune(32))
char = " "
}

sympad := padValue(!r.positive(nspad), char)
j := sympad + ot + sympad
tjn = join(str, j)
} else {
char = string(rune(32)) // by default, use WHSP padding for symbol ops
char = " "
sympad := padValue(true, char)
j := sympad + ot + sympad
tjn = join(str, j)
}
fstr = append(fstr, tjn)
builder.WriteString(tjn)
}
}

// Finally, join the completed slices using the
// pad char, enclose in parenthesis (maybe), and
// condense any consecutive WHSP/HTAB chars down
// to one (1) WHSP char as needed.
fpad := pad + join(fstr, pad) + pad
fpad := pad + builder.String() + pad
result := condenseWHSP(r.paren(fpad))

return result
}

Expand Down

0 comments on commit 352afc3

Please sign in to comment.