Skip to content

Commit

Permalink
More localized Text string performance improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
aabewhite committed Aug 11, 2024
1 parent 1897116 commit eb0894d
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 13 deletions.
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ let package = Package(
],
dependencies: [
.package(url: "https://source.skip.tools/skip.git", from: "0.10.2"),
.package(url: "https://source.skip.tools/skip-model.git", from: "0.8.0"),
.package(url: "https://source.skip.tools/skip-model.git", from: "0.8.2"),
],
targets: [
.target(name: "SkipUI", dependencies: [.product(name: "SkipModel", package: "skip-model")], plugins: [.plugin(name: "skipstone", package: "skip")]),
Expand Down
44 changes: 33 additions & 11 deletions Sources/SkipUI/SkipUI/Text/LocalizedStringKey.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ public struct LocalizedStringKey : ExpressibleByStringInterpolation, Equatable {
/// The type that should be used for literal segments.
public typealias StringLiteralType = String

var values: [AnyHashable] = []
#if SKIP
let values: MutableList<AnyHashable> = mutableListOf()
#endif
var pattern = ""

public init(literalCapacity: Int, interpolationCount: Int) {
Expand All @@ -56,52 +58,72 @@ public struct LocalizedStringKey : ExpressibleByStringInterpolation, Equatable {
}

public mutating func appendInterpolation(_ string: String) {
values.append(string)
#if SKIP
values.add(string)
#endif
pattern += "%@"
}

public mutating func appendInterpolation(_ int: Int) {
values.append(int)
#if SKIP
values.add(int)
#endif
pattern += "%lld"
}

public mutating func appendInterpolation(_ int: Int16) {
values.append(int)
#if SKIP
values.add(int)
#endif
pattern += "%d"
}

public mutating func appendInterpolation(_ int: Int64) {
values.append(int)
#if SKIP
values.add(int)
#endif
pattern += "%lld"
}

public mutating func appendInterpolation(_ int: UInt) {
values.append(int)
#if SKIP
values.add(int)
#endif
pattern += "%llu"
}

public mutating func appendInterpolation(_ int: UInt16) {
values.append(int)
#if SKIP
values.add(int)
#endif
pattern += "%u"
}

public mutating func appendInterpolation(_ int: UInt64) {
values.append(int)
#if SKIP
values.add(int)
#endif
pattern += "%llu"
}

public mutating func appendInterpolation(_ double: Double) {
values.append(double)
#if SKIP
values.add(double)
#endif
pattern += "%lf"
}

public mutating func appendInterpolation(_ float: Float) {
values.append(float)
#if SKIP
values.add(float)
#endif
pattern += "%f"
}

public mutating func appendInterpolation<T: Hashable>(_ value: T) {
values.append(value as AnyHashable)
#if SKIP
values.add(value as AnyHashable)
#endif
pattern += "%@"
}

Expand Down
2 changes: 1 addition & 1 deletion Sources/SkipUI/SkipUI/Text/Text.swift
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ struct _Text: View, Equatable {
let locfmt = self.bundle?.localizedBundle(locale: locale).localizedKotlinFormatString(forKey: key.patternFormat, value: nil, table: self.tableName) ?? key.patternFormat.kotlinFormatString

// re-interpret the placeholder strings in the resulting localized string with the string interpolation's values
let replaced = locfmt.format(*key.stringInterpolation.values.kotlin(nocopy: true).toTypedArray())
let replaced = locfmt.format(*key.stringInterpolation.values.toTypedArray())
return replaced
}

Expand Down

0 comments on commit eb0894d

Please sign in to comment.