Scoop API

scoop:scoop.readline:0.1.0

src/history/memory.scoop

Selected production source · src/history/memory.scoop

1package scoop.readline.history
3import scoop.readline.readlineConfig
5import scoop.readline.Config
6import scoop.text.*
8/** Bounded, in-memory history with configurable consecutive-duplicate and space filtering. */
9public class MemoryHistory public constructor(config: Config = readlineConfig()): History {
10 private val entries: ArrayList<String> = ArrayList<String>()
11 internal var maxLength: Long = config.maxHistorySize
12 internal var ignoreDuplicates: Boolean = config.historyIgnoreDuplicates
13 internal var ignoreSpace: Boolean = config.historyIgnoreSpace
14 private val caseSensitive: Boolean = config.historyCaseSensitive
16 public override val size: Long get() = entries.size
18 public override fun get(index: Long, direction: SearchDirection): SearchResult? =
19 if (index < 0L || index >= size)
20 None
21 else
22 Some(SearchResult(entries[index], index, 0L))
24 public override fun add(line: String): Boolean {
25 if (maxLength == 0L || line.byteLength == 0L)
26 return false
27 if (ignoreSpace && line.getCharAt(0L).toString().isBlank())
28 return false
29 if (ignoreDuplicates && size > 0L && entries[size - 1L] == line)
30 return false
31 if (size == maxLength)
32 entries.removeAt(0L)
33 entries.add(line)
34 return true
35 }
37 public override fun clear() {
38 entries.clear()
39 }
40 public override fun setMaxLength(length: Long) {
41 if (length < 0L)
42 throw IllegalArgumentException(Some("negative history length"))
43 maxLength = length
44 while (size > length)
45 entries.removeAt(0L)
46 }
47 public override fun setIgnoreDuplicates(ignore: Boolean) {
48 ignoreDuplicates = ignore
49 }
50 public override fun setIgnoreSpace(ignore: Boolean) {
51 ignoreSpace = ignore
52 }
53 public override fun search(
54 term: String,
55 start: Long,
56 direction: SearchDirection
57 ): SearchResult? = findMatch(term, start, direction, false)
58 public override fun startsWith(
59 term: String,
60 start: Long,
61 direction: SearchDirection
62 ): SearchResult? = findMatch(term, start, direction, true)
64 private fun findMatch(
65 term: String,
66 start: Long,
67 direction: SearchDirection,
68 prefix: Boolean
69 ): SearchResult? {
70 if (term.byteLength == 0L || start < 0L || start >= size)
71 return None
72 var index = start
73 while (index >= 0L && index < size) {
74 val entry = entries[index]
75 val position =
76 if (!caseSensitive)
77 foldedMatch(entry, term, prefix)
78 else
79 if (prefix) {
80 if (entry.startsWith(term))
81 term.charLength
82 else
83 -1L
84 } else
85 entry.indexOf(term)
86 if (position >= 0L)
87 return Some(SearchResult(entry, index, position))
88 index += if (direction == SearchDirection.Forward)
89 1L
90 else
91 -1L
92 }
93 return None
94 }
96 /** Returns an independent snapshot, from oldest to newest. */
97 public fun toArray(): Array<String> = entries.toArray()
99 internal fun emptyCopy(): MemoryHistory =
100 MemoryHistory(
101 readlineConfig {
102 this.maxHistorySize = this @MemoryHistory.maxLength
103 this.historyIgnoreDuplicates = this @MemoryHistory.ignoreDuplicates
104 this.historyIgnoreSpace = this @MemoryHistory.ignoreSpace
105 this.historyCaseSensitive = this @MemoryHistory.caseSensitive
106 }
107 )