2017-03-13 18:55:02 -04:00
|
|
|
// Copyright 2017 The Hugo Authors. All rights reserved.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2018-11-29 22:32:53 -05:00
|
|
|
// Package strings provides template functions for manipulating strings.
|
2017-03-13 18:55:02 -04:00
|
|
|
package strings
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
2022-05-02 10:07:52 -04:00
|
|
|
"fmt"
|
2017-03-13 18:55:02 -04:00
|
|
|
"html/template"
|
2021-05-03 03:10:06 -04:00
|
|
|
"regexp"
|
2020-07-03 04:34:40 -04:00
|
|
|
"strings"
|
2023-01-31 03:54:34 -05:00
|
|
|
"unicode"
|
2017-03-13 18:55:02 -04:00
|
|
|
"unicode/utf8"
|
|
|
|
|
2022-02-26 04:42:21 -05:00
|
|
|
"github.com/gohugoio/hugo/common/text"
|
2017-06-13 12:42:45 -04:00
|
|
|
"github.com/gohugoio/hugo/deps"
|
|
|
|
"github.com/gohugoio/hugo/helpers"
|
2022-05-25 04:56:14 -04:00
|
|
|
"github.com/gohugoio/hugo/tpl"
|
2024-04-01 22:26:15 -04:00
|
|
|
"github.com/rogpeppe/go-internal/diff"
|
2020-08-27 22:59:20 -04:00
|
|
|
|
2017-06-13 13:07:35 -04:00
|
|
|
"github.com/spf13/cast"
|
2017-03-13 18:55:02 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// New returns a new instance of the strings-namespaced template functions.
|
|
|
|
func New(d *deps.Deps) *Namespace {
|
2023-01-04 12:24:36 -05:00
|
|
|
return &Namespace{deps: d}
|
2017-03-13 18:55:02 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Namespace provides template functions for the "strings" namespace.
|
|
|
|
// Most functions mimic the Go stdlib, but the order of the parameters may be
|
|
|
|
// different to ease their use in the Go template system.
|
|
|
|
type Namespace struct {
|
2023-01-04 12:24:36 -05:00
|
|
|
deps *deps.Deps
|
2017-03-13 18:55:02 -04:00
|
|
|
}
|
|
|
|
|
2020-12-16 06:11:32 -05:00
|
|
|
// CountRunes returns the number of runes in s, excluding whitespace.
|
2022-03-17 17:03:27 -04:00
|
|
|
func (ns *Namespace) CountRunes(s any) (int, error) {
|
2017-03-13 18:55:02 -04:00
|
|
|
ss, err := cast.ToStringE(s)
|
|
|
|
if err != nil {
|
all: Rework page store, add a dynacache, improve partial rebuilds, and some general spring cleaning
There are some breaking changes in this commit, see #11455.
Closes #11455
Closes #11549
This fixes a set of bugs (see issue list) and it is also paying some technical debt accumulated over the years. We now build with Staticcheck enabled in the CI build.
The performance should be about the same as before for regular sized Hugo sites, but it should perform and scale much better to larger data sets, as objects that uses lots of memory (e.g. rendered Markdown, big JSON files read into maps with transform.Unmarshal etc.) will now get automatically garbage collected if needed. Performance on partial rebuilds when running the server in fast render mode should be the same, but the change detection should be much more accurate.
A list of the notable new features:
* A new dependency tracker that covers (almost) all of Hugo's API and is used to do fine grained partial rebuilds when running the server.
* A new and simpler tree document store which allows fast lookups and prefix-walking in all dimensions (e.g. language) concurrently.
* You can now configure an upper memory limit allowing for much larger data sets and/or running on lower specced PCs.
We have lifted the "no resources in sub folders" restriction for branch bundles (e.g. sections).
Memory Limit
* Hugos will, by default, set aside a quarter of the total system memory, but you can set this via the OS environment variable HUGO_MEMORYLIMIT (in gigabytes). This is backed by a partitioned LRU cache used throughout Hugo. A cache that gets dynamically resized in low memory situations, allowing Go's Garbage Collector to free the memory.
New Dependency Tracker: Hugo has had a rule based coarse grained approach to server rebuilds that has worked mostly pretty well, but there have been some surprises (e.g. stale content). This is now revamped with a new dependency tracker that can quickly calculate the delta given a changed resource (e.g. a content file, template, JS file etc.). This handles transitive relations, e.g. $page -> js.Build -> JS import, or $page1.Content -> render hook -> site.GetPage -> $page2.Title, or $page1.Content -> shortcode -> partial -> site.RegularPages -> $page2.Content -> shortcode ..., and should also handle changes to aggregated values (e.g. site.Lastmod) effectively.
This covers all of Hugo's API with 2 known exceptions (a list that may not be fully exhaustive):
Changes to files loaded with template func os.ReadFile may not be handled correctly. We recommend loading resources with resources.Get
Changes to Hugo objects (e.g. Page) passed in the template context to lang.Translate may not be detected correctly. We recommend having simple i18n templates without too much data context passed in other than simple types such as strings and numbers.
Note that the cachebuster configuration (when A changes then rebuild B) works well with the above, but we recommend that you revise that configuration, as it in most situations should not be needed. One example where it is still needed is with TailwindCSS and using changes to hugo_stats.json to trigger new CSS rebuilds.
Document Store: Previously, a little simplified, we split the document store (where we store pages and resources) in a tree per language. This worked pretty well, but the structure made some operations harder than they needed to be. We have now restructured it into one Radix tree for all languages. Internally the language is considered to be a dimension of that tree, and the tree can be viewed in all dimensions concurrently. This makes some operations re. language simpler (e.g. finding translations is just a slice range), but the idea is that it should also be relatively inexpensive to add more dimensions if needed (e.g. role).
Fixes #10169
Fixes #10364
Fixes #10482
Fixes #10630
Fixes #10656
Fixes #10694
Fixes #10918
Fixes #11262
Fixes #11439
Fixes #11453
Fixes #11457
Fixes #11466
Fixes #11540
Fixes #11551
Fixes #11556
Fixes #11654
Fixes #11661
Fixes #11663
Fixes #11664
Fixes #11669
Fixes #11671
Fixes #11807
Fixes #11808
Fixes #11809
Fixes #11815
Fixes #11840
Fixes #11853
Fixes #11860
Fixes #11883
Fixes #11904
Fixes #7388
Fixes #7425
Fixes #7436
Fixes #7544
Fixes #7882
Fixes #7960
Fixes #8255
Fixes #8307
Fixes #8863
Fixes #8927
Fixes #9192
Fixes #9324
2023-12-24 13:11:05 -05:00
|
|
|
return 0, fmt.Errorf("failed to convert content to string: %w", err)
|
2017-03-13 18:55:02 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
counter := 0
|
2022-05-25 04:56:14 -04:00
|
|
|
for _, r := range tpl.StripHTML(ss) {
|
2017-03-13 18:55:02 -04:00
|
|
|
if !helpers.IsWhitespace(r) {
|
|
|
|
counter++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return counter, nil
|
|
|
|
}
|
|
|
|
|
2018-06-04 13:47:03 -04:00
|
|
|
// RuneCount returns the number of runes in s.
|
2022-03-17 17:03:27 -04:00
|
|
|
func (ns *Namespace) RuneCount(s any) (int, error) {
|
2018-06-04 13:47:03 -04:00
|
|
|
ss, err := cast.ToStringE(s)
|
|
|
|
if err != nil {
|
all: Rework page store, add a dynacache, improve partial rebuilds, and some general spring cleaning
There are some breaking changes in this commit, see #11455.
Closes #11455
Closes #11549
This fixes a set of bugs (see issue list) and it is also paying some technical debt accumulated over the years. We now build with Staticcheck enabled in the CI build.
The performance should be about the same as before for regular sized Hugo sites, but it should perform and scale much better to larger data sets, as objects that uses lots of memory (e.g. rendered Markdown, big JSON files read into maps with transform.Unmarshal etc.) will now get automatically garbage collected if needed. Performance on partial rebuilds when running the server in fast render mode should be the same, but the change detection should be much more accurate.
A list of the notable new features:
* A new dependency tracker that covers (almost) all of Hugo's API and is used to do fine grained partial rebuilds when running the server.
* A new and simpler tree document store which allows fast lookups and prefix-walking in all dimensions (e.g. language) concurrently.
* You can now configure an upper memory limit allowing for much larger data sets and/or running on lower specced PCs.
We have lifted the "no resources in sub folders" restriction for branch bundles (e.g. sections).
Memory Limit
* Hugos will, by default, set aside a quarter of the total system memory, but you can set this via the OS environment variable HUGO_MEMORYLIMIT (in gigabytes). This is backed by a partitioned LRU cache used throughout Hugo. A cache that gets dynamically resized in low memory situations, allowing Go's Garbage Collector to free the memory.
New Dependency Tracker: Hugo has had a rule based coarse grained approach to server rebuilds that has worked mostly pretty well, but there have been some surprises (e.g. stale content). This is now revamped with a new dependency tracker that can quickly calculate the delta given a changed resource (e.g. a content file, template, JS file etc.). This handles transitive relations, e.g. $page -> js.Build -> JS import, or $page1.Content -> render hook -> site.GetPage -> $page2.Title, or $page1.Content -> shortcode -> partial -> site.RegularPages -> $page2.Content -> shortcode ..., and should also handle changes to aggregated values (e.g. site.Lastmod) effectively.
This covers all of Hugo's API with 2 known exceptions (a list that may not be fully exhaustive):
Changes to files loaded with template func os.ReadFile may not be handled correctly. We recommend loading resources with resources.Get
Changes to Hugo objects (e.g. Page) passed in the template context to lang.Translate may not be detected correctly. We recommend having simple i18n templates without too much data context passed in other than simple types such as strings and numbers.
Note that the cachebuster configuration (when A changes then rebuild B) works well with the above, but we recommend that you revise that configuration, as it in most situations should not be needed. One example where it is still needed is with TailwindCSS and using changes to hugo_stats.json to trigger new CSS rebuilds.
Document Store: Previously, a little simplified, we split the document store (where we store pages and resources) in a tree per language. This worked pretty well, but the structure made some operations harder than they needed to be. We have now restructured it into one Radix tree for all languages. Internally the language is considered to be a dimension of that tree, and the tree can be viewed in all dimensions concurrently. This makes some operations re. language simpler (e.g. finding translations is just a slice range), but the idea is that it should also be relatively inexpensive to add more dimensions if needed (e.g. role).
Fixes #10169
Fixes #10364
Fixes #10482
Fixes #10630
Fixes #10656
Fixes #10694
Fixes #10918
Fixes #11262
Fixes #11439
Fixes #11453
Fixes #11457
Fixes #11466
Fixes #11540
Fixes #11551
Fixes #11556
Fixes #11654
Fixes #11661
Fixes #11663
Fixes #11664
Fixes #11669
Fixes #11671
Fixes #11807
Fixes #11808
Fixes #11809
Fixes #11815
Fixes #11840
Fixes #11853
Fixes #11860
Fixes #11883
Fixes #11904
Fixes #7388
Fixes #7425
Fixes #7436
Fixes #7544
Fixes #7882
Fixes #7960
Fixes #8255
Fixes #8307
Fixes #8863
Fixes #8927
Fixes #9192
Fixes #9324
2023-12-24 13:11:05 -05:00
|
|
|
return 0, fmt.Errorf("failed to convert content to string: %w", err)
|
2018-06-04 13:47:03 -04:00
|
|
|
}
|
|
|
|
return utf8.RuneCountInString(ss), nil
|
|
|
|
}
|
|
|
|
|
2017-03-13 18:55:02 -04:00
|
|
|
// CountWords returns the approximate word count in s.
|
2022-03-17 17:03:27 -04:00
|
|
|
func (ns *Namespace) CountWords(s any) (int, error) {
|
2017-03-13 18:55:02 -04:00
|
|
|
ss, err := cast.ToStringE(s)
|
|
|
|
if err != nil {
|
all: Rework page store, add a dynacache, improve partial rebuilds, and some general spring cleaning
There are some breaking changes in this commit, see #11455.
Closes #11455
Closes #11549
This fixes a set of bugs (see issue list) and it is also paying some technical debt accumulated over the years. We now build with Staticcheck enabled in the CI build.
The performance should be about the same as before for regular sized Hugo sites, but it should perform and scale much better to larger data sets, as objects that uses lots of memory (e.g. rendered Markdown, big JSON files read into maps with transform.Unmarshal etc.) will now get automatically garbage collected if needed. Performance on partial rebuilds when running the server in fast render mode should be the same, but the change detection should be much more accurate.
A list of the notable new features:
* A new dependency tracker that covers (almost) all of Hugo's API and is used to do fine grained partial rebuilds when running the server.
* A new and simpler tree document store which allows fast lookups and prefix-walking in all dimensions (e.g. language) concurrently.
* You can now configure an upper memory limit allowing for much larger data sets and/or running on lower specced PCs.
We have lifted the "no resources in sub folders" restriction for branch bundles (e.g. sections).
Memory Limit
* Hugos will, by default, set aside a quarter of the total system memory, but you can set this via the OS environment variable HUGO_MEMORYLIMIT (in gigabytes). This is backed by a partitioned LRU cache used throughout Hugo. A cache that gets dynamically resized in low memory situations, allowing Go's Garbage Collector to free the memory.
New Dependency Tracker: Hugo has had a rule based coarse grained approach to server rebuilds that has worked mostly pretty well, but there have been some surprises (e.g. stale content). This is now revamped with a new dependency tracker that can quickly calculate the delta given a changed resource (e.g. a content file, template, JS file etc.). This handles transitive relations, e.g. $page -> js.Build -> JS import, or $page1.Content -> render hook -> site.GetPage -> $page2.Title, or $page1.Content -> shortcode -> partial -> site.RegularPages -> $page2.Content -> shortcode ..., and should also handle changes to aggregated values (e.g. site.Lastmod) effectively.
This covers all of Hugo's API with 2 known exceptions (a list that may not be fully exhaustive):
Changes to files loaded with template func os.ReadFile may not be handled correctly. We recommend loading resources with resources.Get
Changes to Hugo objects (e.g. Page) passed in the template context to lang.Translate may not be detected correctly. We recommend having simple i18n templates without too much data context passed in other than simple types such as strings and numbers.
Note that the cachebuster configuration (when A changes then rebuild B) works well with the above, but we recommend that you revise that configuration, as it in most situations should not be needed. One example where it is still needed is with TailwindCSS and using changes to hugo_stats.json to trigger new CSS rebuilds.
Document Store: Previously, a little simplified, we split the document store (where we store pages and resources) in a tree per language. This worked pretty well, but the structure made some operations harder than they needed to be. We have now restructured it into one Radix tree for all languages. Internally the language is considered to be a dimension of that tree, and the tree can be viewed in all dimensions concurrently. This makes some operations re. language simpler (e.g. finding translations is just a slice range), but the idea is that it should also be relatively inexpensive to add more dimensions if needed (e.g. role).
Fixes #10169
Fixes #10364
Fixes #10482
Fixes #10630
Fixes #10656
Fixes #10694
Fixes #10918
Fixes #11262
Fixes #11439
Fixes #11453
Fixes #11457
Fixes #11466
Fixes #11540
Fixes #11551
Fixes #11556
Fixes #11654
Fixes #11661
Fixes #11663
Fixes #11664
Fixes #11669
Fixes #11671
Fixes #11807
Fixes #11808
Fixes #11809
Fixes #11815
Fixes #11840
Fixes #11853
Fixes #11860
Fixes #11883
Fixes #11904
Fixes #7388
Fixes #7425
Fixes #7436
Fixes #7544
Fixes #7882
Fixes #7960
Fixes #8255
Fixes #8307
Fixes #8863
Fixes #8927
Fixes #9192
Fixes #9324
2023-12-24 13:11:05 -05:00
|
|
|
return 0, fmt.Errorf("failed to convert content to string: %w", err)
|
2017-03-13 18:55:02 -04:00
|
|
|
}
|
|
|
|
|
2021-05-03 03:10:06 -04:00
|
|
|
isCJKLanguage, err := regexp.MatchString(`\p{Han}|\p{Hangul}|\p{Hiragana}|\p{Katakana}`, ss)
|
|
|
|
if err != nil {
|
all: Rework page store, add a dynacache, improve partial rebuilds, and some general spring cleaning
There are some breaking changes in this commit, see #11455.
Closes #11455
Closes #11549
This fixes a set of bugs (see issue list) and it is also paying some technical debt accumulated over the years. We now build with Staticcheck enabled in the CI build.
The performance should be about the same as before for regular sized Hugo sites, but it should perform and scale much better to larger data sets, as objects that uses lots of memory (e.g. rendered Markdown, big JSON files read into maps with transform.Unmarshal etc.) will now get automatically garbage collected if needed. Performance on partial rebuilds when running the server in fast render mode should be the same, but the change detection should be much more accurate.
A list of the notable new features:
* A new dependency tracker that covers (almost) all of Hugo's API and is used to do fine grained partial rebuilds when running the server.
* A new and simpler tree document store which allows fast lookups and prefix-walking in all dimensions (e.g. language) concurrently.
* You can now configure an upper memory limit allowing for much larger data sets and/or running on lower specced PCs.
We have lifted the "no resources in sub folders" restriction for branch bundles (e.g. sections).
Memory Limit
* Hugos will, by default, set aside a quarter of the total system memory, but you can set this via the OS environment variable HUGO_MEMORYLIMIT (in gigabytes). This is backed by a partitioned LRU cache used throughout Hugo. A cache that gets dynamically resized in low memory situations, allowing Go's Garbage Collector to free the memory.
New Dependency Tracker: Hugo has had a rule based coarse grained approach to server rebuilds that has worked mostly pretty well, but there have been some surprises (e.g. stale content). This is now revamped with a new dependency tracker that can quickly calculate the delta given a changed resource (e.g. a content file, template, JS file etc.). This handles transitive relations, e.g. $page -> js.Build -> JS import, or $page1.Content -> render hook -> site.GetPage -> $page2.Title, or $page1.Content -> shortcode -> partial -> site.RegularPages -> $page2.Content -> shortcode ..., and should also handle changes to aggregated values (e.g. site.Lastmod) effectively.
This covers all of Hugo's API with 2 known exceptions (a list that may not be fully exhaustive):
Changes to files loaded with template func os.ReadFile may not be handled correctly. We recommend loading resources with resources.Get
Changes to Hugo objects (e.g. Page) passed in the template context to lang.Translate may not be detected correctly. We recommend having simple i18n templates without too much data context passed in other than simple types such as strings and numbers.
Note that the cachebuster configuration (when A changes then rebuild B) works well with the above, but we recommend that you revise that configuration, as it in most situations should not be needed. One example where it is still needed is with TailwindCSS and using changes to hugo_stats.json to trigger new CSS rebuilds.
Document Store: Previously, a little simplified, we split the document store (where we store pages and resources) in a tree per language. This worked pretty well, but the structure made some operations harder than they needed to be. We have now restructured it into one Radix tree for all languages. Internally the language is considered to be a dimension of that tree, and the tree can be viewed in all dimensions concurrently. This makes some operations re. language simpler (e.g. finding translations is just a slice range), but the idea is that it should also be relatively inexpensive to add more dimensions if needed (e.g. role).
Fixes #10169
Fixes #10364
Fixes #10482
Fixes #10630
Fixes #10656
Fixes #10694
Fixes #10918
Fixes #11262
Fixes #11439
Fixes #11453
Fixes #11457
Fixes #11466
Fixes #11540
Fixes #11551
Fixes #11556
Fixes #11654
Fixes #11661
Fixes #11663
Fixes #11664
Fixes #11669
Fixes #11671
Fixes #11807
Fixes #11808
Fixes #11809
Fixes #11815
Fixes #11840
Fixes #11853
Fixes #11860
Fixes #11883
Fixes #11904
Fixes #7388
Fixes #7425
Fixes #7436
Fixes #7544
Fixes #7882
Fixes #7960
Fixes #8255
Fixes #8307
Fixes #8863
Fixes #8927
Fixes #9192
Fixes #9324
2023-12-24 13:11:05 -05:00
|
|
|
return 0, fmt.Errorf("failed to match regex pattern against string: %w", err)
|
2021-05-03 03:10:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if !isCJKLanguage {
|
2022-05-25 04:56:14 -04:00
|
|
|
return len(strings.Fields(tpl.StripHTML(ss))), nil
|
2021-05-03 03:10:06 -04:00
|
|
|
}
|
|
|
|
|
2017-03-13 18:55:02 -04:00
|
|
|
counter := 0
|
2022-05-25 04:56:14 -04:00
|
|
|
for _, word := range strings.Fields(tpl.StripHTML(ss)) {
|
2017-03-13 18:55:02 -04:00
|
|
|
runeCount := utf8.RuneCountInString(word)
|
|
|
|
if len(word) == runeCount {
|
|
|
|
counter++
|
|
|
|
} else {
|
|
|
|
counter += runeCount
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return counter, nil
|
|
|
|
}
|
|
|
|
|
2020-07-03 04:34:40 -04:00
|
|
|
// Count counts the number of non-overlapping instances of substr in s.
|
|
|
|
// If substr is an empty string, Count returns 1 + the number of Unicode code points in s.
|
2022-03-17 17:03:27 -04:00
|
|
|
func (ns *Namespace) Count(substr, s any) (int, error) {
|
2020-07-03 04:34:40 -04:00
|
|
|
substrs, err := cast.ToStringE(substr)
|
|
|
|
if err != nil {
|
all: Rework page store, add a dynacache, improve partial rebuilds, and some general spring cleaning
There are some breaking changes in this commit, see #11455.
Closes #11455
Closes #11549
This fixes a set of bugs (see issue list) and it is also paying some technical debt accumulated over the years. We now build with Staticcheck enabled in the CI build.
The performance should be about the same as before for regular sized Hugo sites, but it should perform and scale much better to larger data sets, as objects that uses lots of memory (e.g. rendered Markdown, big JSON files read into maps with transform.Unmarshal etc.) will now get automatically garbage collected if needed. Performance on partial rebuilds when running the server in fast render mode should be the same, but the change detection should be much more accurate.
A list of the notable new features:
* A new dependency tracker that covers (almost) all of Hugo's API and is used to do fine grained partial rebuilds when running the server.
* A new and simpler tree document store which allows fast lookups and prefix-walking in all dimensions (e.g. language) concurrently.
* You can now configure an upper memory limit allowing for much larger data sets and/or running on lower specced PCs.
We have lifted the "no resources in sub folders" restriction for branch bundles (e.g. sections).
Memory Limit
* Hugos will, by default, set aside a quarter of the total system memory, but you can set this via the OS environment variable HUGO_MEMORYLIMIT (in gigabytes). This is backed by a partitioned LRU cache used throughout Hugo. A cache that gets dynamically resized in low memory situations, allowing Go's Garbage Collector to free the memory.
New Dependency Tracker: Hugo has had a rule based coarse grained approach to server rebuilds that has worked mostly pretty well, but there have been some surprises (e.g. stale content). This is now revamped with a new dependency tracker that can quickly calculate the delta given a changed resource (e.g. a content file, template, JS file etc.). This handles transitive relations, e.g. $page -> js.Build -> JS import, or $page1.Content -> render hook -> site.GetPage -> $page2.Title, or $page1.Content -> shortcode -> partial -> site.RegularPages -> $page2.Content -> shortcode ..., and should also handle changes to aggregated values (e.g. site.Lastmod) effectively.
This covers all of Hugo's API with 2 known exceptions (a list that may not be fully exhaustive):
Changes to files loaded with template func os.ReadFile may not be handled correctly. We recommend loading resources with resources.Get
Changes to Hugo objects (e.g. Page) passed in the template context to lang.Translate may not be detected correctly. We recommend having simple i18n templates without too much data context passed in other than simple types such as strings and numbers.
Note that the cachebuster configuration (when A changes then rebuild B) works well with the above, but we recommend that you revise that configuration, as it in most situations should not be needed. One example where it is still needed is with TailwindCSS and using changes to hugo_stats.json to trigger new CSS rebuilds.
Document Store: Previously, a little simplified, we split the document store (where we store pages and resources) in a tree per language. This worked pretty well, but the structure made some operations harder than they needed to be. We have now restructured it into one Radix tree for all languages. Internally the language is considered to be a dimension of that tree, and the tree can be viewed in all dimensions concurrently. This makes some operations re. language simpler (e.g. finding translations is just a slice range), but the idea is that it should also be relatively inexpensive to add more dimensions if needed (e.g. role).
Fixes #10169
Fixes #10364
Fixes #10482
Fixes #10630
Fixes #10656
Fixes #10694
Fixes #10918
Fixes #11262
Fixes #11439
Fixes #11453
Fixes #11457
Fixes #11466
Fixes #11540
Fixes #11551
Fixes #11556
Fixes #11654
Fixes #11661
Fixes #11663
Fixes #11664
Fixes #11669
Fixes #11671
Fixes #11807
Fixes #11808
Fixes #11809
Fixes #11815
Fixes #11840
Fixes #11853
Fixes #11860
Fixes #11883
Fixes #11904
Fixes #7388
Fixes #7425
Fixes #7436
Fixes #7544
Fixes #7882
Fixes #7960
Fixes #8255
Fixes #8307
Fixes #8863
Fixes #8927
Fixes #9192
Fixes #9324
2023-12-24 13:11:05 -05:00
|
|
|
return 0, fmt.Errorf("failed to convert substr to string: %w", err)
|
2020-07-03 04:34:40 -04:00
|
|
|
}
|
|
|
|
ss, err := cast.ToStringE(s)
|
|
|
|
if err != nil {
|
all: Rework page store, add a dynacache, improve partial rebuilds, and some general spring cleaning
There are some breaking changes in this commit, see #11455.
Closes #11455
Closes #11549
This fixes a set of bugs (see issue list) and it is also paying some technical debt accumulated over the years. We now build with Staticcheck enabled in the CI build.
The performance should be about the same as before for regular sized Hugo sites, but it should perform and scale much better to larger data sets, as objects that uses lots of memory (e.g. rendered Markdown, big JSON files read into maps with transform.Unmarshal etc.) will now get automatically garbage collected if needed. Performance on partial rebuilds when running the server in fast render mode should be the same, but the change detection should be much more accurate.
A list of the notable new features:
* A new dependency tracker that covers (almost) all of Hugo's API and is used to do fine grained partial rebuilds when running the server.
* A new and simpler tree document store which allows fast lookups and prefix-walking in all dimensions (e.g. language) concurrently.
* You can now configure an upper memory limit allowing for much larger data sets and/or running on lower specced PCs.
We have lifted the "no resources in sub folders" restriction for branch bundles (e.g. sections).
Memory Limit
* Hugos will, by default, set aside a quarter of the total system memory, but you can set this via the OS environment variable HUGO_MEMORYLIMIT (in gigabytes). This is backed by a partitioned LRU cache used throughout Hugo. A cache that gets dynamically resized in low memory situations, allowing Go's Garbage Collector to free the memory.
New Dependency Tracker: Hugo has had a rule based coarse grained approach to server rebuilds that has worked mostly pretty well, but there have been some surprises (e.g. stale content). This is now revamped with a new dependency tracker that can quickly calculate the delta given a changed resource (e.g. a content file, template, JS file etc.). This handles transitive relations, e.g. $page -> js.Build -> JS import, or $page1.Content -> render hook -> site.GetPage -> $page2.Title, or $page1.Content -> shortcode -> partial -> site.RegularPages -> $page2.Content -> shortcode ..., and should also handle changes to aggregated values (e.g. site.Lastmod) effectively.
This covers all of Hugo's API with 2 known exceptions (a list that may not be fully exhaustive):
Changes to files loaded with template func os.ReadFile may not be handled correctly. We recommend loading resources with resources.Get
Changes to Hugo objects (e.g. Page) passed in the template context to lang.Translate may not be detected correctly. We recommend having simple i18n templates without too much data context passed in other than simple types such as strings and numbers.
Note that the cachebuster configuration (when A changes then rebuild B) works well with the above, but we recommend that you revise that configuration, as it in most situations should not be needed. One example where it is still needed is with TailwindCSS and using changes to hugo_stats.json to trigger new CSS rebuilds.
Document Store: Previously, a little simplified, we split the document store (where we store pages and resources) in a tree per language. This worked pretty well, but the structure made some operations harder than they needed to be. We have now restructured it into one Radix tree for all languages. Internally the language is considered to be a dimension of that tree, and the tree can be viewed in all dimensions concurrently. This makes some operations re. language simpler (e.g. finding translations is just a slice range), but the idea is that it should also be relatively inexpensive to add more dimensions if needed (e.g. role).
Fixes #10169
Fixes #10364
Fixes #10482
Fixes #10630
Fixes #10656
Fixes #10694
Fixes #10918
Fixes #11262
Fixes #11439
Fixes #11453
Fixes #11457
Fixes #11466
Fixes #11540
Fixes #11551
Fixes #11556
Fixes #11654
Fixes #11661
Fixes #11663
Fixes #11664
Fixes #11669
Fixes #11671
Fixes #11807
Fixes #11808
Fixes #11809
Fixes #11815
Fixes #11840
Fixes #11853
Fixes #11860
Fixes #11883
Fixes #11904
Fixes #7388
Fixes #7425
Fixes #7436
Fixes #7544
Fixes #7882
Fixes #7960
Fixes #8255
Fixes #8307
Fixes #8863
Fixes #8927
Fixes #9192
Fixes #9324
2023-12-24 13:11:05 -05:00
|
|
|
return 0, fmt.Errorf("failed to convert s to string: %w", err)
|
2020-07-03 04:34:40 -04:00
|
|
|
}
|
|
|
|
return strings.Count(ss, substrs), nil
|
|
|
|
}
|
|
|
|
|
2017-03-13 18:55:02 -04:00
|
|
|
// Chomp returns a copy of s with all trailing newline characters removed.
|
2022-03-17 17:03:27 -04:00
|
|
|
func (ns *Namespace) Chomp(s any) (any, error) {
|
2017-03-13 18:55:02 -04:00
|
|
|
ss, err := cast.ToStringE(s)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2022-02-26 04:42:21 -05:00
|
|
|
res := text.Chomp(ss)
|
2017-10-13 16:57:52 -04:00
|
|
|
switch s.(type) {
|
|
|
|
case template.HTML:
|
|
|
|
return template.HTML(res), nil
|
|
|
|
default:
|
|
|
|
return res, nil
|
|
|
|
}
|
2017-03-13 18:55:02 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Contains reports whether substr is in s.
|
2022-03-17 17:03:27 -04:00
|
|
|
func (ns *Namespace) Contains(s, substr any) (bool, error) {
|
2017-03-13 18:55:02 -04:00
|
|
|
ss, err := cast.ToStringE(s)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
su, err := cast.ToStringE(substr)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
2020-08-27 22:59:20 -04:00
|
|
|
return strings.Contains(ss, su), nil
|
2017-03-13 18:55:02 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// ContainsAny reports whether any Unicode code points in chars are within s.
|
2022-03-17 17:03:27 -04:00
|
|
|
func (ns *Namespace) ContainsAny(s, chars any) (bool, error) {
|
2017-03-13 18:55:02 -04:00
|
|
|
ss, err := cast.ToStringE(s)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
sc, err := cast.ToStringE(chars)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
2020-08-27 22:59:20 -04:00
|
|
|
return strings.ContainsAny(ss, sc), nil
|
2017-03-13 18:55:02 -04:00
|
|
|
}
|
|
|
|
|
2023-01-31 03:54:34 -05:00
|
|
|
// ContainsNonSpace reports whether s contains any non-space characters as defined
|
|
|
|
// by Unicode's White Space property,
|
2023-01-04 12:24:36 -05:00
|
|
|
// <docsmeta>{"newIn": "0.111.0" }</docsmeta>
|
2024-04-04 11:22:33 -04:00
|
|
|
func (ns *Namespace) ContainsNonSpace(s any) (bool, error) {
|
|
|
|
ss, err := cast.ToStringE(s)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
2023-01-31 03:54:34 -05:00
|
|
|
|
|
|
|
for _, r := range ss {
|
|
|
|
if !unicode.IsSpace(r) {
|
2024-04-04 11:22:33 -04:00
|
|
|
return true, nil
|
2023-01-31 03:54:34 -05:00
|
|
|
}
|
|
|
|
}
|
2024-04-04 11:22:33 -04:00
|
|
|
return false, nil
|
2023-01-31 03:54:34 -05:00
|
|
|
}
|
|
|
|
|
2024-04-01 22:26:15 -04:00
|
|
|
// Diff returns an anchored diff of the two texts old and new in the “unified
|
|
|
|
// diff” format. If old and new are identical, Diff returns an empty string.
|
2024-04-04 11:22:33 -04:00
|
|
|
func (ns *Namespace) Diff(oldname string, old any, newname string, new any) (string, error) {
|
|
|
|
olds, err := cast.ToStringE(old)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
news, err := cast.ToStringE(new)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return string(diff.Diff(oldname, []byte(olds), newname, []byte(news))), nil
|
2024-04-01 22:26:15 -04:00
|
|
|
}
|
|
|
|
|
2017-03-13 18:55:02 -04:00
|
|
|
// HasPrefix tests whether the input s begins with prefix.
|
2022-03-17 17:03:27 -04:00
|
|
|
func (ns *Namespace) HasPrefix(s, prefix any) (bool, error) {
|
2017-03-13 18:55:02 -04:00
|
|
|
ss, err := cast.ToStringE(s)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
sx, err := cast.ToStringE(prefix)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
2020-08-27 22:59:20 -04:00
|
|
|
return strings.HasPrefix(ss, sx), nil
|
2017-03-13 18:55:02 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// HasSuffix tests whether the input s begins with suffix.
|
2022-03-17 17:03:27 -04:00
|
|
|
func (ns *Namespace) HasSuffix(s, suffix any) (bool, error) {
|
2017-03-13 18:55:02 -04:00
|
|
|
ss, err := cast.ToStringE(s)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
sx, err := cast.ToStringE(suffix)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
2020-08-27 22:59:20 -04:00
|
|
|
return strings.HasSuffix(ss, sx), nil
|
2017-03-13 18:55:02 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Replace returns a copy of the string s with all occurrences of old replaced
|
2020-08-27 22:59:20 -04:00
|
|
|
// with new. The number of replacements can be limited with an optional fourth
|
|
|
|
// parameter.
|
2022-03-17 17:03:27 -04:00
|
|
|
func (ns *Namespace) Replace(s, old, new any, limit ...any) (string, error) {
|
2017-03-13 18:55:02 -04:00
|
|
|
ss, err := cast.ToStringE(s)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
so, err := cast.ToStringE(old)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
sn, err := cast.ToStringE(new)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2020-08-27 22:59:20 -04:00
|
|
|
if len(limit) == 0 {
|
|
|
|
return strings.ReplaceAll(ss, so, sn), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
lim, err := cast.ToIntE(limit[0])
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return strings.Replace(ss, so, sn, lim), nil
|
2017-03-13 18:55:02 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// SliceString slices a string by specifying a half-open range with
|
|
|
|
// two indices, start and end. 1 and 4 creates a slice including elements 1 through 3.
|
|
|
|
// The end index can be omitted, it defaults to the string's length.
|
2022-03-17 17:03:27 -04:00
|
|
|
func (ns *Namespace) SliceString(a any, startEnd ...any) (string, error) {
|
2017-03-13 18:55:02 -04:00
|
|
|
aStr, err := cast.ToStringE(a)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
var argStart, argEnd int
|
|
|
|
|
|
|
|
argNum := len(startEnd)
|
|
|
|
|
|
|
|
if argNum > 0 {
|
|
|
|
if argStart, err = cast.ToIntE(startEnd[0]); err != nil {
|
|
|
|
return "", errors.New("start argument must be integer")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if argNum > 1 {
|
|
|
|
if argEnd, err = cast.ToIntE(startEnd[1]); err != nil {
|
|
|
|
return "", errors.New("end argument must be integer")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if argNum > 2 {
|
|
|
|
return "", errors.New("too many arguments")
|
|
|
|
}
|
|
|
|
|
|
|
|
asRunes := []rune(aStr)
|
|
|
|
|
|
|
|
if argNum > 0 && (argStart < 0 || argStart >= len(asRunes)) {
|
|
|
|
return "", errors.New("slice bounds out of range")
|
|
|
|
}
|
|
|
|
|
|
|
|
if argNum == 2 {
|
|
|
|
if argEnd < 0 || argEnd > len(asRunes) {
|
|
|
|
return "", errors.New("slice bounds out of range")
|
|
|
|
}
|
|
|
|
return string(asRunes[argStart:argEnd]), nil
|
|
|
|
} else if argNum == 1 {
|
|
|
|
return string(asRunes[argStart:]), nil
|
|
|
|
} else {
|
|
|
|
return string(asRunes[:]), nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Split slices an input string into all substrings separated by delimiter.
|
2022-03-17 17:03:27 -04:00
|
|
|
func (ns *Namespace) Split(a any, delimiter string) ([]string, error) {
|
2017-03-13 18:55:02 -04:00
|
|
|
aStr, err := cast.ToStringE(a)
|
|
|
|
if err != nil {
|
|
|
|
return []string{}, err
|
|
|
|
}
|
|
|
|
|
2020-08-27 22:59:20 -04:00
|
|
|
return strings.Split(aStr, delimiter), nil
|
2017-03-13 18:55:02 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Substr extracts parts of a string, beginning at the character at the specified
|
|
|
|
// position, and returns the specified number of characters.
|
|
|
|
//
|
|
|
|
// It normally takes two parameters: start and length.
|
|
|
|
// It can also take one parameter: start, i.e. length is omitted, in which case
|
|
|
|
// the substring starting from start until the end of the string will be returned.
|
|
|
|
//
|
|
|
|
// To extract characters from the end of the string, use a negative start number.
|
|
|
|
//
|
|
|
|
// In addition, borrowing from the extended behavior described at http://php.net/substr,
|
|
|
|
// if length is given and is negative, then that many characters will be omitted from
|
|
|
|
// the end of string.
|
2022-03-17 17:03:27 -04:00
|
|
|
func (ns *Namespace) Substr(a any, nums ...any) (string, error) {
|
2020-11-28 00:43:01 -05:00
|
|
|
s, err := cast.ToStringE(a)
|
2017-03-13 18:55:02 -04:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2020-11-28 00:43:01 -05:00
|
|
|
asRunes := []rune(s)
|
|
|
|
rlen := len(asRunes)
|
2017-03-13 18:55:02 -04:00
|
|
|
|
2020-11-28 00:43:01 -05:00
|
|
|
var start, length int
|
2017-03-13 18:55:02 -04:00
|
|
|
|
|
|
|
switch len(nums) {
|
|
|
|
case 0:
|
2020-11-28 00:43:01 -05:00
|
|
|
return "", errors.New("too few arguments")
|
2017-03-13 18:55:02 -04:00
|
|
|
case 1:
|
|
|
|
if start, err = cast.ToIntE(nums[0]); err != nil {
|
2020-11-28 00:43:01 -05:00
|
|
|
return "", errors.New("start argument must be an integer")
|
2017-03-13 18:55:02 -04:00
|
|
|
}
|
2020-11-28 00:43:01 -05:00
|
|
|
length = rlen
|
2017-03-13 18:55:02 -04:00
|
|
|
case 2:
|
|
|
|
if start, err = cast.ToIntE(nums[0]); err != nil {
|
2020-11-28 00:43:01 -05:00
|
|
|
return "", errors.New("start argument must be an integer")
|
2017-03-13 18:55:02 -04:00
|
|
|
}
|
|
|
|
if length, err = cast.ToIntE(nums[1]); err != nil {
|
2020-11-28 00:43:01 -05:00
|
|
|
return "", errors.New("length argument must be an integer")
|
2017-03-13 18:55:02 -04:00
|
|
|
}
|
|
|
|
default:
|
|
|
|
return "", errors.New("too many arguments")
|
|
|
|
}
|
|
|
|
|
2020-11-28 00:43:01 -05:00
|
|
|
if rlen == 0 {
|
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if start < 0 {
|
|
|
|
start += rlen
|
|
|
|
}
|
|
|
|
|
|
|
|
// start was originally negative beyond rlen
|
|
|
|
if start < 0 {
|
2017-03-13 18:55:02 -04:00
|
|
|
start = 0
|
|
|
|
}
|
2020-11-28 00:43:01 -05:00
|
|
|
|
|
|
|
if start > rlen-1 {
|
2021-01-03 13:01:42 -05:00
|
|
|
return "", nil
|
2017-03-13 18:55:02 -04:00
|
|
|
}
|
|
|
|
|
2020-11-28 00:43:01 -05:00
|
|
|
end := rlen
|
|
|
|
|
2020-11-28 10:56:49 -05:00
|
|
|
switch {
|
|
|
|
case length == 0:
|
|
|
|
return "", nil
|
|
|
|
case length < 0:
|
2020-11-28 00:43:01 -05:00
|
|
|
end += length
|
2020-11-28 10:56:49 -05:00
|
|
|
case length > 0:
|
2020-11-28 00:43:01 -05:00
|
|
|
end = start + length
|
|
|
|
}
|
|
|
|
|
|
|
|
if start >= end {
|
2021-01-03 13:01:42 -05:00
|
|
|
return "", nil
|
2017-03-13 18:55:02 -04:00
|
|
|
}
|
|
|
|
|
2020-11-28 00:43:01 -05:00
|
|
|
if end < 0 {
|
|
|
|
return "", nil
|
2017-03-13 18:55:02 -04:00
|
|
|
}
|
2020-11-28 00:43:01 -05:00
|
|
|
|
|
|
|
if end > rlen {
|
|
|
|
end = rlen
|
2017-03-13 18:55:02 -04:00
|
|
|
}
|
|
|
|
|
2020-11-28 00:43:01 -05:00
|
|
|
return string(asRunes[start:end]), nil
|
2017-03-13 18:55:02 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Title returns a copy of the input s with all Unicode letters that begin words
|
|
|
|
// mapped to their title case.
|
2022-03-17 17:03:27 -04:00
|
|
|
func (ns *Namespace) Title(s any) (string, error) {
|
2017-03-13 18:55:02 -04:00
|
|
|
ss, err := cast.ToStringE(s)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2023-01-04 12:24:36 -05:00
|
|
|
return ns.deps.Conf.CreateTitle(ss), nil
|
2017-03-13 18:55:02 -04:00
|
|
|
}
|
|
|
|
|
2022-04-30 12:12:08 -04:00
|
|
|
// FirstUpper converts s making the first character upper case.
|
2022-03-17 17:03:27 -04:00
|
|
|
func (ns *Namespace) FirstUpper(s any) (string, error) {
|
2018-09-07 03:05:25 -04:00
|
|
|
ss, err := cast.ToStringE(s)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return helpers.FirstUpper(ss), nil
|
|
|
|
}
|
|
|
|
|
2017-03-13 18:55:02 -04:00
|
|
|
// ToLower returns a copy of the input s with all Unicode letters mapped to their
|
|
|
|
// lower case.
|
2022-03-17 17:03:27 -04:00
|
|
|
func (ns *Namespace) ToLower(s any) (string, error) {
|
2017-03-13 18:55:02 -04:00
|
|
|
ss, err := cast.ToStringE(s)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2020-08-27 22:59:20 -04:00
|
|
|
return strings.ToLower(ss), nil
|
2017-03-13 18:55:02 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// ToUpper returns a copy of the input s with all Unicode letters mapped to their
|
|
|
|
// upper case.
|
2022-03-17 17:03:27 -04:00
|
|
|
func (ns *Namespace) ToUpper(s any) (string, error) {
|
2017-03-13 18:55:02 -04:00
|
|
|
ss, err := cast.ToStringE(s)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2020-08-27 22:59:20 -04:00
|
|
|
return strings.ToUpper(ss), nil
|
2017-03-13 18:55:02 -04:00
|
|
|
}
|
|
|
|
|
2022-04-30 12:12:08 -04:00
|
|
|
// Trim returns converts the strings s removing all leading and trailing characters defined
|
|
|
|
// contained.
|
2022-03-17 17:03:27 -04:00
|
|
|
func (ns *Namespace) Trim(s, cutset any) (string, error) {
|
2017-03-13 18:55:02 -04:00
|
|
|
ss, err := cast.ToStringE(s)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
sc, err := cast.ToStringE(cutset)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2020-08-27 22:59:20 -04:00
|
|
|
return strings.Trim(ss, sc), nil
|
2017-03-13 18:55:02 -04:00
|
|
|
}
|
|
|
|
|
2017-08-18 03:12:04 -04:00
|
|
|
// TrimLeft returns a slice of the string s with all leading characters
|
|
|
|
// contained in cutset removed.
|
2022-03-17 17:03:27 -04:00
|
|
|
func (ns *Namespace) TrimLeft(cutset, s any) (string, error) {
|
2017-08-18 03:12:04 -04:00
|
|
|
ss, err := cast.ToStringE(s)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
sc, err := cast.ToStringE(cutset)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2020-08-27 22:59:20 -04:00
|
|
|
return strings.TrimLeft(ss, sc), nil
|
2017-08-18 03:12:04 -04:00
|
|
|
}
|
|
|
|
|
2017-03-13 18:55:02 -04:00
|
|
|
// TrimPrefix returns s without the provided leading prefix string. If s doesn't
|
|
|
|
// start with prefix, s is returned unchanged.
|
2022-03-17 17:03:27 -04:00
|
|
|
func (ns *Namespace) TrimPrefix(prefix, s any) (string, error) {
|
2017-03-13 18:55:02 -04:00
|
|
|
ss, err := cast.ToStringE(s)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
sx, err := cast.ToStringE(prefix)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2020-08-27 22:59:20 -04:00
|
|
|
return strings.TrimPrefix(ss, sx), nil
|
2017-03-13 18:55:02 -04:00
|
|
|
}
|
|
|
|
|
2017-08-18 03:12:04 -04:00
|
|
|
// TrimRight returns a slice of the string s with all trailing characters
|
|
|
|
// contained in cutset removed.
|
2022-03-17 17:03:27 -04:00
|
|
|
func (ns *Namespace) TrimRight(cutset, s any) (string, error) {
|
2017-08-18 03:12:04 -04:00
|
|
|
ss, err := cast.ToStringE(s)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
sc, err := cast.ToStringE(cutset)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2020-08-27 22:59:20 -04:00
|
|
|
return strings.TrimRight(ss, sc), nil
|
2017-08-18 03:12:04 -04:00
|
|
|
}
|
|
|
|
|
2017-03-13 18:55:02 -04:00
|
|
|
// TrimSuffix returns s without the provided trailing suffix string. If s
|
|
|
|
// doesn't end with suffix, s is returned unchanged.
|
2022-03-17 17:03:27 -04:00
|
|
|
func (ns *Namespace) TrimSuffix(suffix, s any) (string, error) {
|
2017-03-13 18:55:02 -04:00
|
|
|
ss, err := cast.ToStringE(s)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
sx, err := cast.ToStringE(suffix)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2020-08-27 22:59:20 -04:00
|
|
|
return strings.TrimSuffix(ss, sx), nil
|
2017-03-13 18:55:02 -04:00
|
|
|
}
|
2018-06-03 02:55:37 -04:00
|
|
|
|
2022-04-30 12:12:08 -04:00
|
|
|
// Repeat returns a new string consisting of n copies of the string s.
|
2022-03-17 17:03:27 -04:00
|
|
|
func (ns *Namespace) Repeat(n, s any) (string, error) {
|
2018-06-03 02:55:37 -04:00
|
|
|
ss, err := cast.ToStringE(s)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
sn, err := cast.ToIntE(n)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2018-06-03 03:35:45 -04:00
|
|
|
if sn < 0 {
|
|
|
|
return "", errors.New("strings: negative Repeat count")
|
2018-06-03 02:55:37 -04:00
|
|
|
}
|
|
|
|
|
2020-08-27 22:59:20 -04:00
|
|
|
return strings.Repeat(ss, sn), nil
|
2018-06-03 02:55:37 -04:00
|
|
|
}
|