Skip to content

Commit

Permalink
Use DefaultStringInterpolation for repeated strings
Browse files Browse the repository at this point in the history
  • Loading branch information
JanGorman committed Apr 7, 2019
1 parent 1ffeeec commit 7b8521c
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 35 deletions.
4 changes: 0 additions & 4 deletions Sources/Border.swift
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
//
// Border.swift
// Table
//
// Created by Jan GORMAN on 28/05/2017.
//
//

public protocol Border {
var topBody: String { get }
Expand Down
4 changes: 0 additions & 4 deletions Sources/Column.swift
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
//
// Column.swift
// Table
//
// Created by Jan GORMAN on 28/05/2017.
//
//

public enum Alignment {
case left, center, right
Expand Down
4 changes: 0 additions & 4 deletions Sources/Configuration.swift
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
//
// Configuration.swift
// Table
//
// Created by Jan GORMAN on 28/05/2017.
//
//

import Foundation

Expand Down
15 changes: 15 additions & 0 deletions Sources/DefaultStringInterpolation+Table.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//
// Created by Jan Gorman on 07.04.19.
//

import Foundation

extension DefaultStringInterpolation {

mutating func appendInterpolation(repeat str: String, _ count: Int) {
for _ in 0..<count {
appendInterpolation(str)
}
}

}
17 changes: 0 additions & 17 deletions Sources/String+Extensions.swift

This file was deleted.

12 changes: 6 additions & 6 deletions Sources/Table.swift
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public final class Table {

private func align(row: String, width: Int, alignment: Alignment) -> String {
if row.count == 0 {
return " ".repeated(times: width)
return "\(repeat: " ", width)"
}

var availableWidth = width - row.count
Expand All @@ -85,16 +85,16 @@ public final class Table {
}

private func alignLeft(row: String, availableWidth: Int) -> String {
return row + " ".repeated(times: availableWidth)
return "\(row)\(repeat: " ", availableWidth)"
}

private func alignCenter(row: String, availableWidth: Int) -> String {
let halfWidth = availableWidth / 2
return " ".repeated(times: halfWidth) + row + " ".repeated(times: halfWidth)
return "\(repeat: " ", halfWidth)\(row)\(repeat: " ", halfWidth)"
}

private func alignRight(row: String, availableWidth: Int) -> String {
return " ".repeated(times: availableWidth) + row
return "\(repeat: " ", availableWidth)\(row)"
}

private func makeConfiguration(rows: [[String]]) -> Configuration {
Expand Down Expand Up @@ -132,7 +132,7 @@ public final class Table {
return rows.map {
$0.enumerated().map {
let column = configuration.columns[$0]
return " ".repeated(times: column.paddingLeft ?? 0) + $1 + " ".repeated(times: column.paddingRight ?? 0)
return "\(repeat: " ", column.paddingLeft ?? 0)\($1)\(repeat: " ", column.paddingRight ?? 0)"
}
}
}
Expand Down Expand Up @@ -172,7 +172,7 @@ public final class Table {

private func drawBorder(columnWidths: [Int], body: String, join: String, left: String, right: String) -> String {
let columns = columnWidths.map {
body.repeated(times: $0)
"\(repeat: body, $0)"
}.joined(separator: join)

return left + columns + right + "\n"
Expand Down

0 comments on commit 7b8521c

Please sign in to comment.