scoop:scoop.readline:0.1.0
src/history/memory.scoop
Selected production source · src/history/memory.scoop
1
package scoop.readline.history3
import scoop.readline.readlineConfig5
import scoop.readline.Config6
import scoop.text.*8
/** Bounded, in-memory history with configurable consecutive-duplicate and space filtering. */9
public class MemoryHistory public constructor(config: Config = readlineConfig()): History {10
private val entries: ArrayList<String> = ArrayList<String>()11
internal var maxLength: Long = config.maxHistorySize12
internal var ignoreDuplicates: Boolean = config.historyIgnoreDuplicates13
internal var ignoreSpace: Boolean = config.historyIgnoreSpace14
private val caseSensitive: Boolean = config.historyCaseSensitive16
public override val size: Long get() = entries.size18
public override fun get(index: Long, direction: SearchDirection): SearchResult? =19
if (index < 0L || index >= size)20
None21
else22
Some(SearchResult(entries[index], index, 0L))24
public override fun add(line: String): Boolean {25
if (maxLength == 0L || line.byteLength == 0L)26
return false27
if (ignoreSpace && line.getCharAt(0L).toString().isBlank())28
return false29
if (ignoreDuplicates && size > 0L && entries[size - 1L] == line)30
return false31
if (size == maxLength)32
entries.removeAt(0L)33
entries.add(line)34
return true35
}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 = length44
while (size > length)45
entries.removeAt(0L)46
}47
public override fun setIgnoreDuplicates(ignore: Boolean) {48
ignoreDuplicates = ignore49
}50
public override fun setIgnoreSpace(ignore: Boolean) {51
ignoreSpace = ignore52
}53
public override fun search(54
term: String,55
start: Long,56
direction: SearchDirection57
): SearchResult? = findMatch(term, start, direction, false)58
public override fun startsWith(59
term: String,60
start: Long,61
direction: SearchDirection62
): SearchResult? = findMatch(term, start, direction, true)64
private fun findMatch(65
term: String,66
start: Long,67
direction: SearchDirection,68
prefix: Boolean69
): SearchResult? {70
if (term.byteLength == 0L || start < 0L || start >= size)71
return None72
var index = start73
while (index >= 0L && index < size) {74
val entry = entries[index]75
val position =76
if (!caseSensitive)77
foldedMatch(entry, term, prefix)78
else79
if (prefix) {80
if (entry.startsWith(term))81
term.charLength82
else83
-1L84
} else85
entry.indexOf(term)86
if (position >= 0L)87
return Some(SearchResult(entry, index, position))88
index += if (direction == SearchDirection.Forward)89
1L90
else91
-1L92
}93
return None94
}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.maxLength103
this.historyIgnoreDuplicates = this @MemoryHistory.ignoreDuplicates104
this.historyIgnoreSpace = this @MemoryHistory.ignoreSpace105
this.historyCaseSensitive = this @MemoryHistory.caseSensitive106
}107
)108
}