Swift Package Manager支持正则表达式字面量吗?

huangapple go评论66阅读模式
英文:

Does the Swift Package Manager support regex literals?

问题

I am using the Swift Package Manager, with Swift 5.7.1. I would like to compile code with a regex literal. For example, main.swift could be as follows:

let message = "Hello, world"
print(message.replacing(/[aeiou]/, with: "[vowel]"))

I am not at present interested in the output, in with the compilation.

If I create a project in XCode, it works just fine. However, if I try it in the Swift Package Manager (using swift build) it obviously does not recognize that /[aeiou]/ is a regex literal. Instead, I get messages like '/'' is not a prefix unary operator.

Is there any way to get this to work with the Swift Package Manager, or is it simply not yet supported?

英文:

I am using the Swift Package Manager, with Swift 5.7.1. I would like to compile code with a regex literal. For example, main.swift could be as follows:

let message = "Hello, world"
print(message.replacing(/[aeiou]/, with: "[vowel]"))

I am not at present interested in the output, in with the compilation.

If I create a project in XCode, it works just fine. However, if I try it in the Swift Package Manager (using swift build) it obviously does not recognize that /[aeiou]/ is a regex literal. Instead, I get messages like '/' is not a prefix unary operator.

Is there any way to get this to work with the Swift Package Manager, or is it simply not yet supported?

答案1

得分: 10

Swift 5.7的正则表达式字面量在使用Swift Package Manager时可用,但必须在你的`Package.swift`中显式启用。默认情况下,新的语法被禁用,因为它是一个源代码破坏性的语言更改(由于现有的在注释语法和运算符中使用`/`操作符)。

这是一个示例的`Package.swift`,它为所有的包目标设置了“-enable-bare-slash-regex”。如果你正在全面采用Swift 5.7,你可能还希望包含一些其他的设置:

```swift
// swift-tools-version: 5.7

import PackageDescription

let package = Package(
  name: "your-package-name",
  platforms: [...],
  dependencies: [...],
  targets: [
    .target(
      name: "FooBar",
      dependencies: []
    ),
    .testTarget(
      name: "FooBarTests",
      dependencies: [
        "FooBar",
      ]
    ),
  ]
)

for target in package.targets {
  target.swiftSettings = target.swiftSettings ?? []
  target.swiftSettings?.append(
    .unsafeFlags([
      "-Xfrontend", "-warn-concurrency",
      "-Xfrontend", "-enable-actor-data-race-checks",
      "-enable-bare-slash-regex",
    ])
  )
}

在未来的Swift版本中,一旦现有的代码有足够的时间过渡到Swift 5.7,正则表达式字面量可能会默认启用。


Swift 5.8更新

Swift 5.8发布支持逐步采用即将到来的语言改进enableUpcomingFeature帮助避免了“使用不安全标志会使包含此目标的产品不能被其他包使用”的情况。

let swiftSettings: [SwiftSetting] = [
    // -enable-bare-slash-regex 变成
    .enableUpcomingFeature("BareSlashRegexLiterals"), 
    // -warn-concurrency 变成
    .enableUpcomingFeature("StrictConcurrency"), 
    .unsafeFlags(["-enable-actor-data-race-checks"], 
        .when(configuration: .debug)),
]

for target in package.targets {
    target.swiftSettings = target.swiftSettings ?? []
    target.swiftSettings?.append(contentsOf: swiftSettings)
}
英文:

Swift 5.7's regex literals can work when using the Swift Package Manager, but they must be explicitly enabled in your Package.swift. By default, the new syntax is disabled since it's a source-breaking language change (due to the existing use of "/" in comment syntax and operators the / operator).

Here's an illustrative Package.swift that sets "-enable-bare-slash-regex" for all the package targets. I've also included a couple of additional settings that you may want if you're working towards fully adopting Swift 5.7:

// swift-tools-version: 5.7

import PackageDescription

let package = Package(
  name: "your-package-name",
  platforms: [...],
  dependencies: [...],
  targets: [
    .target(
      name: "FooBar",
      dependencies: []
    ),
    .testTarget(
      name: "FooBarTests",
      dependencies: [
        "FooBar",
      ]
    ),
  ]
)

for target in package.targets {
  target.swiftSettings = target.swiftSettings ?? []
  target.swiftSettings?.append(
    .unsafeFlags([
      "-Xfrontend", "-warn-concurrency",
      "-Xfrontend", "-enable-actor-data-race-checks",
      "-enable-bare-slash-regex",
    ])
  )
}

In a future Swift version and once there’s been time for existing code to transition to Swift 5.7, regex literals will likely be enabled by default.


Swift 5.8 Update

The Swift 5.8 release supports the piecemeal adoption of upcoming language improvements. The enableUpcomingFeature helps avoid the situation where "use of unsafe flags make the products containing this target ineligible for use by other packages".

let swiftSettings: [SwiftSetting] = [
    // -enable-bare-slash-regex becomes
    .enableUpcomingFeature("BareSlashRegexLiterals"), 
    // -warn-concurrency becomes
    .enableUpcomingFeature("StrictConcurrency"), 
    .unsafeFlags(["-enable-actor-data-race-checks"], 
        .when(configuration: .debug)),
]

for target in package.targets {
    target.swiftSettings = target.swiftSettings ?? []
    target.swiftSettings?.append(contentsOf: swiftSettings)
}

huangapple
  • 本文由 发表于 2023年2月27日 01:07:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/75573646.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定