Uploaded image for project: 'Swift'
  1. Swift
  2. SR-9749

The encodedOffset of String.Index has been changed to either UTF-8 or UTF-16.

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Resolved
    • Priority: Medium
    • Resolution: Done
    • Component/s: Standard Library
    • Labels:
      None
    • Environment:

      swift-DEVELOPMENT-SNAPSHOT-2019-01-23-a
      swift-5.0-DEVELOPMENT-SNAPSHOT-2019-01-23-a

      Description

      The problem is:

      1. Before merging https://github.com/apple/swift/pull/20315, The encodedOffset of String.Index was specified as UTF-16 offset, and it actually worked as expected.

        /// The offset into a string's UTF-16 encoding for this index.
        @inlinable // FIXME(sil-serialize-all)
        public var encodedOffset : Int {

        https://github.com/apple/swift/pull/20315/files#diff-8ec9c169d4bc3c678838b4067481979eL130

        /// Creates a new index at the specified UTF-16 offset.
        ///
        /// - Parameter offset: An offset in UTF-16 code units.
        @inlinable // FIXME(sil-serialize-all)
        public init(encodedOffset offset: Int) {

        https://github.com/apple/swift/pull/20315/files#diff-8ec9c169d4bc3c678838b4067481979eL106

      2. A lot of codes were written that expect that behavior (including swift-corelibs-foundation)
        e.g. codes using encodedOffset in Swift on GitHub can be found: https://github.com/search?l=Swift&q=encodedOffset&type=Code
      3. However, the change is not written in changelog etc., and it is understood only from the description of UTF-16 deleted from each documentation comment. In fact, swift-corelibs-foundation did not recognize that change was necessary, which caused SR-9454.

      I think that some countermeasures are necessary:

      • [MUST] Behavior changes should be documented (eg within changelog).
      • [MAY] Some code change
        Mark init(encodedOffset:) and encodedOffset as deprecated
        or
        Introduce new property exposing encode information(UTF-8 or UTF-16) to String

      /cc: Michael Ilseman

        Attachments

          Activity

            People

            • Assignee:
              milseman Michael Ilseman
              Reporter:
              norio_nomura Norio Nomura
            • Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: