Skip to content

Commit

Permalink
strategy pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
ddddddO committed Apr 22, 2022
1 parent 7d327a4 commit e2106d8
Show file tree
Hide file tree
Showing 2 changed files with 165 additions and 157 deletions.
169 changes: 12 additions & 157 deletions node_generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,34 +8,33 @@ import (
)

type rootGenerator struct {
r io.Reader
st spaceType
counter *counter
scanner *bufio.Scanner
strategy nodeGenerateStrategy
}

func newRootGenerator(r io.Reader, st spaceType) *rootGenerator {
return &rootGenerator{
r: r,
st: st,
counter: newCounter(),
scanner: bufio.NewScanner(r),
strategy: newStrategy(st),
}
}

func (rg *rootGenerator) generate() ([]*Node, error) {
var (
scanner = bufio.NewScanner(rg.r)
stack *stack
counter = newCounter()
generateNodeFunc = rg.st.decideGenerateFunc()
roots []*Node
stack *stack
roots []*Node
)

for scanner.Scan() {
currentNode := generateNodeFunc(scanner.Text(), counter.next())
for rg.scanner.Scan() {
currentNode := rg.strategy.generate(rg.scanner.Text(), rg.counter.next())
if err := currentNode.validate(); err != nil {
return nil, err
}

if currentNode.isRoot() {
counter.reset()
rg.counter.reset()
roots = append(roots, currentNode)
stack = newStack()
stack.push(currentNode)
Expand All @@ -49,7 +48,7 @@ func (rg *rootGenerator) generate() ([]*Node, error) {
stack.dfs(currentNode)
}

if err := scanner.Err(); err != nil {
if err := rg.scanner.Err(); err != nil {
return nil, err
}
return roots, nil
Expand All @@ -69,147 +68,3 @@ func (n *Node) validate() error {
}
return nil
}

type generateFunc func(row string, idx uint) *Node

type spaceType int

const (
spacesTab spaceType = iota
spacesTwo
spacesFour
)

func (st spaceType) decideGenerateFunc() generateFunc {
switch st {
case spacesTwo:
return generateFuncTwoSpaces
case spacesFour:
return generateFuncFourSpaces
default:
return generateFuncTab
}
}

// https://ja.wikipedia.org/wiki/ASCII
const (
hyphen = 45
space = 32
tab = 9
)

const (
rootHierarchyNum = uint(1)
)

func generateFuncTab(row string, idx uint) *Node {
var (
text = ""
hierarchy = rootHierarchyNum
)
var (
isPrevChar = false
isRoot = false
)

for i, r := range row {
switch r {
case hyphen:
if i == 0 {
isRoot = true
}
if isPrevChar {
text += string(r)
continue
}
isPrevChar = false
case space:
if isPrevChar {
text += string(r)
continue
}
case tab:
hierarchy++
default: // directry or file text char
text += string(r)
isPrevChar = true
}
}
if !isRoot && hierarchy == rootHierarchyNum {
hierarchy = 0
}

return newNode(text, hierarchy, idx)
}

func generateFuncTwoSpaces(row string, idx uint) *Node {
var (
text = ""
hierarchy = rootHierarchyNum
)
var (
spaceCnt = uint(0)
isPrevChar = false
)

for _, r := range row {
switch r {
case hyphen:
if isPrevChar {
text += string(r)
continue
}
if spaceCnt%2 == 0 {
hierarchy += spaceCnt / 2
}
isPrevChar = false
case space:
if isPrevChar {
text += string(r)
continue
}
spaceCnt++
default: // directry or file text char
text += string(r)
isPrevChar = true
}
}

return newNode(text, hierarchy, idx)
}

func generateFuncFourSpaces(row string, idx uint) *Node {
var (
text = ""
hierarchy = rootHierarchyNum
)
var (
spaceCnt = uint(0)
isPrevChar = false
)

for _, r := range row {
switch r {
case hyphen:
if isPrevChar {
text += string(r)
continue
}
if spaceCnt%4 == 0 {
hierarchy += spaceCnt / 4
}
isPrevChar = false
case space:
if isPrevChar {
text += string(r)
continue
}
spaceCnt++
default: // directry or file text char
text += string(r)
isPrevChar = true
}
}

return newNode(text, hierarchy, idx)
}
153 changes: 153 additions & 0 deletions node_generate_strategy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
package gtree

type spaceType int

const (
spacesTab spaceType = iota
spacesTwo
spacesFour
)

type nodeGenerateStrategy interface {
generate(row string, idx uint) *Node
}

func newStrategy(st spaceType) nodeGenerateStrategy {
switch st {
case spacesTwo:
return &twoSpacesStrategy{}
case spacesFour:
return &fourSpacesStrategy{}
default:
return &tabStrategy{}
}
}

// https://ja.wikipedia.org/wiki/ASCII
const (
hyphen = 45
space = 32
tab = 9
)

const (
rootHierarchyNum = uint(1)
)

type tabStrategy struct{}

func (*tabStrategy) generate(row string, idx uint) *Node {
var (
text = ""
hierarchy = rootHierarchyNum
)
var (
isPrevChar = false
isRoot = false
)

for i, r := range row {
switch r {
case hyphen:
if i == 0 {
isRoot = true
}
if isPrevChar {
text += string(r)
continue
}
isPrevChar = false
case space:
if isPrevChar {
text += string(r)
continue
}
case tab:
hierarchy++
default: // directry or file text char
text += string(r)
isPrevChar = true
}
}
if !isRoot && hierarchy == rootHierarchyNum {
hierarchy = 0
}

return newNode(text, hierarchy, idx)
}

type twoSpacesStrategy struct{}

func (*twoSpacesStrategy) generate(row string, idx uint) *Node {
var (
text = ""
hierarchy = rootHierarchyNum
)
var (
spaceCnt = uint(0)
isPrevChar = false
)

for _, r := range row {
switch r {
case hyphen:
if isPrevChar {
text += string(r)
continue
}
if spaceCnt%2 == 0 {
hierarchy += spaceCnt / 2
}
isPrevChar = false
case space:
if isPrevChar {
text += string(r)
continue
}
spaceCnt++
default: // directry or file text char
text += string(r)
isPrevChar = true
}
}

return newNode(text, hierarchy, idx)
}

type fourSpacesStrategy struct{}

func (*fourSpacesStrategy) generate(row string, idx uint) *Node {
var (
text = ""
hierarchy = rootHierarchyNum
)
var (
spaceCnt = uint(0)
isPrevChar = false
)

for _, r := range row {
switch r {
case hyphen:
if isPrevChar {
text += string(r)
continue
}
if spaceCnt%4 == 0 {
hierarchy += spaceCnt / 4
}
isPrevChar = false
case space:
if isPrevChar {
text += string(r)
continue
}
spaceCnt++
default: // directry or file text char
text += string(r)
isPrevChar = true
}
}

return newNode(text, hierarchy, idx)
}

0 comments on commit e2106d8

Please sign in to comment.