File tree Expand file tree Collapse file tree 2 files changed +34
-1
lines changed
main/scala/org/apache/spark/util
test/scala/org/apache/spark/util Expand file tree Collapse file tree 2 files changed +34
-1
lines changed Original file line number Diff line number Diff line change @@ -2063,7 +2063,7 @@ private[spark] object Utils extends Logging {
20632063 * @param str
20642064 * @return the trimmed value of str
20652065 */
2066- def trimExceptCRLF (str : String ): String = {
2066+ private [util] def trimExceptCRLF (str : String ): String = {
20672067 val firstPos = str.indexWhere(nonSpaceOrNaturalLineDelimiter)
20682068 val lastPos = str.lastIndexWhere(nonSpaceOrNaturalLineDelimiter)
20692069 if (firstPos >= 0 && lastPos >= 0 ) {
Original file line number Diff line number Diff line change @@ -1205,6 +1205,39 @@ class UtilsSuite extends SparkFunSuite with ResetSystemProperties with Logging {
12051205 assert(Utils .stringHalfWidth(" \u0967\u0968\u0969 " ) == 3 )
12061206 // scalastyle:on nonascii
12071207 }
1208+
1209+ test(" trimExceptCRLF standalone" ) {
1210+ val crlfSet = Set (" \r " , " \n " )
1211+ val nonPrintableButCRLF =
1212+ (0 to 32 ).map(_.toChar.toString).toSet -- crlfSet
1213+
1214+ // identity for CRLF
1215+ crlfSet
1216+ .foreach(s => Utils .trimExceptCRLF(s) === s)
1217+
1218+ // empty for other non-printables
1219+ nonPrintableButCRLF
1220+ .foreach(s => assert(Utils .trimExceptCRLF(s) === " " ))
1221+
1222+ // identity for a printable string
1223+ assert(Utils .trimExceptCRLF(" a" ) === " a" )
1224+
1225+ // identity for strings with CRLF
1226+ crlfSet
1227+ .foreach { s =>
1228+ assert(Utils .trimExceptCRLF(s " ${s}a " ) === s " ${s}a " )
1229+ assert(Utils .trimExceptCRLF(s " a ${s}" ) === s " a ${s}" )
1230+ assert(Utils .trimExceptCRLF(s " b ${s}b " ) === s " b ${s}b " )
1231+ }
1232+
1233+ // trim nonPrintableButCRLF except when inside a string
1234+ nonPrintableButCRLF
1235+ .foreach { s =>
1236+ assert(Utils .trimExceptCRLF(s " ${s}a " ) === " a" )
1237+ assert(Utils .trimExceptCRLF(s " a ${s}" ) === " a" )
1238+ assert(Utils .trimExceptCRLF(s " b ${s}b " ) === s " b ${s}b " )
1239+ }
1240+ }
12081241}
12091242
12101243private class SimpleExtension
You can’t perform that action at this time.
0 commit comments