Skip to content

Commit

Permalink
Updated StringExtensions
Browse files Browse the repository at this point in the history
  • Loading branch information
gkjpettet committed Jun 2, 2024
1 parent 037e7a2 commit 9ffc968
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 33 deletions.
5 changes: 3 additions & 2 deletions src/XUI.xojo_project
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Type=Desktop
RBProjectVersion=2023.011
RBProjectVersion=2024.02
MinIDEVersion=20210300
OrigIDEVersion=20210301
Class=App;App.xojo_code;&h000000007F52B7FF;&h0000000000000000;false
Expand All @@ -17,7 +17,6 @@ Folder=macOS Specific;XUI/Required/macOS Specific;&h000000007D10DFFF;&h000000004
Module=Maths;XUI/Required/Maths.xojo_code;&h000000004986D7FF;&h0000000049AEC7FF;false
Module=XUIColorExtensions;XUI/Required/Extensions/XUIColorExtensions.xojo_code;&h000000003FCFBFFF;&h000000004A274FFF;false
Folder=NotificationCenter;XUI/Required/NotificationCenter;&h0000000056B12FFF;&h0000000049AEC7FF;false
Module=StringExtensions;XUI/Required/StringExtensions.xojo_code;&h000000000F5A47FF;&h0000000049AEC7FF;false
Folder=Text UI Classes;XUI/Required/Text UI Classes;&h000000001243C7FF;&h0000000049AEC7FF;false
Module=XUI;XUI/Required/XUI.xojo_code;&h0000000075C34FFF;&h0000000049AEC7FF;false
Class=XUIApp;XUI/Required/XUIApp.xojo_code;&h000000003FD27FFF;&h0000000049AEC7FF;false
Expand Down Expand Up @@ -289,6 +288,7 @@ Class=XUITextSelection;XUI/Required/Text UI Classes/XUITextSelection.xojo_code;&
Class=XUITextLine;XUI/Required/Text UI Classes/XUITextLine.xojo_code;&h000000007F00C7FF;&h000000001243C7FF;false
Module=AppKit;XUI/Required/macOS Specific/AppKit.xojo_code;&h000000003C9D27FF;&h000000007D10DFFF;false
Class=NSScrollViewCanvas;XUI/Required/macOS Specific/NSScrollViewCanvas.xojo_code;&h0000000005FF5FFF;&h000000007D10DFFF;false
Module=StringExtensions;XUI/Required/StringExtensions.xojo_code;&h000000000F5A47FF;&h0000000049AEC7FF;false
AppMenuBar=BasicMenuBar
MajorVersion=2
MinorVersion=3
Expand Down Expand Up @@ -323,6 +323,7 @@ HiDPI=True
DarkMode=True
CopyRedistNextToWindowsEXE=False
IncludePDB=False
WinUIFramework=False
IsWebProject=False
LinuxBuildArchitecture=1
MacBuildArchitecture=4
Expand Down
129 changes: 99 additions & 30 deletions src/XUI/Required/StringExtensions.xojo_code
Original file line number Diff line number Diff line change
Expand Up @@ -54,27 +54,21 @@ Protected Module StringExtensions
/// It's at least 4x faster to use `Text` to split into characters and then iterate over that array
/// than to use the native `String.Characters()` method that returns an `Iterable`.

// Get the characters as a Text array.
Var tmp() As Text = s.ToText.Split

// Now convert each character to a string and add it to our return array.
Var chars() As String
chars.ResizeTo(tmp.LastIndex)
For i As Integer = 0 To tmp.LastIndex
chars(i) = tmp(i)
Next i

Return chars

End Function
#tag EndMethod

#tag Method, Flags = &h0, Description = 52657475726E7320746865206E756D626572206F66206368617261637465727320696E207468652070617373656420737472696E672028696E636C7564696E67206D756C7469627974652063686172616374657273292E
Function CharacterCount(Extends s As String) As Integer
/// Returns the number of characters in the passed string (including multibyte characters).

Var t As Text = s.ToText
Return t.Length
#If XojoVersion >= 2024.02 Then
Return s.Characters
#Else
// Get the characters as a Text array.
Var tmp() As Text = s.ToText.Split

// Now convert each character to a string and add it to our return array.
Var chars() As String
chars.ResizeTo(tmp.LastIndex)
For i As Integer = 0 To tmp.LastIndex
chars(i) = tmp(i)
Next i

Return chars
#EndIf

End Function
#tag EndMethod
Expand Down Expand Up @@ -777,8 +771,17 @@ Protected Module StringExtensions
Function LeftCharacters(Extends s As String, count As Integer) As String
/// Returns `count` left-most characters from `s`.

