fmtstr

package module
v1.1.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 7, 2024 License: Apache-2.0 Imports: 8 Imported by: 1

README

godoc codecov Go Report Card

fmtstr - format string utility

fmtstr is a package for parsing fmt package format strings and doing interesting things with the results. This package is primarily built to support the custom gotext parsing of text and html template files within the Go-Enjin theming system.

Installation

> go get github.com/go-corelibs/fmtstr@latest

Examples

Parse

func main() {
    replaced, labelled, variables, err := Parse("Testing: %d %T things", ".Count", ".Data")
    // err == nil in this case
    // replaced == "Testing: %[1]d %[2]T things"
    // labelled == "Testing: {Count} {Data} things"
    // variables contains all the specifics of each format variable used and
    // if you're not building a language translation system, or otherwise doing
    // strange and different things with format strings, this is likely too
    // much information and not the actual Go package you're looking for
}

Go-CoreLibs

Go-CoreLibs is a repository of shared code between the Go-Curses and Go-Enjin projects.

License

Copyright 2023 The Go-CoreLibs Authors

Licensed under the Apache License, Version 2.0 (the "License");
you may not use 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.

Documentation

Overview

Package fmtstr is a fmt format string utility module. fmtstr is intended for use within the Go-Enjin project which parse text/html templates for `_` translation function invocations in order to produce a user interface for updating a locale's translation strings. See Decompose for more details.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrPosArgNotImpl = errors.New("positional argument support for width and precision is not implemented yet")
)

Functions

This section is empty.

Types

type Modifier

type Modifier uint8
const (
	NoModifiers Modifier = 0
	ModPlus     Modifier = 1 << iota
	ModMinus
	ModHash
	ModSpace
	ModDecimal
	ModZeroPad
)

type Variable

type Variable struct {
	Type      string
	Label     string
	Source    string
	Pos       int
	Verb      Verb
	Width     int
	Precision int
	Modifiers Modifier
}

func (*Variable) Has

func (v *Variable) Has(m Modifier) (present bool)

func (*Variable) String

func (v *Variable) String() (value string)

type Variables

type Variables []*Variable

func Decompose added in v1.1.0

func Decompose(format string, argv ...string) (replaced, labelled string, variables Variables, err error)

Decompose is intended to be used primarily by the Go-Enjin project for parsing text and html template files for custom `_` translation function calls. The `_` func is essentially a wrapper around a language printer instance's `printer.Sprintf` function. The Go-Enjin `enjenv` command has a `be-extract-locales` sub-command which parses the template sources and identifies these `_` calls, making note of the literal arguments passed to the `_` function. These arguments are further examined and coalesced into the varargs `argv` list passed to Decompose along with the translation `format` string.

Decompose examines the `format` string for the standard fmt substitution variables and builds up a list of Variables which contains most of the flags and other components of any given substitution Variable. The only fmt format string feature that Decompose does not support at this time are the explicit argument indexes for the width and precision flags, identified by the square bracketed digit and an asterisk `*`. See the fmt godoc: https://pkg.go.dev/fmt#hdr-Explicit_argument_indexes

Decompose returns the list of Variables along with two modified versions of the original format string. The first, `replaced` is the same as the original with the exception that all variables are replaced with their explicit position versions. For example: `%d %d` becomes `%[1]d %[2]d`. The `labelled` format string the variables replaced with labels that are derived from the corresponding argument in the `argv` list given. The labels are CamelCased and have their position suffixed. For example: if `argv[0]` is `$.some_thing`, the label would be `SomeThing1`, and for a more complete example: a format of `%d %d` is labelled as `{Num1} {Num2}` if there were no parsable argv names.

If the argv list does not have useful text for labelled, such as if the `enjenv` template parser provided garbage for whatever reason, Decompose is lenient and won't throw any errors about too few arguments to the format string. Decompose will use just the format string specifier and derive a meaningful label from that. For example: `%d` would become `Num1` and `%f` would become `Float1`.

func (Variables) Count

func (v Variables) Count() (argc int)

func (Variables) Sort

func (v Variables) Sort() (sorted Variables)

func (Variables) String

func (v Variables) String() (values string)

type Verb

type Verb string

func (Verb) Equal

func (v Verb) Equal(o Verb) (equal bool)

func (Verb) Label

func (v Verb) Label() string

func (Verb) String

func (v Verb) String() string

func (Verb) Type

func (v Verb) Type() string

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL