Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions .github/workflows/swift.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# This workflow will build a Swift project
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-swift

name: Swift

on:
push:
branches: [ "master" ]
pull_request:
branches: [ "master" ]

jobs:
build:

runs-on: macos-latest

steps:
- uses: actions/checkout@v4
- name: Build
run: swift build -v
- name: Run tests
run: swift test -v
102 changes: 102 additions & 0 deletions .swiftpm/xcode/xcshareddata/xcschemes/HMFoundation.xcscheme
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "1520"
version = "1.7">
<BuildAction
parallelizeBuildables = "YES"
buildImplicitDependencies = "YES">
<BuildActionEntries>
<BuildActionEntry
buildForTesting = "YES"
buildForRunning = "YES"
buildForProfiling = "YES"
buildForArchiving = "YES"
buildForAnalyzing = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "HMFoundation"
BuildableName = "HMFoundation"
BlueprintName = "HMFoundation"
ReferencedContainer = "container:">
</BuildableReference>
</BuildActionEntry>
<BuildActionEntry
buildForTesting = "YES"
buildForRunning = "YES"
buildForProfiling = "NO"
buildForArchiving = "NO"
buildForAnalyzing = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "MyLibraryTests"
BuildableName = "MyLibraryTests"
BlueprintName = "MyLibraryTests"
ReferencedContainer = "container:">
</BuildableReference>
</BuildActionEntry>
</BuildActionEntries>
</BuildAction>
<TestAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
shouldUseLaunchSchemeArgsEnv = "YES"
shouldAutocreateTestPlan = "YES">
<Testables>
<TestableReference
skipped = "NO">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "MyLibraryTests"
BuildableName = "MyLibraryTests"
BlueprintName = "MyLibraryTests"
ReferencedContainer = "container:">
</BuildableReference>
</TestableReference>
<TestableReference
skipped = "NO">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "HMFoundationTests"
BuildableName = "HMFoundationTests"
BlueprintName = "HMFoundationTests"
ReferencedContainer = "container:">
</BuildableReference>
</TestableReference>
</Testables>
</TestAction>
<LaunchAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
launchStyle = "0"
useCustomWorkingDirectory = "NO"
ignoresPersistentStateOnLaunch = "NO"
debugDocumentVersioning = "YES"
debugServiceExtension = "internal"
allowLocationSimulation = "YES">
</LaunchAction>
<ProfileAction
buildConfiguration = "Release"
shouldUseLaunchSchemeArgsEnv = "YES"
savedToolIdentifier = ""
useCustomWorkingDirectory = "NO"
debugDocumentVersioning = "YES">
<MacroExpansion>
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "HMFoundation"
BuildableName = "HMFoundation"
BlueprintName = "HMFoundation"
ReferencedContainer = "container:">
</BuildableReference>
</MacroExpansion>
</ProfileAction>
<AnalyzeAction
buildConfiguration = "Debug">
</AnalyzeAction>
<ArchiveAction
buildConfiguration = "Release"
revealArchiveInOrganizer = "YES">
</ArchiveAction>
</Scheme>
39 changes: 38 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,40 @@
# HMFoundation

A description of this package.
Swift package HMFoundation provides some useful functions and classes for Swift programming.

## Prerequisites

