extensions.st
branchjv
changeset 1108 1bdcfc9842d0
parent 1102 60e2741ec859
child 1120 e0ec9591b6e8
--- a/extensions.st	Thu Jul 09 23:31:03 2020 +0100
+++ b/extensions.st	Mon Aug 10 14:37:20 2020 +0100
@@ -85,6 +85,117 @@
     "Modified: / 13-10-2010 / 17:28:10 / Jan Vrany <jan.vrany@fit.cvut.cz>"
 ! !
 
+!CharacterArray methodsFor:'special string converting'!
+
+spacesToTabs
+    "For each line, convert each leading sequence of `numSpaces` spaces
+     into a tab.
+     Used to convert sources indentation from Smalltalk/X to Squeak-world.
+    "     
+
+    ^ self spacesToTabs: 4
+
+    "Created: / 10-08-2020 / 12:03:56 / Jan Vrany <jan.vrany@labware.com>"
+! !
+
+!CharacterArray methodsFor:'special string converting'!
+
+spacesToTabs: numSpaces
+    "For each line, convert each leading sequence of `numSpaces` spaces
+     into a tab.
+     Used to convert sources indentation from spaces to tabs.
+    "     
+
+    | sz pos out crlf |
+
+    crlf := String with: Character return with: Character linefeed.
+    sz := self size.
+    pos := 1.
+    out := CharacterWriteStream new: self size.
+    [ pos <= sz ] whileTrue: [
+        | numSpacesFound end |
+
+        "/ Search for leading consecutive `numSpaces` spaces.
+        numSpacesFound := 0.
+        [ (pos + numSpacesFound) <= sz and:[(self at: pos + numSpacesFound) == Character space ] ] whileTrue: [
+            numSpacesFound := numSpacesFound + 1.            
+        ].
+        "/ Replace spaces with tabs
+        (numSpacesFound // numSpaces) timesRepeat: [
+            out nextPut: Character tab.
+            pos := pos + numSpaces.
+        ].
+
+        "/ And finally, put rest of the line (if any)
+        end := self indexOfAny: crlf startingAt: pos ifAbsent: sz.
+        out nextPutAll: self startingAt: pos to: end.
+
+        pos := end + 1.
+    ].
+    ^ out contents.
+
+    "
+    'hello' spacesToTabs: 4
+
+    '    hello world' spacesToTabs: 4
+    "
+
+    "Created: / 06-08-2020 / 11:05:47 / Jan Vrany <jan.vrany@labware.com>"
+    "Modified: / 10-08-2020 / 12:19:26 / Jan Vrany <jan.vrany@labware.com>"
+! !
+
+!CharacterArray methodsFor:'special string converting'!
+
+tabsToSpaces
+    "For each line, convert each leading tab into 4 spaces. 
+     Used to convert sources from Squeak-world indentation to Smalltalk/X 
+     indentation.
+    "
+    ^ self tabsToSpaces: 4
+
+    "Created: / 10-08-2020 / 12:00:26 / Jan Vrany <jan.vrany@labware.com>"
+! !
+
+!CharacterArray methodsFor:'special string converting'!
+
+tabsToSpaces: numSpaces
+    "For each line, convert each leading tab into `numSpaces` spaces. 
+     Used to convert sources indentation from tabs to spaces.
+    "
+
+    | sz pos out crlf |
+
+    crlf := String with: Character return with: Character linefeed.
+    sz := self size.
+    pos := 1.
+    out := CharacterWriteStream new: self size.
+    [ pos <= sz ] whileTrue: [
+        | end |
+
+        "/ Search and replace leading tabs with `numSpaces` spaces
+        [ pos <= sz and:[ (self at: pos) == Character tab ] ] whileTrue: [
+            out next: numSpaces put: Character space.
+            pos := pos + 1.
+        ].
+
+        "/ And finally, put rest of the line (if any)
+        end := self indexOfAny: crlf startingAt: pos ifAbsent: sz.
+        out nextPutAll: self startingAt: pos to: end.
+
+        pos := end + 1.
+    ].
+    ^ out contents.
+
+    "
+    'hello' tabsToSpaces: 4
+
+    (Character tab asString , Character tab asString , 'hello world') tabsToSpaces: 4
+    "
+
+    "Created: / 06-08-2020 / 11:04:49 / Jan Vrany <jan.vrany@labware.com>"
+    "Modified: / 10-08-2020 / 12:18:40 / Jan Vrany <jan.vrany@labware.com>"
+! !
+
 !Class methodsFor:'*monticello'!
 
 asClassDefinition