Skip to content

Commit

Permalink
Move QuantityComparisonResult to StringBasics
Browse files Browse the repository at this point in the history
  • Loading branch information
1024jp committed Jul 9, 2024
1 parent 81e35d0 commit fc36b45
Show file tree
Hide file tree
Showing 7 changed files with 93 additions and 44 deletions.
2 changes: 1 addition & 1 deletion CotEditor.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -2091,11 +2091,11 @@
2AED46721E43942300751C45 /* TextFinderTests.swift */,
2AC72EA1253478D5001D3CA0 /* FileDropItemTests.swift */,
2ABEFB6923DC0CA0008769F4 /* EditorCounterTests.swift */,
2A80BE8F27FFFA8900D2F7FF /* LineEndingScannerTests.swift */,
2A1125C023F180FF006A1DB2 /* LineRangeCacheableTests.swift */,
2A8E47E4299A2401006A40D8 /* EditedRangeSetTests.swift */,
2A3F8F672429E04000CBBA89 /* DebouncerTests.swift */,
2A04E9BA27FD6911008C82D8 /* SnippetTests.swift */,
2A80BE8F27FFFA8900D2F7FF /* LineEndingScannerTests.swift */,
2A57B991294EDD9600771696 /* FormatStylesTests.swift */,
);
name = Models;
Expand Down
28 changes: 0 additions & 28 deletions CotEditor/Sources/Collection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -190,12 +190,6 @@ extension MutableCollection where Self: RandomAccessCollection {

// MARK: - Count

enum QuantityComparisonResult {

case less, equal, greater
}


extension Sequence {

/// Counts up elements by enumerating collection until encountering the element that doesn't satisfy the given predicate.
Expand All @@ -208,26 +202,4 @@ extension Sequence {

try self.prefix(while: predicate).count
}


/// Performance efficient way to compare the number of elements with the given number.
///
/// - Note: This method takes advantage especially when counting elements is heavy (such as String count) and the number to compare is small.
///
/// - Parameter number: The number of elements to test.
/// - Returns: The result whether the number of the elements in the receiver is less than, equal, or more than the given number.
func compareCount(with number: Int) -> QuantityComparisonResult {

assert(number >= 0, "The count number to compare should be a natural number.")

guard number >= 0 else { return .greater }

var count = 0
for _ in self {
count += 1
if count > number { return .greater }
}

return (count == number) ? .equal : .less
}
}
1 change: 1 addition & 0 deletions CotEditor/Sources/EditorCounter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

import Foundation
import Observation
import StringBasics

@MainActor protocol EditorSource: AnyObject {

Expand Down
1 change: 1 addition & 0 deletions CotEditor/Sources/EditorTextViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import ControlUI
import CharacterInfo
import Defaults
import FuzzyRange
import StringBasics
import TextClipping

final class EditorTextViewController: NSViewController, NSServicesMenuRequestor, NSTextViewDelegate {
Expand Down
36 changes: 36 additions & 0 deletions Packages/EditorCore/Sources/StringBasics/Collection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
// limitations under the License.
//

// MARK: - unique

public extension Sequence where Element: Equatable {

/// An array consists of unique elements of receiver by keeping ordering.
Expand All @@ -47,3 +49,37 @@ public extension Array where Element: Equatable {
self = self.uniqued
}
}



// MARK: - Count

public enum QuantityComparisonResult {

case less, equal, greater
}


public extension Sequence {

/// Performance efficient way to compare the number of elements with the given number.
///
/// - Note: This method takes advantage especially when counting elements is heavy (such as String count) and the number to compare is small.
///
/// - Parameter number: The number of elements to test.
/// - Returns: The result whether the number of the elements in the receiver is less than, equal, or more than the given number.
func compareCount(with number: Int) -> QuantityComparisonResult {

assert(number >= 0, "The count number to compare should be a natural number.")

guard number >= 0 else { return .greater }

var count = 0
for _ in self {
count += 1
if count > number { return .greater }
}

return (count == number) ? .equal : .less
}
}
54 changes: 54 additions & 0 deletions Packages/EditorCore/Tests/StringBasicsTests/CollectionTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
//
// CollectionTests.swift
// StringBasicsTests
//
// CotEditor
// https://coteditor.com
//
// Created by 1024jp on 2024-07-09.
//
// ---------------------------------------------------------------------------
//
// Β© 2024 1024jp
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//


import Testing
@testable import StringBasics

struct CollectionTests {

@Test func unique() {

#expect([String]().uniqued.isEmpty)
#expect(["dog"].uniqued == ["dog"])
#expect(["dog", "dog", "cat", "cow", "cat", "dog"].uniqued == ["dog", "cat", "cow"])
}


@Test func compareCount() {

#expect("".compareCount(with: 0) == .equal)
#expect("".compareCount(with: 1) == .less)

#expect("a".compareCount(with: 1) == .equal)
#expect("πŸ•".compareCount(with: 1) == .equal)
#expect("πŸ•β€πŸ¦Ί".compareCount(with: 1) == .equal)

#expect("🐢🐱".compareCount(with: 3) == .less)
#expect("🐢🐱".compareCount(with: 2) == .equal)
#expect("🐢🐱".compareCount(with: 1) == .greater)
}
}
15 changes: 0 additions & 15 deletions Tests/CollectionTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -55,21 +55,6 @@ struct CollectionTests {
}


@Test func compareCount() {

#expect("".compareCount(with: 0) == .equal)
#expect("".compareCount(with: 1) == .less)

#expect("a".compareCount(with: 1) == .equal)
#expect("πŸ•".compareCount(with: 1) == .equal)
#expect("πŸ•β€πŸ¦Ί".compareCount(with: 1) == .equal)

#expect("🐢🐱".compareCount(with: 3) == .less)
#expect("🐢🐱".compareCount(with: 2) == .equal)
#expect("🐢🐱".compareCount(with: 1) == .greater)
}


@Test func mapKeys() {

let dict = [1: 1, 2: 2, 3: 3]
Expand Down

0 comments on commit fc36b45

Please sign in to comment.