2328 'asd''f;d''dd;s' asCollectionOfSubstringsSeparatedBy:$; exceptIn:$' |
2329 'asd''f;d''dd;s' asCollectionOfSubstringsSeparatedBy:$; exceptIn:$' |
2329 " |
2330 " |
2330 "/ 'asd "hello bla" foo "bla bla" bar' asCollectionOfSubstringsSeparatedBy:$ exceptIn:$" |
2331 "/ 'asd "hello bla" foo "bla bla" bar' asCollectionOfSubstringsSeparatedBy:$ exceptIn:$" |
2331 ! |
2332 ! |
2332 |
2333 |
2333 asCollectionOfSubstringsSeparatedBy: aFieldSeparator textSeparator:aTextSeparatorOrNil |
2334 asCollectionOfSubstringsSeparatedBy:aFieldSeparator textSeparator:aTextSeparatorOrNil |
2334 "return a collection containing the words (separated by aFieldSeparator) |
2335 "return a collection containing the words (separated by aFieldSeparator) of the receiver. |
2335 of the receiver. This allows breaking up csv strings." |
2336 Individual words might be enclosed in aTextSeparator characters, in case they contain blanks or fieldSeparators. |
2336 |
2337 Typically used for CSV line parsing, with a $; as aFieldSeparator and $'' (dquote) as textSeparator." |
2337 ^ self asCollectionOfSubstringsSeparatedByAll: aFieldSeparator |
2338 |
2338 |
2339 |items myClass scanningWord |
2339 "/ '"First", "Second,SecondAdd", "Third"' asCollectionOfSubstringsSeparatedBy:',' textSeparator: '"' |
2340 inStream element| |
2340 "/ 'First,Second,Third' asCollectionOfSubstringsSeparatedBy:',' textSeparator: nil |
2341 |
|
2342 aTextSeparatorOrNil isNil ifTrue:[ |
|
2343 ^ self asCollectionOfSubstringsSeparatedByAll: aFieldSeparator |
|
2344 ]. |
|
2345 |
|
2346 items := OrderedCollection new. |
|
2347 myClass := self species. |
|
2348 |
|
2349 inStream := ReadStream on:self. |
|
2350 [ |
|
2351 inStream skipSeparators. |
|
2352 inStream atEnd |
|
2353 ] whileFalse:[ |
|
2354 inStream peek == aTextSeparatorOrNil ifTrue:[ |
|
2355 inStream next. |
|
2356 element := ''. |
|
2357 scanningWord := true. |
|
2358 [ inStream atEnd not and:[scanningWord] ] whileTrue:[ |
|
2359 element := element , (inStream upTo:aTextSeparatorOrNil). |
|
2360 (inStream peek == aTextSeparatorOrNil) ifTrue:[ |
|
2361 element := element , aTextSeparatorOrNil. |
|
2362 inStream next. |
|
2363 ] ifFalse:[ |
|
2364 scanningWord := false. |
|
2365 ]. |
|
2366 ]. |
|
2367 inStream upToAllExcluding:aFieldSeparator. |
|
2368 ] ifFalse:[ |
|
2369 element := inStream upToAllExcluding:aFieldSeparator |
|
2370 ]. |
|
2371 items add:element. |
|
2372 ]. |
|
2373 |
|
2374 ^ items |
|
2375 |
|
2376 " |
|
2377 self assert:(('#First#, #Second,SecondAdd#, #Third#' asCollectionOfSubstringsSeparatedBy:',' textSeparator: $#) |
|
2378 sameContentsAs:#('First' 'Second,SecondAdd' 'Third')) |
|
2379 self assert:(('#Fir##st#, #Second,SecondAdd#, #Third#' asCollectionOfSubstringsSeparatedBy:',' textSeparator: $#) |
|
2380 sameContentsAs:#('Fir#st' 'Second,SecondAdd' 'Third')) |
|
2381 self assert:(('#Fir##st#, Second,SecondAdd, #Third#' asCollectionOfSubstringsSeparatedBy:',' textSeparator: $#) |
|
2382 sameContentsAs:#('Fir#st' 'Second' 'SecondAdd' 'Third')) |
|
2383 " |
|
2384 " |
|
2385 'First,Second,Third' asCollectionOfSubstringsSeparatedBy:',' textSeparator: nil |
|
2386 " |
|
2387 |
|
2388 "Modified: / 07-04-2011 / 13:23:19 / cg" |
2341 ! |
2389 ! |
2342 |
2390 |
2343 asCollectionOfSubstringsSeparatedByAll:aSeparatorString |
2391 asCollectionOfSubstringsSeparatedByAll:aSeparatorString |
2344 "return a collection containing the lines (separated by aSeparatorString) |
2392 "return a collection containing the lines (separated by aSeparatorString) |
2345 of the receiver. If aSeparatorString occurs multiple times in a row, |
2393 of the receiver. If aSeparatorString occurs multiple times in a row, |