Var t As Text = s.ToText
Return t.Left(count)
#If XojoVersion >= 2024.02 Then
Var chars() As String = s.Characters
Var tmp() As String
For i As Integer = 0 To count - 1
tmp.Add(chars(i))
Next i
Return String.FromArray(tmp, "")
#Else
Var t As Text = s.ToText
Return t.Left(count)
#EndIf
End Function
#tag EndMethod

Expand All @@ -800,19 +803,74 @@ Protected Module StringExtensions

#tag Method, Flags = &h0, Description = 52657475726E7320616C6C206F662074686520636861726163746572732066726F6D206073746172746020746F2074686520656E64206F66206073602E2054686520737461727420706F736974696F6E2069732061207A65726F2D62617365642E
Function MiddleCharacters(Extends s As String, start As Integer) As String
/// Returns all of the characters from `start` to the end of `s`. The start position is a zero-based.
/// Returns all of the characters from `start` to the end of `s`.
/// The start position is a zero-based.

Var t As Text = s.ToText
Return t.Mid(start)
#If XojoVersion >= 2024.02 Then
Var chars() As String = s.Characters
Var tmp() As String
Var limit As Integer = s.CharacterCount - 1
For i As Integer = start To limit
tmp.Add(chars(i))
Next i
Return String.FromArray(tmp, "")
#Else
Var t As Text = s.ToText
Return t.Mid(start)
#EndIf
End Function
#tag EndMethod

#tag Method, Flags = &h0, Description = 52657475726E732060636F756E746020636861726163746572732066726F6D206073602E2048616E646C6573206D756C7469627974652063686172616374657273206C696B6520656D6F6A692E
Function MiddleCharacters(Extends s As String, start As Integer, count As Integer) As String
/// Returns `count` characters from `s`. Handles multibyte characters like emoji.

Var t As Text = s.ToText
Return t.Mid(start, count)
#If XojoVersion >= 2024.02 Then
Var chars() As String = s.Characters
Var tmp() As String
For i As Integer = start To start + count - 1
tmp.Add(chars(i))
Next i
Return String.FromArray(tmp, "")
#Else
Var t As Text = s.ToText
Return t.Mid(start, count)
#EndIf

End Function
#tag EndMethod

#tag Method, Flags = &h0, Description = 50616473207468652070617373656420737472696E6720746F20746865206C65667420776974682060636861726020746865207370656369666965642060636F756E7460206E756D626572206F662074696D65732E
Function PadLeft(Extends s As String, count As Integer, char As String) As String
/// Pads the passed string to the left with `char` the specified `count` number of times.

// Quick escape?
If count <= 0 Then Return s

Var padding() As String
For i As Integer = 1 To count
padding.Add(char)
Next i

Return String.FromArray(padding, "") + s

End Function
#tag EndMethod

#tag Method, Flags = &h0, Description = 50616473207468652070617373656420737472696E6720746F20746865206C65667420776974682060636861726020746865207370656369666965642060636F756E7460206E756D626572206F662074696D65732E
Function PadLeft(s As String, count As Integer, char As String) As String
/// Pads the passed string to the left with `char` the specified `count` number of times.

// Quick escape?
If count <= 0 Then Return s

Var padding() As String
For i As Integer = 1 To count
padding.Add(char)
Next i

Return String.FromArray(padding, "") + s

End Function
#tag EndMethod

Expand All @@ -833,8 +891,19 @@ Protected Module StringExtensions
Function RightCharacters(Extends s As String, count As Integer) As String
/// Returns `count` right-most characters from `s`.

Var t As Text = s.ToText
Return t.Right(count)
#If XojoVersion >= 2024.02 Then
Var chars() As String = s.Characters
Var tmp() As String
Var limit As Integer = chars.LastIndex - count + 1
For i As Integer = chars.LastIndex DownTo limit
tmp.AddAt(0, chars(i))
Next i
Return String.FromArray(tmp, "")
#Else
Var t As Text = s.ToText
Return t.Right(count)
#EndIf

End Function
#tag EndMethod

Expand Down
2 changes: 1 addition & 1 deletion src/XUI/XUI Utilities/MarkdownKit.xojo_code
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ Protected Module MarkdownKit
#Pragma StackOverflowChecking False
#Pragma DisableBoundsChecking

Var tmp() As Text = s.ToText.Split
Var tmp() As String = s.Characters
Var chars() As MKCharacter
Var tmpLastIndex As Integer = tmp.LastIndex

Expand Down

0 comments on commit 9ffc968

Please sign in to comment.