Skip to content
This repository has been archived by the owner on Mar 6, 2024. It is now read-only.

Commit

Permalink
Closes #1486: Fix string extensions indexing issues (#874)
Browse files Browse the repository at this point in the history
The string extensions method _truncated_ did not truncate the string to the specified limit including the leader.

For example, with a limit of 15 and the deafult leader the truncated string length was 18 for .head and 16 for .tail and .middle
  • Loading branch information
flbaue authored and sblatz committed Oct 23, 2018
1 parent b2a97bc commit 316bf0a
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 7 deletions.
4 changes: 4 additions & 0 deletions Blockzilla.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
objects = {

/* Begin PBXBuildFile section */
050E8B1E2064FECE00DF6090 /* StringExtensionTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 050E8B1D2064FECE00DF6090 /* StringExtensionTest.swift */; };
0B0D6BC41F3CDDBB00497D08 /* CollapsedURLTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0B0D6BC31F3CDDBB00497D08 /* CollapsedURLTest.swift */; };
0B37F9A61E2FCAD4002DF74B /* SearchProviderTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0B37F9A51E2FCAD4002DF74B /* SearchProviderTest.swift */; };
0B70C1631DE6128900CEF7E0 /* WebsiteMemoryTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0B70C1621DE6128900CEF7E0 /* WebsiteMemoryTest.swift */; };
Expand Down Expand Up @@ -248,6 +249,7 @@
/* End PBXCopyFilesBuildPhase section */

/* Begin PBXFileReference section */
050E8B1D2064FECE00DF6090 /* StringExtensionTest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StringExtensionTest.swift; sourceTree = "<group>"; };
0B0D6BC31F3CDDBB00497D08 /* CollapsedURLTest.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CollapsedURLTest.swift; sourceTree = "<group>"; };
0B37F9A51E2FCAD4002DF74B /* SearchProviderTest.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SearchProviderTest.swift; sourceTree = "<group>"; };
0B4868651F2250A3008C3C09 /* PastenGoTest.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PastenGoTest.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -1196,6 +1198,7 @@
D8E015601FCF409F00CA3B9F /* Info.plist */,
D831FEE1205247A400EAE19A /* BrowserViewControllerTests.swift */,
D8E0156D1FD9E40F00CA3B9F /* DomainCompletionTests.swift */,
050E8B1D2064FECE00DF6090 /* StringExtensionTest.swift */,
D803D37320EF1A49001F2819 /* UserAgentTests.swift */,
);
path = ClientTests;
Expand Down Expand Up @@ -1853,6 +1856,7 @@
D803D37420EF1A49001F2819 /* UserAgentTests.swift in Sources */,
D831FEE2205247A400EAE19A /* BrowserViewControllerTests.swift in Sources */,
D8E0155F1FCF409F00CA3B9F /* SearchEngineManagerTests.swift in Sources */,
050E8B1E2064FECE00DF6090 /* StringExtensionTest.swift in Sources */,
D8E0156E1FD9E40F00CA3B9F /* DomainCompletionTests.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down
14 changes: 7 additions & 7 deletions Blockzilla/StringExtensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,22 @@ extension String {
}

func truncated(limit: Int, position: TruncationPosition = .tail, leader: String = "...") -> String {
guard self.count > limit else { return self }
guard count > limit else { return self }

switch position {
case .head:
let truncated = self[self.index(startIndex, offsetBy: limit - leader.count)...]
let truncated = self[index(endIndex, offsetBy: leader.count - limit)...]
return leader + truncated
case .middle:
let headCharactersCount = Int(ceil(Float(limit - leader.count) / 2.0))
let head = self[...self.index(startIndex, offsetBy: headCharactersCount)]
let headCharactersCount = (limit - leader.count) / 2
let head = self[..<index(startIndex, offsetBy: headCharactersCount)]

let tailCharactersCount = Int(floor(Float(limit - leader.count) / 2.0))
let tail = self[self.index(endIndex, offsetBy: -tailCharactersCount)...]
let tailCharactersCount = Int(ceil(Double(limit - leader.count) / 2.0))
let tail = self[index(endIndex, offsetBy: -tailCharactersCount)...]

return head + leader + tail
case .tail:
let truncated = self[...self.index(startIndex, offsetBy: limit - leader.self.count)]
let truncated = self[..<index(startIndex, offsetBy: limit - leader.count)]
return truncated + leader
}
}
Expand Down
51 changes: 51 additions & 0 deletions ClientTests/StringExtensionTest.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

import XCTest
@testable import Firefox_Focus

class StringExtensionTest: XCTestCase {


func testStringShouldBeTruncated() {
let originalString = "This is a long title string"

let headTruncatedString = originalString.truncated(limit: 15, position: .head, leader: "..")
XCTAssertEqual(headTruncatedString.count, 15)
XCTAssertEqual(headTruncatedString, ".. title string")

let middleTruncatedString = originalString.truncated(limit: 15, position: .middle, leader: "..")
XCTAssertEqual(middleTruncatedString.count, 15)
XCTAssertEqual(middleTruncatedString, "This i.. string")

let tailTruncatedString = originalString.truncated(limit: 15, position: .tail, leader: "..")
XCTAssertEqual(tailTruncatedString.count, 15)
XCTAssertEqual(tailTruncatedString, "This is a lon..")
}

func testStringShouldStartWithString() {
let originalString = "This is a long title string"
let startString = "This is"

let isStarting = originalString.startsWith(other: startString)
XCTAssertEqual(isStarting, true)
}

func testStringShouldStartWithEmptyString() {
let originalString = "This is a long title string"
let startString = ""

let isStarting = originalString.startsWith(other: startString)
XCTAssertEqual(isStarting, true)
}

func testStringShouldNotStartWithString() {
let originalString = "This is a long title string"
let startString = "TX"

let isStarting = originalString.startsWith(other: startString)
XCTAssertEqual(isStarting, false)
}

}

0 comments on commit 316bf0a

Please sign in to comment.