regexp_map

package module
v0.2.3 Latest Latest
Warning

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

Go to latest
Published: Aug 19, 2023 License: MIT Imports: 2 Imported by: 0

README

What is the Regexp Map

The Regexp Map is a specify Map. Its key can is a string or a a regexp. when the index is string. It will to index the string. If it didn't match. It will to match the regexp.

Note

  • Require Regexp is independent.
  • No Thread Safe

Usage

package main

import (
	"fmt"

	"github.com/CorrectRoadH/regexp_map"
)

func main() {
	reMap := regexp_map.Map[string]{}
	reMap.Store("https://youtube.com", "youtube")
	reMap.Store("https://bilibili.com", "bilibili")
	reMap.StoreRegex("^https?://(?:www\\.)?bilibili\\.com(/[\\w-]+)*/?(\\?[^#]*)?(#.*)?$", "bilibili")

	result1, ok, key := reMap.Load("https://youtube.com")
	fmt.Println(result1, ok, key) // youtube true https://youtube.com

	result2, ok, _ := reMap.Load("https://bilibili.com")
	fmt.Println(result2, ok) // bilibili true

	result3, ok, key := reMap.Load("https://www.bilibili.com/video/BV1394y1k7D2/")
	fmt.Println(result3, ok, key) // bilibili true ^https?://(?:www\.)?bilibili\.com(/[\w-]+)*/?(\?[^#]*)?(#.*)?$

	result4, ok, key := reMap.Load("https://discord.com")
	fmt.Println(result4, ok, key) //  false
}

Benchmark

The time cost is O(log(n)).

➜  regexp_map git:(main) ✗ go test -bench='Bench' -benchtime 5s ./...
?       github.com/CorrectRoadH/regexp_map      [no test files]
goos: darwin
goarch: arm64
pkg: github.com/CorrectRoadH/regexp_map/benchmark
Benchmark_store_and_load_pure_Regepx_V1-10         16027            635505 ns/op
Benchmark_store_and_load_string_V1-10           18337298               395.2 ns/op
Benchmark_store_and_load_mix_V1-10                 15450            622352 ns/op
PASS
ok      github.com/CorrectRoadH/regexp_map/benchmark    35.600s
PASS
ok      github.com/CorrectRoadH/regexp_map/test 1.807s

RoadMap

  • add thread safe
  • optimize the performance
  • add Regexp check
  • implment delete api
  • add more usage

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Map

type Map[T any] struct {
	RegexpTree *RegexpNode
	// contains filtered or unexported fields
}

func (*Map[T]) Delete

func (m *Map[T]) Delete(key string)

func (*Map[T]) Load

func (r *Map[T]) Load(key string) (actual T, loaded bool, match string)

建个二分树????🤪

func (*Map[T]) LoadAndDelete

func (m *Map[T]) LoadAndDelete(key string) (T, bool)

func (*Map[T]) Range

func (m *Map[T]) Range(f func(key string, value any) bool)

func (*Map[T]) Store

func (r *Map[T]) Store(key string, value T)

func (*Map[T]) StoreRegex

func (r *Map[T]) StoreRegex(key string, value T)

type RegexpMap

type RegexpMap[T any] interface {
	Store(key string, value T)
	StoreRegex(key string, value T)
	Load(key string) (T, bool, string)
	Delete(key string)
	Range(f func(key string, value any) bool)
}

type RegexpNode

type RegexpNode struct {
	Left      *RegexpNode
	Right     *RegexpNode
	Regexp    *regexp.Regexp
	RegexpRaw string
}

func (*RegexpNode) Find

func (r *RegexpNode) Find(content string) (string, bool)

func (*RegexpNode) Insert

func (r *RegexpNode) Insert(re string)

Jump to

Keyboard shortcuts

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