[Pharo-project] Issue 3347 in pharo: simplified and unified String's line-ending changing methods

pharo at googlecode.com pharo at googlecode.com
Sat Nov 27 18:18:13 CET 2010


Status: FixedWaitingToBePharoed
Owner: stephane.ducasse
Labels: Milestone-1.3 Type-Squeak

New issue 3347 by stephane.ducasse: simplified and unified String's  
line-ending changing methods
http://code.google.com/p/pharo/issues/detail?id=3347

Levente Uzonyi uploaded a new version of Collections to project The Trunk:
http://source.squeak.org/trunk/Collections-ul.409.mcz

==================== Summary ====================

Name: Collections-ul.409
Author: ul
Time: 22 November 2010, 1:35:14.026 pm
UUID: 849eb41a-5717-9e44-ab23-eae36fb603bb
Ancestors: Collections-ul.408

- introduced String >> #withLineEndings:
- simplified and unified String's line-ending changing methods:  
#withInternetLineEndings, #withSqueakLineEndings  and #withUnixLineEndings

=============== Diff against Collections-ul.408 ===============

Item was changed:
  ----- Method: String>>withInternetLineEndings (in category 'internet')  
-----
  withInternetLineEndings
        "change line endings from CR's and LF's to CRLF's.  This is probably  
in prepration for sending a string over the Internet"

+       ^self withLineEndings: String crlf!
-       ^self class
-               new: self size * 16 // 15 "provisions for CR-LF pairs"
-               streamContents: [ :stream |
-                       self lineIndicesDo:  
[:start :endWithoutDelimiters :end |
-                               stream next: 1 + endWithoutDelimiters -  
start putAll: self startingAt: start.
-                               endWithoutDelimiters = end ifFalse: [
-                                       stream crlf ] ] ]!

Item was added:
+ ----- Method: String>>withLineEndings: (in category 'internet') -----
+ withLineEndings: lineEndingString
+
+       | stream |
+       stream := nil.
+       self lineIndicesDo: [ :start :endWithoutDelimiters :end |
+               (stream isNil and: [ endWithoutDelimiters ~= end ]) ifTrue:  
[
+                       (self copyFrom: endWithoutDelimiters + 1 to: end) =  
lineEndingString ifFalse: [
+                               stream := WriteStream with: self copy.
+                               stream position: start - 1 ] ].
+               stream ifNotNil: [
+                       stream next: endWithoutDelimiters - start + 1  
putAll: self startingAt: start.
+                       endWithoutDelimiters = end ifFalse: [
+                               stream nextPutAll: lineEndingString ] ] ].
+       ^stream
+               ifNil: [ self ]
+               ifNotNil: [
+                       stream position = self size
+                               ifTrue: [ stream originalContents ]
+                               ifFalse: [ stream contents ] ]!

Item was changed:
  ----- Method: String>>withSqueakLineEndings (in category 'internet') -----
  withSqueakLineEndings
        "Assume the string is textual, and that CR, LF, and CRLF are all  
valid line endings.
        Replace each occurence with a single CR."
+       | cr lf indexLF indexCR |
-       | cr lf inPos outPos outString newOutPos indexLF indexCR |
        lf := Character linefeed.
        indexLF := self indexOf: lf startingAt: 1.
        indexLF = 0 ifTrue: [^self].

        cr := Character cr.
        indexCR := self indexOf: cr startingAt: 1.
        indexCR = 0 ifTrue: [^self copy replaceAll: lf with: cr].

+       ^self withLineEndings: String cr!
-       inPos := outPos := 1.
-       outString := String new: self size.
-
-       ["check if next CR (if any) is before next LF"
-       (indexCR > 0 and: [indexCR < indexLF])
-               ifTrue: [
-                       newOutPos := outPos + 1 + indexCR - inPos.
-                       outString replaceFrom: outPos to: newOutPos - 1  
with: self startingAt: inPos.
-                       outPos := newOutPos.
-                       1 + indexCR = indexLF
-                               ifTrue: ["Caught a CR-LF pair"
-                                       inPos := 1 + indexLF.
-                                       indexLF := self  indexOf: lf  
startingAt: inPos]
-                               ifFalse: [inPos := 1 + indexCR].
-                       indexCR := self indexOf: cr startingAt: inPos]
-               ifFalse: [
-                       newOutPos := outPos + 1 + indexLF - inPos.
-                       outString replaceFrom: outPos to: newOutPos - 2  
with: self startingAt: inPos.
-                       outString at: newOutPos - 1 put: cr.
-                       outPos := newOutPos.
-                       inPos := 1 + indexLF.
-                       indexLF := self indexOf: lf startingAt: inPos].
-       indexLF = 0]
-               whileFalse.
-
-       "no more LF line endings.  copy the rest"
-       newOutPos := outPos + (self size - inPos + 1).
-       outString replaceFrom: outPos to: newOutPos - 1 with: self  
startingAt: inPos.
-       ^outString copyFrom: 1 to: newOutPos - 1!

Item was changed:
  ----- Method: String>>withUnixLineEndings (in category 'internet') -----
  withUnixLineEndings
        "Assume the string is textual, and that CR, LF, and CRLF are all  
valid line endings.
        Replace each occurence with a single LF."
+       | cr lf indexLF indexCR |
-       | cr lf inPos outPos outString newOutPos indexLF indexCR |
        cr := Character cr.
        indexCR := self indexOf: cr startingAt: 1.
        indexCR = 0 ifTrue: [^self].

        lf := Character linefeed.
        indexLF := self indexOf: lf startingAt: 1.
        indexLF = 0 ifTrue: [^self copy replaceAll: cr with: lf].

+       ^self withLineEndings: String lf!
-       inPos := outPos := 1.
-       outString := String new: self size.
-
-       ["check if next CR is before next LF or if there are no more LF"
-       (indexLF = 0 or: [indexCR < indexLF])
-               ifTrue: [
-                       newOutPos := outPos + 1 + indexCR - inPos.
-                       outString replaceFrom: outPos to: newOutPos - 2  
with: self startingAt: inPos.
-                       outString at: newOutPos - 1 put: lf.
-                       outPos := newOutPos.
-                       1 + indexCR = indexLF
-                               ifTrue: ["Caught a CR-LF pair"
-                                       inPos := 1 + indexLF.
-                                       indexLF := self  indexOf: lf  
startingAt: inPos]
-                               ifFalse: [inPos := 1 + indexCR].
-                       indexCR := self indexOf: cr startingAt: inPos]
-               ifFalse: [
-                       newOutPos := outPos + 1 + indexLF - inPos.
-                       outString replaceFrom: outPos to: newOutPos - 1  
with: self startingAt: inPos.
-                       outPos := newOutPos.
-                       inPos := 1 + indexLF.
-                       indexLF := self indexOf: lf startingAt: inPos].
-       indexCR = 0]
-               whileFalse.
-
-       "no more CR line endings.  copy the rest"
-       newOutPos := outPos + (self size - inPos + 1).
-       outString replaceFrom: outPos to: newOutPos - 1 with: self  
startingAt: inPos.
-       ^outString copyFrom: 1 to: newOutPos - 1!





More information about the Pharo-project mailing list