From 8c843ceefd781a407475e4756a2b6b5cec25a2da Mon Sep 17 00:00:00 2001 From: Matthew Holt Date: Wed, 16 Sep 2015 21:30:56 -0600 Subject: [PATCH] middleware: Add StripExt to Context type for stripping extensions from paths --- middleware/context.go | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/middleware/context.go b/middleware/context.go index 8820c4e5f..ade03e112 100644 --- a/middleware/context.go +++ b/middleware/context.go @@ -131,6 +131,19 @@ func (c Context) Truncate(input string, length int) string { return input } +// StripExt returns the input string without the extension, +// which is the suffix starting with the final '.' character +// but not before the final path separator ('/') character. +// If there is no extension, the whole input is returned. +func (c Context) StripExt(path string) string { + for i := len(path) - 1; i >= 0 && path[i] != '/'; i-- { + if path[i] == '.' { + return path[:i] + } + } + return path +} + // Replace replaces instances of find in input with replacement. func (c Context) Replace(input, find, replacement string) string { return strings.Replace(input, find, replacement, -1)