File: intern.go

package info (click to toggle)
golang-github-pkg-diff 0.0~git20210226.20ebb0f-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 3,528 kB
  • sloc: makefile: 3
file content (25 lines) | stat: -rw-r--r-- 767 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
// Package intern provides string interning.
//
// Unlike much string interning, the routines in this package
// return *string instead of string. This enables extremely
// cheap (compare only a pointer) comparisons of any strings
// interned by this package. Since diff algorithms involve
// many string comparisons, this often ends up paying for the
// cost of the interning. Also, in the typical case,
// diffs involve lots of repeated lines (most of the file
// contents are typically unchanged, so any give line
// appears at least twice), so string interning saves memory.
package intern

type Strings map[string]*string

func (m Strings) FromBytes(b []byte) *string {
	p, ok := m[string(b)]
	if ok {
		return p
	}
	s := string(b)
	p = &s
	m[s] = p
	return p
}