From 6cf724791a8c37d65774d94740fa5782b030c4fb Mon Sep 17 00:00:00 2001 From: DmitriyMusatkin Date: Mon, 5 Aug 2024 11:06:49 -0700 Subject: [PATCH] cleanup --- .../AwsCommonRuntimeKit/crt/Utilities.swift | 19 ++++++++++--------- .../endpoint/EndpointsRequestContext.swift | 2 +- .../crt/Utilities.swift | 4 ++-- 3 files changed, 13 insertions(+), 12 deletions(-) diff --git a/Source/AwsCommonRuntimeKit/crt/Utilities.swift b/Source/AwsCommonRuntimeKit/crt/Utilities.swift index 6c05fd30..2bb3f691 100644 --- a/Source/AwsCommonRuntimeKit/crt/Utilities.swift +++ b/Source/AwsCommonRuntimeKit/crt/Utilities.swift @@ -274,16 +274,17 @@ func withByteCursorFromStrings( } extension Array where Element == String { - func withByteCursorArray( - let cStrings = arg.map { strdup($0) } + [nil] - let cursors = cStrings.map { aws_byte_cursor_from_c_str($0) } - let len = cursors.count + func withByteCursorArray(_ body: (UnsafePointer, 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) + } } } diff --git a/Source/AwsCommonRuntimeKit/sdkutils/endpoint/EndpointsRequestContext.swift b/Source/AwsCommonRuntimeKit/sdkutils/endpoint/EndpointsRequestContext.swift index cc4b9f2e..d37f992e 100644 --- a/Source/AwsCommonRuntimeKit/sdkutils/endpoint/EndpointsRequestContext.swift +++ b/Source/AwsCommonRuntimeKit/sdkutils/endpoint/EndpointsRequestContext.swift @@ -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 { diff --git a/Test/AwsCommonRuntimeKitTests/crt/Utilities.swift b/Test/AwsCommonRuntimeKitTests/crt/Utilities.swift index 056167cb..b43c41f4 100644 --- a/Test/AwsCommonRuntimeKitTests/crt/Utilities.swift +++ b/Test/AwsCommonRuntimeKitTests/crt/Utilities.swift @@ -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..