mirror of
https://github.com/fergalmoran/ladybird.git
synced 2025-12-22 09:19:03 +00:00
AK+Meta: Add SwiftAK module to add helpers to construct swift Strings
This allows constructing Foundation.Data and Swift.String without unnecessary copies from AK.StringView and AK.String respectively.
This commit is contained in:
committed by
Andrew Kaster
parent
349b17cc7a
commit
c1c7e5ff3e
25
AK/AK+Swift.swift
Normal file
25
AK/AK+Swift.swift
Normal file
@@ -0,0 +1,25 @@
|
||||
/*
|
||||
* Copyright (c) 2024, Andrew Kaster <akaster@serenityos.org>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
import AK
|
||||
import Foundation
|
||||
|
||||
public extension Foundation.Data {
|
||||
init(_ string: AK.StringView) {
|
||||
let bytes = string.bytes()
|
||||
self.init(bytesNoCopy: UnsafeMutableRawPointer(mutating: bytes.data()), count: bytes.size(), deallocator: .none)
|
||||
}
|
||||
}
|
||||
|
||||
public extension Swift.String {
|
||||
init?(_ string: AK.String) {
|
||||
self.init(data: Foundation.Data(string.__bytes_as_string_viewUnsafe()), encoding: .utf8)
|
||||
}
|
||||
|
||||
init?(_ string: AK.StringView) {
|
||||
self.init(data: Foundation.Data(string), encoding: .utf8)
|
||||
}
|
||||
}
|
||||
@@ -68,4 +68,8 @@ if (ENABLE_SWIFT)
|
||||
"${CMAKE_CURRENT_BINARY_DIR}/Backtrace.h"
|
||||
"${CMAKE_CURRENT_BINARY_DIR}/Debug.h"
|
||||
)
|
||||
|
||||
target_compile_features(AK PUBLIC cxx_std_23)
|
||||
set_target_properties(AK PROPERTIES Swift_MODULE_NAME SwiftAK)
|
||||
target_sources(AK PRIVATE AK+Swift.swift)
|
||||
endif()
|
||||
|
||||
Reference in New Issue
Block a user