Skip to content

Commit 06a06ed

Browse files
authored
Conform Substring to RESPStringRenderable (#111)
1 parent 4d963b5 commit 06a06ed

File tree

3 files changed

+22
-0
lines changed

3 files changed

+22
-0
lines changed

Sources/Valkey/RESP/RESPRenderable.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,16 @@ extension String: RESPRenderable {
5454
}
5555
}
5656

57+
extension Substring: RESPRenderable {
58+
@inlinable
59+
public var respEntries: Int { 1 }
60+
61+
@inlinable
62+
public func encode(into commandEncoder: inout ValkeyCommandEncoder) {
63+
commandEncoder.encodeBulkString(self)
64+
}
65+
}
66+
5767
extension Array: RESPRenderable where Element: RESPRenderable {
5868
@inlinable
5969
public var respEntries: Int {

Sources/Valkey/RESP/RESPStringRenderable.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ public protocol RESPStringRenderable: Sendable, Hashable {
2727

2828
extension String: RESPStringRenderable {}
2929

30+
extension Substring: RESPStringRenderable {}
31+
3032
extension ByteBuffer: RESPStringRenderable {
3133
public func encode(into commandEncoder: inout ValkeyCommandEncoder) {
3234
commandEncoder.encodeBulkString(self.readableBytesView)

Sources/Valkey/ValkeyCommandEncoder.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,16 @@ public struct ValkeyCommandEncoder {
3838
buffer.writeStaticString("\r\n")
3939
}
4040

41+
@inlinable
42+
mutating func encodeBulkString(_ string: Substring) {
43+
encodeIdentifier(.bulkString)
44+
45+
buffer.writeString(String(string.utf8.count))
46+
buffer.writeStaticString("\r\n")
47+
buffer.writeSubstring(string)
48+
buffer.writeStaticString("\r\n")
49+
}
50+
4151
@inlinable
4252
mutating func encodeBulkString<Bytes: Collection>(_ string: Bytes) where Bytes.Element == UInt8 {
4353
encodeIdentifier(.bulkString)

0 commit comments

Comments
 (0)