Skip to content

Commit

Permalink
simplify
Browse files Browse the repository at this point in the history
  • Loading branch information
DmitriyMusatkin committed Aug 2, 2024
1 parent a7d01a7 commit 6d7c7db
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 45 deletions.
37 changes: 6 additions & 31 deletions Source/AwsCommonRuntimeKit/crt/Utilities.swift
Original file line number Diff line number Diff line change
Expand Up @@ -273,36 +273,11 @@ func withByteCursorFromStrings<Result>(
}
}

public func scan<S: Sequence, U>(_ seq: S, _ initial: U, _ combine: (U, S.Element) -> U) -> [U] {
var result: [U] = []
result.reserveCapacity(seq.underestimatedCount)
var runningResult = initial
for element in seq {
runningResult = combine(runningResult, element)
result.append(runningResult)
}
return result
}

/// Note: this function and associated scan function above are copied verbatim from
/// swift standard lib where its private.
public func withArrayOfCStrings<R>(
_ args: [String], _ body: ([UnsafeMutablePointer<CChar>?]) -> R
) -> R {
let argsCounts = Array(args.map { $0.utf8.count + 1 })
let argsOffsets = [ 0 ] + scan(argsCounts, 0, +)
let argsBufferSize = argsOffsets.last!
var argsBuffer: [UInt8] = []
argsBuffer.reserveCapacity(argsBufferSize)
for arg in args {
argsBuffer.append(contentsOf: arg.utf8)
argsBuffer.append(0)
}
return argsBuffer.withUnsafeMutableBufferPointer { (argsBuffer) in
let ptr = UnsafeMutableRawPointer(argsBuffer.baseAddress!).bindMemory(
to: CChar.self, capacity: argsBuffer.count)
var cStrings: [UnsafeMutablePointer<CChar>?] = argsOffsets.map { ptr + $0 }
cStrings[cStrings.count - 1] = nil
return body(cStrings)
func withByteCursorArrayFromStringArray<R>(
_ arg: [String], _ body: (UnsafePointer<aws_byte_cursor>, Int) -> R) -> R {
let cursors = arg.map { aws_byte_cursor_from_c_str($0) }
let len = cursors.count
return cursors.withUnsafeBufferPointer { cursorsPtr in
return body(cursorsPtr.baseAddress!, len)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,17 +48,6 @@ public class EndpointsRequestContext {
}
}

private func withByteCursorArrayFromStringArray<R>(
_ arg: [String], _ body: (UnsafeMutablePointer<aws_byte_cursor>, Int) -> R) -> R {
withArrayOfCStrings(arg) { cStrArr in
var cursors = cStrArr.map { aws_byte_cursor_from_c_str($0) }
let len = cursors.count
return cursors.withUnsafeMutableBufferPointer { cursorsPtr in
return body(cursorsPtr.baseAddress!, len)
}
}
}

/// Add a string array endpoint parameter to the request context
/// - Parameters:
/// - name: The name of the parameter
Expand Down
27 changes: 27 additions & 0 deletions Test/AwsCommonRuntimeKitTests/crt/Utilities.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0.

import XCTest
@testable import AwsCommonRuntimeKit
import AwsCCommon

class UtilitiesTests: XCBaseTestCase {

func testStringArrayToByteCursorArray() {
let strings1 = ["a", "b"]
withByteCursorArrayFromStringArray(strings1) { cursors, len in
XCTAssertEqual(len, 2)
for i in 0..<len {
withUnsafePointer(to: cursors[i]) { pointer in
XCTAssertTrue(aws_byte_cursor_is_valid(pointer))
}
}
}

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

Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class EndpointsRuleEngineTests: XCBaseTestCase {
"partitions": [
{
"id": "aws",
"regionRegex": "^(us|eu|ap|sa|ca|me|af)-\\w+-\\d+$",
"regionRegex": "^(us|eu|ap|sa|ca|me|af)\\-\\w+\\-\\d+$",
"regions": {
"af-south-1": {},
"af-east-1": {},
Expand Down Expand Up @@ -288,5 +288,6 @@ class EndpointsRuleEngineTests: XCBaseTestCase {
try! context.add(name: "Region", value: "us-west-2")
try! context.add(name: "Boolean", value: true)
try! context.add(name: "StringArray", value: ["a", "b"])
try! context.add(name: "StringArray", value: [])
}
}
2 changes: 1 addition & 1 deletion aws-common-runtime/aws-c-io
Submodule aws-c-io updated 1 files
+2 −2 tests/socket_test.c

0 comments on commit 6d7c7db

Please sign in to comment.