data

package
v0.4.17 Latest Latest
Warning

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

Go to latest
Published: Feb 26, 2026 License: MIT Imports: 1 Imported by: 0

Documentation

Overview

data package is used to encode/decode data within psv tables

Purpose:

  • use "\|" to allow "|" characters to be used in psv tables
  • translate tabs to "\t" to prevent column mis-alignment

Mappings:

| character |    plaintext     |    data     |        psv        |                  notes                  |
| --------- | ---------------- | ----------- | ----------------- | --------------------------------------- |
| pipe      | {pipe}           | -           | -                 | not possible - used as column separator |
|           | -                | {pipe}      | {backslash}{pipe} | encode before rendering                 |
| --------- | ---------------- | ----------- | ----------------- | --------------------------------------- |
| tab       | {tab}            | {tab}       | {backslash}"t"    | encode before rendering                 |
| --------- | ---------------- | ----------- | ----------------- | --------------------------------------- |
| backslash | {backslash}<any> | -unchanged- | -unchanged-       |                                         |

Decisions:

  • do not attempt to handle all manner of "standard" escape sequences
  • after splitting columns, a single "\" is just a "\"
  • do not perform round-trip translation
  • do not use surrounding pairs (e.g. quotes), the side-effects of an unmatched pair are unrecoverable
  • alternative: see [psv.Unquote] and [psv.Replace] for help with
Example (Data_encoding)
package main

import (
	"fmt"

	"codeberg.org/japh/psv"
)

func main() {
	// This example demonstrates the automatic decoding / encoding that psv performs
	//
	// Notes:
	//  - the "data" and "output" columns are specially encoded so that psv
	//    takes no notice of them, allowing us to compare their _actual_ contents.
	//  - the "stripped padding" input should be indented a few spaces (i.e. to demonstrate whitespace stripping).
	//    if the word "padded" is not surrounded by extra spaces, please add them back in :-)
	table := psv.NewTable().FromString(`
        |          label          |       input        |        data        |       output       |                   description                    |
        | ----------------------- | ------------------ | ------------------ | ------------------ | ------------------------------------------------ |
        | pipe                    | \|                 | {p}                | {bsl}{p}           | pipes are encoded/decoded                        |
        | tab                     | \t                 | {t}                | {bsl}t             | tab's are encoded/decoded                        |
        | newline                 | \n                 | {bsl}n             | {bsl}n             | other escape sequences are left untouched        |
        | backslash               | \                  | {bsl}              | {bsl}              | a lone-backslash is kept as is                   |
        | surrounding backslashes | \ \                | {bsl}{sp}{bsl}     | {bsl}{sp}{bsl}     | a lone-backslash is kept as is                   |
        | stripped padding        |   padded           | padded             | padded             | surrounding spaces are always removed            |
        | quoted spaces           | ' spaces '         | ' spaces '         | ' spaces '         | remove quotes manually with psv.Unquote()        |
        | embedded spaces         | a  b               | a  b               | a  b               | embedded spaces are kept, even without quotes    |
        | embedded quotes         | "G'day!", he cried | "G'day!", he cried | "G'day!", he cried | embedded and unmatched quotes are left unchanged |
    `)

	visualise := func(s string) string {
		s = psv.Replace(s, "\x09", `{t}`)
		s = psv.Replace(s, "\x20", `{sp}`)
		s = psv.Replace(s, `|`, `{p}`)
		s = psv.Replace(s, `\`, `{bsl}`)
		return s
	}

	w := 32
	for _, row := range table.DataRows() {
		var (
			testName = row.Field("label")
			gotData  = row.Field("input") // data that psv got from the input column
		)
		fmt.Printf("%-*s %s\n", w, testName+":", visualise(gotData))
	}

}
Output:

pipe:                            {p}
tab:                             {t}
newline:                         {bsl}n
backslash:                       {bsl}
surrounding backslashes:         {bsl}{sp}{bsl}
stripped padding:                padded
quoted spaces:                   '{sp}spaces{sp}'
embedded spaces:                 a{sp}{sp}b
embedded quotes:                 "G'day!",{sp}he{sp}cried

Index

Examples

Constants

View Source
const (
	TabRune       = '\x09' // tab
	BackslashRune = '\x5c' // backslash
	PipeRune      = '\x7c' // pipe
	Tab           = "\x09" // tab
	Backslash     = "\x5c" // backslash
	Pipe          = "\x7c" // pipe
)

Variables

This section is empty.

Functions

func Decode

func Decode(encoded string) (plaintext string, replaced []int)

Decode returns the plaintext representation of an encoded input and a slice of ints representing the byte position of the characters that were replaced.

TODO: 2026-02-11 only used to decode a single escaped character - e.g. `\|`

  • remove loop
  • remove replaced return value

func Encode

func Encode(plaintext string) (encoded string)

Encode returns the provided string with all special characters replaced by their encoded equivalents.

i.e. pipes, tabs and backslashes are escaped.

Types

This section is empty.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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