Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
DmitriyMusatkin committed Aug 5, 2024
1 parent ad8bb7a commit 6cf7247
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 12 deletions.
19 changes: 10 additions & 9 deletions Source/AwsCommonRuntimeKit/crt/Utilities.swift
Original file line number Diff line number Diff line change
Expand Up @@ -274,16 +274,17 @@ func withByteCursorFromStrings<Result>(
}

extension Array where Element == String {
func withByteCursorArray<R>(
let cStrings = arg.map { strdup($0) } + [nil]
let cursors = cStrings.map { aws_byte_cursor_from_c_str($0) }
let len = cursors.count
func withByteCursorArray<R>(_ body: (UnsafePointer<aws_byte_cursor>, Int) -> R) -> R {
let cStrings = self.map { strdup($0) } + [nil]
let cursors = cStrings.map { aws_byte_cursor_from_c_str($0) }
let len = cursors.count

defer {
cStrings.forEach { free($0) }
}
defer {
cStrings.forEach { free($0) }
}

return cursors.withUnsafeBufferPointer { cursorsPtr in
return body(cursorsPtr.baseAddress!, len)
return cursors.withUnsafeBufferPointer { cursorsPtr in
return body(cursorsPtr.baseAddress!, len)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public class EndpointsRequestContext {
return
}
if (name.withByteCursor { nameCursor in
withByteCursorArrayFromStringArray(value) { ptr, len in
value.withByteCursorArray { ptr, len in
aws_endpoints_request_context_add_string_array(allocator.rawValue, rawValue, nameCursor, ptr, len)
}
}) != AWS_OP_SUCCESS {
Expand Down
4 changes: 2 additions & 2 deletions Test/AwsCommonRuntimeKitTests/crt/Utilities.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class UtilitiesTests: XCBaseTestCase {

func testStringArrayToByteCursorArray() {
let strings1 = ["a", "b"]
withByteCursorArrayFromStringArray(strings1) { cursors, len in
strings1.withByteCursorArray { cursors, len in
XCTAssertEqual(len, 2)
for i in 0..<len {
withUnsafePointer(to: cursors[i]) { pointer in
Expand All @@ -19,7 +19,7 @@ class UtilitiesTests: XCBaseTestCase {
}

let strings2: [String] = []
withByteCursorArrayFromStringArray(strings2) { cursors, len in
strings2.withByteCursorArray { cursors, len in
XCTAssertEqual(len, 0)
}
}
Expand Down

0 comments on commit 6cf7247

Please sign in to comment.