Skip to content

Commit

Permalink
extend StringBuilder implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
bastie committed Sep 10, 2024
1 parent 8644dd0 commit 64dce24
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
6 changes: 6 additions & 0 deletions Sources/JavApi/lang/String+Java.swift
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,12 @@ extension String {
public static func valueOf (_ char : Character) -> String {
return String ("\(char)")
}

/// Returns self
/// - Returns self
public func toString () -> String {
return self
}

}

Expand Down
22 changes: 21 additions & 1 deletion Sources/JavApi/lang/StringBuilder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,24 @@
* SPDX-FileCopyrightText: 2023 - Sebastian Ritter <[email protected]>
* SPDX-License-Identifier: MIT
*/

// final class => public
public class StringBuilder {

var content : String = ""
var content : String = "" // TODO: implements direct a char array

/// Default constructor
public init (){}

public init (_ newContent : String) {
self.content = newContent
}

// TODO: Test it
public init (_ newContent : any CharSequence) {
self.content = "\(newContent)"
}

/// Append a String type
/// - Parameters String to append
/// - Returns self (fluent interface pattern)
Expand All @@ -27,6 +38,15 @@ public class StringBuilder {
return self
}

public func deleteAt (_ offset : Int) throws -> StringBuilder {
guard offset > -1, offset < self.count else {
throw java.lang.Throwable.IndexOutOufBoundsException(offset, "the index is negative or greater than or equal to count of String")
}
var asCharArray = Array(self.content)
asCharArray.removeFirst(offset)
return StringBuilder(String(asCharArray))
}

public func length () -> Int {
return content.lenght()
}
Expand Down

0 comments on commit 64dce24

Please sign in to comment.