- **[Swift](https://swift.org)**: **3.0+**.

## Installation

The [Swift Package Manager](https://swift.org/package-manager/) is a tool for automating the distribution of Swift code and is integrated into the `swift` compiler.
Once you have your Swift package set up, you can append HMFoundation as a dependency easily.

First, append HMFoundation to the `dependencies` value of your `Package.swift` or the Package list in Xcode.

```swift
dependencies: [
.package(url: "https://github.com/HarutakaMatsumoto/HMFoundation.git", .upToNextMajor(from: "1.0.1"))
]
```

Then, append `HMFoundation` to the targets of your package.

```swift
targets: [
.target(
name: "YourTarget",
dependencies: ["HMFoundation"]),
]
```

That is all! Now you can build your project.

## Contribution

You are very welcome to:

- Create pull requests of any kind
- Let me know if you are using this library and find it useful
- Open issues with request for support because they will help you and many others
132 changes: 130 additions & 2 deletions Sources/HMFoundation/HMFoundation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,71 @@
import Foundation

public extension CustomStringConvertible {

/// Writes the textual representations of the item into the standard
/// output.
func printed() -> Self {
print(self)
return self
}
}

/// Writes a newline character into the standard output.
public func printIndex() {
print("\n")
}

/// Writes the line number into the standard output.
public func printLine(_ line: Int = #line) {
print(line)
}


/// Writes the textual representations of the given items into the standard
/// output.
///
/// You can pass zero or more items to the `print(_:separator:terminator:)`
/// function. The textual representation for each item is the same as that
/// obtained by calling `String(item)`. The following example prints a string,
/// a closed range of integers, and a group of floating-point values to
/// standard output:
///
/// print("One two three four five")
/// // Prints "One two three four five"
///
/// print(1...5)
/// // Prints "1...5"
///
/// print(1.0, 2.0, 3.0, 4.0, 5.0)
/// // Prints "1.0 2.0 3.0 4.0 5.0"
///
/// To print the items separated by something other than a space, pass a string
/// as `separator`.
///
/// print(1.0, 2.0, 3.0, 4.0, 5.0, separator: " ... ")
/// // Prints "1.0 ... 2.0 ... 3.0 ... 4.0 ... 5.0"
///
/// The output from each call to `print(_:separator:terminator:)` includes a
/// newline by default. To print the items without a trailing newline, pass an
/// empty string as `terminator`.
///
/// for n in 1...5 {
/// print(n, terminator: "")
/// }
/// // Prints "12345"
///
/// - Parameters:
/// - items: Zero or more items to print.
/// - separator: A string to print between each item. The default is a single
/// space (`" "`).
/// - terminator: The string to print after all items have been printed. The
/// default is a newline (`"\n"`).

/// Writes the textual representations of the given items into the standard
/// output as the following format.
/// The separator is a comma and a space.
/// The terminator is a newline.
/// - Parameters:
/// - items: Zero or more items to print.
public func HMPrint(_ items: Any...) {
var initial = true
for item in items {
Expand All @@ -41,28 +91,71 @@ public func HMPrint(_ items: Any...) {
print("")
}

/// Side is a type that represents the four sides of a rectangle.
/// - right: The right side of a rectangle.
/// - up: The up side of a rectangle.
/// - left: The left side of a rectangle.
/// - low: The low side of a rectangle.
public enum Side {
case right,up,left,low
}

/// Place is a type that represents the ten places of a rectangle.
/// - previous: The previous place of a rectangle.
/// - center: The center of a rectangle.
/// - upperRight: The upper right of a rectangle.
/// - up: The up of a rectangle.
/// - upperLeft: The upper left of a rectangle.
/// - Left: The left of a rectangle.
/// - lowerLeft: The lower left of a rectangle.
/// - low: The low of a rectangle.
/// - lowerRight: The lower right of a rectangle.
/// - inner: The inner of a rectangle.
/// - next: The next place of a rectangle.
///
/// The following figure shows the places in a rectangle.
/// ```
/// upperLeft up upperRight
/// Left center
/// lowerLeft low lowerRight
/// ```
///
/// The following figure shows the places of a rectangle.
/// ```
/// ┌─────────┐
/// previous │ inner │ next
/// └─────────┘
/// ```
public enum Place: Int {
case previous,center,upperRight,up,upperLeft,Left,lowerLeft,low,lowerRight,inner,next
}

public extension ArraySlice {

/// Copy the array slice to an array.
/// - Returns: An array that copied the array slice.
/// - Complexity: O(*n*), where *n* is the length of the array slice.
/// - Note: The array slice is independent of the array.
func independentized() -> Array<Element> {
return self.map { $0 }
}
}

public extension Character {

/// Returns a boolean value indicating whether the character is a hiragana.
/// - Returns: `true` if the character is a hiragana; otherwise, `false`.
/// - SeeAlso: [Unicode.org](https://unicode.org/charts/PDF/U3040.pdf)
var isHiragana: Bool {
return 0x3040...0x309f ~= self.unicodeScalars.first!.value//なんか汚い
}
}

public extension StringProtocol {

/// Returns a string with hiragana converted to katakana.
/// - Returns: A string with hiragana converted to katakana.
/// - SeeAlso: [Unicode.org](https://unicode.org/charts/PDF/U30A0.pdf)
var katakanalized: String {
let mapped = self.map { (character: Character) -> String in
if character.isHiragana {
Expand All @@ -77,21 +170,37 @@ public extension StringProtocol {
}
}


/// \*\* is a power operator.
infix operator **: PowerPrecedence//publicいらない

/// **PowerPrecedence** is a precedence group that represents the power operator.
precedencegroup PowerPrecedence {//publicいらない
higherThan: MultiplicationPrecedence
lowerThan: BitwiseShiftPrecedence
associativity: right
}

/// \*\*= is a power assignment operator.
infix operator **=: AssignmentPrecedence

/// **Powerable** is a protocol that represents the power operation.
/// - Note: The power operation is defined in [Wikipedia](https://en.wikipedia.org/wiki/Exponentiation)
public protocol Powerable {

/// Returns the result of the power operation.
/// - Parameters:
/// - radix: The base number.
/// - power: The exponent.
/// - Returns: The result of the power operation.
static func ** (_: Self, _: Self) -> Self
}

public extension Powerable {

/// Assigns the result of the power operation to the left operand.
/// - Parameters:
/// - lhs: The left operand.
/// - rhs: The right operand.
static func **= (lhs: inout Self, rhs: Self) {
lhs = lhs ** rhs
}
Expand Down Expand Up @@ -129,6 +238,10 @@ extension Float80: Powerable {
#endif

public extension Array {

/// Writes the textual representations of the array elements into the standard output.
/// The separator is a newline.
/// The terminator is a newline.
func printEach() {
for one in self {
print(one)
Expand All @@ -140,6 +253,15 @@ public extension Array {

//MARK: system
#if os(macOS)

/// Executes the given shell command.
/// - Parameters:
/// - body: The shell command.
/// - Throws: An error if the shell command is invalid.
/// - Note: The shell command is written to a temporary file.
/// The temporary file is executed by the shell.
/// The standard output and the standard error are written to the standard output.
/// - SeeAlso: [Wikipedia](https://en.wikipedia.org/wiki/Shell_script)
public func system(_ body: String) throws {
let process = Process()
let transProcess = Process()
Expand Down Expand Up @@ -185,6 +307,12 @@ public func system(_ body: String) throws {
#endif

public extension Bundle {

/// Returns the value associated with the specified key in the localized or normal information dictionary.
/// - Parameters:
/// - key: The key in the information dictionary.
/// - Returns: The value associated with the key in the localized or normal information dictionary.
/// - Note: The localized information dictionary is preferred.
func getValueFromLocalizedOrNormalInfoDictionary<T>(ofKey key: String, as type: T.Type) -> T? {
if let it = localizedInfoDictionary {
if let value = it[key] {
Expand Down
Loading
Loading