From 01bae63ac1f8bc8636c49510736a7ee91aeb49cc Mon Sep 17 00:00:00 2001 From: Shulhan Date: Thu, 21 Sep 2023 22:02:50 +0700 Subject: all: rename file range to line_range to match with its type The type name is lineRange so the file name should be line_range, not range. --- line_range.go | 96 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ line_range_test.go | 51 +++++++++++++++++++++++++++++ range.go | 96 ------------------------------------------------------ range_test.go | 51 ----------------------------- 4 files changed, 147 insertions(+), 147 deletions(-) create mode 100644 line_range.go create mode 100644 line_range_test.go delete mode 100644 range.go delete mode 100644 range_test.go diff --git a/line_range.go b/line_range.go new file mode 100644 index 0000000..0ad5eab --- /dev/null +++ b/line_range.go @@ -0,0 +1,96 @@ +// SPDX-FileCopyrightText: 2023 M. Shulhan +// SPDX-License-Identifier: GPL-3.0-or-later + +package awwan + +import ( + "strconv" + "strings" +) + +// lineRange contains the start and end of line number. +// For single line, the Start and End must be equal. +// For range that does not have an end, the End value must be zero. +type lineRange struct { + list []linePosition +} + +// parseLineRange parse a line range in the following format, +// +// line-range = start ["-" [end]] *("," line-range) +// +// The start must be greater than zero. +// The end is optional, if its exist it must be equal or greater than start. +// +// The next line-range must not overlap with previous range, otherwise it will +// be skipped. +func parseLineRange(raw string) (lr lineRange) { + raw = strings.TrimSpace(raw) + if len(raw) == 0 { + return lr + } + + var ( + listRange = strings.Split(raw, ",") + + rawPos string + start int64 + end int64 + err error + ok bool + ) + for _, rawPos = range listRange { + rawPos = strings.TrimSpace(rawPos) + if len(rawPos) == 0 { + continue + } + + var pos = strings.Split(rawPos, "-") + if len(pos) > 2 { + // Invalid range. + continue + } + + start, err = strconv.ParseInt(pos[0], 10, 64) + if err != nil || start <= 0 { + continue + } + if len(pos) == 1 { + lr.add(start, start) + continue + } + + if len(pos[1]) == 0 { + ok = lr.add(start, 0) + if ok { + break + } + continue + } + + end, err = strconv.ParseInt(pos[1], 10, 64) + if err != nil || end < start { + continue + } + + lr.add(start, end) + } + return lr +} + +// add new position to line range. +// It will return true if new position is added to the list. +func (lr *lineRange) add(start, end int64) bool { + var pos linePosition + for _, pos = range lr.list { + if start <= pos.start { + return false + } + if start <= pos.end { + return false + } + continue + } + lr.list = append(lr.list, linePosition{start: start, end: end}) + return true +} diff --git a/line_range_test.go b/line_range_test.go new file mode 100644 index 0000000..f7272ef --- /dev/null +++ b/line_range_test.go @@ -0,0 +1,51 @@ +package awwan + +import ( + "testing" + + "github.com/shuLhan/share/lib/test" +) + +func TestParseLineRange(t *testing.T) { + type testCase struct { + raw string + exp lineRange + } + + var cases = []testCase{{ + raw: ``, + }, { + raw: ` 1`, + exp: lineRange{ + list: []linePosition{ + {start: 1, end: 1}, + }, + }, + }, { + raw: ` ,1,`, + exp: lineRange{ + list: []linePosition{ + {start: 1, end: 1}, + }, + }, + }, { + raw: ` ,1,2-1,,-4,-4-6,4-6-,8-10,8-12,10-12,10-,16-,18-20`, + exp: lineRange{ + list: []linePosition{ + {start: 1, end: 1}, + {start: 8, end: 10}, + {start: 16, end: 0}, + }, + }, + }} + + var ( + c testCase + got lineRange + ) + for _, c = range cases { + got = parseLineRange(c.raw) + t.Logf("got.list: %+v", got.list) + test.Assert(t, c.raw, c.exp, got) + } +} diff --git a/range.go b/range.go deleted file mode 100644 index 0ad5eab..0000000 --- a/range.go +++ /dev/null @@ -1,96 +0,0 @@ -// SPDX-FileCopyrightText: 2023 M. Shulhan -// SPDX-License-Identifier: GPL-3.0-or-later - -package awwan - -import ( - "strconv" - "strings" -) - -// lineRange contains the start and end of line number. -// For single line, the Start and End must be equal. -// For range that does not have an end, the End value must be zero. -type lineRange struct { - list []linePosition -} - -// parseLineRange parse a line range in the following format, -// -// line-range = start ["-" [end]] *("," line-range) -// -// The start must be greater than zero. -// The end is optional, if its exist it must be equal or greater than start. -// -// The next line-range must not overlap with previous range, otherwise it will -// be skipped. -func parseLineRange(raw string) (lr lineRange) { - raw = strings.TrimSpace(raw) - if len(raw) == 0 { - return lr - } - - var ( - listRange = strings.Split(raw, ",") - - rawPos string - start int64 - end int64 - err error - ok bool - ) - for _, rawPos = range listRange { - rawPos = strings.TrimSpace(rawPos) - if len(rawPos) == 0 { - continue - } - - var pos = strings.Split(rawPos, "-") - if len(pos) > 2 { - // Invalid range. - continue - } - - start, err = strconv.ParseInt(pos[0], 10, 64) - if err != nil || start <= 0 { - continue - } - if len(pos) == 1 { - lr.add(start, start) - continue - } - - if len(pos[1]) == 0 { - ok = lr.add(start, 0) - if ok { - break - } - continue - } - - end, err = strconv.ParseInt(pos[1], 10, 64) - if err != nil || end < start { - continue - } - - lr.add(start, end) - } - return lr -} - -// add new position to line range. -// It will return true if new position is added to the list. -func (lr *lineRange) add(start, end int64) bool { - var pos linePosition - for _, pos = range lr.list { - if start <= pos.start { - return false - } - if start <= pos.end { - return false - } - continue - } - lr.list = append(lr.list, linePosition{start: start, end: end}) - return true -} diff --git a/range_test.go b/range_test.go deleted file mode 100644 index f7272ef..0000000 --- a/range_test.go +++ /dev/null @@ -1,51 +0,0 @@ -package awwan - -import ( - "testing" - - "github.com/shuLhan/share/lib/test" -) - -func TestParseLineRange(t *testing.T) { - type testCase struct { - raw string - exp lineRange - } - - var cases = []testCase{{ - raw: ``, - }, { - raw: ` 1`, - exp: lineRange{ - list: []linePosition{ - {start: 1, end: 1}, - }, - }, - }, { - raw: ` ,1,`, - exp: lineRange{ - list: []linePosition{ - {start: 1, end: 1}, - }, - }, - }, { - raw: ` ,1,2-1,,-4,-4-6,4-6-,8-10,8-12,10-12,10-,16-,18-20`, - exp: lineRange{ - list: []linePosition{ - {start: 1, end: 1}, - {start: 8, end: 10}, - {start: 16, end: 0}, - }, - }, - }} - - var ( - c testCase - got lineRange - ) - for _, c = range cases { - got = parseLineRange(c.raw) - t.Logf("got.list: %+v", got.list) - test.Assert(t, c.raw, c.exp, got) - } -} -- cgit v1.3