rslicer

package module
v0.0.0-...-f5f4e20 Latest Latest
Warning

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

Go to latest
Published: May 22, 2024 License: BSD-3-Clause Imports: 2 Imported by: 0

README

rslicer

Fast and simple rune slicer. No memory allocations. Just tell borders for runes and get byte positions in a string. Support negative indexes.

Available two functions:

  • GetRuneRange - return byte position for
  • GetSliceByRunes - return string slice

get module

go get github.com/HoskeOwl/rslicer

Examples

Can be used with ascii

package main

import (
	"fmt"

	"github.com/HoskeOwl/rslicer"
)

func main() {
	s := "Some sentence."
	begin, end, err := rslicer.GetRuneRange(s, 0, 4)
    if err != nil{
        fmt.Printf("Got some error: %v", err)
        return
    }
    fmt.Printf("Result: '%v'", s[begin:end]) //Result: 'Some'
}

Can be used with unicode

package main

import (
	"fmt"

	"github.com/HoskeOwl/rslicer"
)

func main() {
	s := "Some 日本語 symbols."
	begin, end, err := rslicer.GetRuneRange(s, 5, 8)
	if err != nil {
		fmt.Printf("Got some error: %v", err)
		return
	}
	fmt.Printf("Result: '%v'", s[begin:end]) //Result: '日本語'
}

And with negative indexes too.

package main

import (
	"fmt"

	"github.com/HoskeOwl/rslicer"
)

func main() {
	s := "Some 日本語 symbols."
	begin, end, err := rslicer.GetRuneRange(s, 5, -9)
	if err != nil {
		fmt.Printf("Got some error: %v", err)
		return
	}
	fmt.Printf("Result: '%v'", s[begin:end]) //Result: '日本語'
}

Or directry get slice.

package main

import (
	"fmt"

	"github.com/HoskeOwl/rslicer"
)

func main() {
	s := "Some 日本語 symbols."
	out, err := rslicer.GetRuneSlice(s, 5, -9)
	if err != nil {
		fmt.Printf("Got some error: %v", err)
		return
	}
	fmt.Printf("Result: '%v'", out) //Result: '日本語'
}

Library Benchmark

goos: linux
goarch: amd64
pkg: github.com/HoskeOwl/rslicer
cpu: AMD Ryzen 7 4700U with Radeon Graphics  

# slice through runes transformation
BenchmarkSmallThroughRunes-8                	41294881	        30.36 ns/op	       0 B/op	       0 allocs/op
BenchmarkMediumThroughRunes-8               	 2165407	       542.9 ns/op	     176 B/op	       1 allocs/op
BenchmarkLargeThroughRunes-8                	  137636	      7809 ns/op	    2000 B/op	       2 allocs/op

# slice through library
BenchmarkPositiveSmallWithFunction-8        	48914659	        21.65 ns/op	       0 B/op	       0 allocs/op
BenchmarkNegativeSmallWithFunction-8        	40990228	        24.60 ns/op	       0 B/op	       0 allocs/op
BenchmarkPositiveMediumWithFunction-8       	16005105	        76.07 ns/op	       0 B/op	       0 allocs/op
BenchmarkNegativeMediumWithFunction-8       	14637120	        80.41 ns/op	       0 B/op	       0 allocs/op
BenchmarkPositiveLargeWithFunction-8        	  727724	      1551 ns/op	       0 B/op	       0 allocs/op
BenchmarkNegativeLargeWithFunction-8        	  936205	      1275 ns/op	       0 B/op	       0 allocs/op
BenchmarkNegativeLargeSliceWithFunction-8   	  845863	      1269 ns/op	       0 B/op	       0 allocs/op
PASS
ok  	github.com/HoskeOwl/rslicer	16.561s

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrOutOfRange error = errors.New("Rune indexes out of range")
)

Functions

func GetRuneRange

func GetRuneRange(source string, begin, end int) (int, int, error)

func GetSliceByRunes

func GetSliceByRunes(source string, begin, end int) (string, error)

Types

This section is empty.

Jump to

Keyboard shortcuts

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