Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
caddy
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
nexedi
caddy
Commits
2ecc8370
Commit
2ecc8370
authored
Feb 24, 2016
by
Matthew Holt
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
templates: .Truncate can truncate from end of string if length is negative
parent
c37ad7f6
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
34 additions
and
4 deletions
+34
-4
middleware/context.go
middleware/context.go
+9
-3
middleware/context_test.go
middleware/context_test.go
+25
-1
No files found.
middleware/context.go
View file @
2ecc8370
...
...
@@ -132,10 +132,16 @@ func (c Context) PathMatches(pattern string) bool {
return
Path
(
c
.
Req
.
URL
.
Path
)
.
Matches
(
pattern
)
}
// Truncate truncates the input string to the given length. If
// input is shorter than length, the entire string is returned.
// Truncate truncates the input string to the given length.
// If length is negative, it returns that many characters
// starting from the end of the string. If the absolute value
// of length is greater than len(input), the whole input is
// returned.
func
(
c
Context
)
Truncate
(
input
string
,
length
int
)
string
{
if
len
(
input
)
>
length
{
if
length
<
0
&&
len
(
input
)
+
length
>
0
{
return
input
[
len
(
input
)
+
length
:
]
}
if
length
>=
0
&&
len
(
input
)
>
length
{
return
input
[
:
length
]
}
return
input
...
...
middleware/context_test.go
View file @
2ecc8370
...
...
@@ -459,12 +459,36 @@ func TestTruncate(t *testing.T) {
inputLength
:
10
,
expected
:
"string"
,
},
// Test 3 - zero length
{
inputString
:
"string"
,
inputLength
:
0
,
expected
:
""
,
},
// Test 4 - negative, smaller length
{
inputString
:
"string"
,
inputLength
:
-
5
,
expected
:
"tring"
,
},
// Test 5 - negative, exact length
{
inputString
:
"string"
,
inputLength
:
-
6
,
expected
:
"string"
,
},
// Test 6 - negative, bigger length
{
inputString
:
"string"
,
inputLength
:
-
7
,
expected
:
"string"
,
},
}
for
i
,
test
:=
range
tests
{
actual
:=
context
.
Truncate
(
test
.
inputString
,
test
.
inputLength
)
if
actual
!=
test
.
expected
{
t
.
Errorf
(
getTestPrefix
(
i
)
+
"Expected
%s, found %s
. Input was Truncate(%q, %d)"
,
test
.
expected
,
actual
,
test
.
inputString
,
test
.
inputLength
)
t
.
Errorf
(
getTestPrefix
(
i
)
+
"Expected
'%s', found '%s'
. Input was Truncate(%q, %d)"
,
test
.
expected
,
actual
,
test
.
inputString
,
test
.
inputLength
)
}
}
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment