From d1e59ec070d523b1f2d6b14b106ee7028c82f2aa Mon Sep 17 00:00:00 2001 From: Li Haoyi Date: Tue, 7 Dec 2021 12:53:25 +0800 Subject: [PATCH 1/2] . --- os/src-jvm/ResourcePath.scala | 2 +- os/src/Path.scala | 21 ++++++++++++++------- os/test/src/PathTests.scala | 7 +++++++ 3 files changed, 22 insertions(+), 8 deletions(-) diff --git a/os/src-jvm/ResourcePath.scala b/os/src-jvm/ResourcePath.scala index 6e7160e2..cb97dde2 100644 --- a/os/src-jvm/ResourcePath.scala +++ b/os/src-jvm/ResourcePath.scala @@ -23,7 +23,7 @@ class ResourcePath private[os](val resRoot: ResourceRoot, segments0: Array[Strin def toSource = new Source.WritableSource(getInputStream) val segments: IndexedSeq[String] = segments0 type ThisType = ResourcePath - def last = segments0.last + def lastOpt = segments0.lastOption override def toString = resRoot.errorName + "/" + segments0.mkString("/") protected[this] def make(p: Seq[String], ups: Int) = { if (ups > 0){ diff --git a/os/src/Path.scala b/os/src/Path.scala index 10717e0b..6e573d97 100644 --- a/os/src/Path.scala +++ b/os/src/Path.scala @@ -182,9 +182,14 @@ trait BasePathImpl extends BasePath{ def /(chunk: PathChunk): ThisType def ext = { - val li = last.lastIndexOf('.') - if (li == -1) "" - else last.slice(li+1, last.length) + lastOpt match{ + case None => "" + case Some(lastSegment) => + val li = lastSegment.lastIndexOf('.') + if (li == -1) "" + else last.slice(li+1, last.length) + } + } override def baseName: String = { @@ -193,7 +198,9 @@ trait BasePathImpl extends BasePath{ else last.slice(0, li) } - def last: String + def last: String = lastOpt.getOrElse("empty path has no last segment") + + def lastOpt: Option[String] } object PathError{ @@ -239,7 +246,7 @@ object FilePath { */ class RelPath private[os](segments0: Array[String], val ups: Int) extends FilePath with BasePathImpl with SegmentedPath { - def last = segments.last + def lastOpt = segments.lastOption val segments: IndexedSeq[String] = segments0 type ThisType = RelPath require(ups >= 0) @@ -305,7 +312,7 @@ object RelPath { */ class SubPath private[os](val segments0: Array[String]) extends FilePath with BasePathImpl with SegmentedPath { - def last = segments.last + def lastOpt = segments.lastOption val segments: IndexedSeq[String] = segments0 type ThisType = SubPath protected[this] def make(p: Seq[String], ups: Int) = { @@ -433,7 +440,7 @@ class Path private[os](val wrapped: java.nio.file.Path) def segmentCount = wrapped.getNameCount type ThisType = Path - def last = wrapped.getFileName.toString + def lastOpt = Option(wrapped.getFileName).map(_.toString) def /(chunk: PathChunk): ThisType = { if (chunk.ups > wrapped.getNameCount) throw PathError.AbsolutePathOutsideRoot diff --git a/os/test/src/PathTests.scala b/os/test/src/PathTests.scala index 37c2e363..5908edb5 100644 --- a/os/test/src/PathTests.scala +++ b/os/test/src/PathTests.scala @@ -47,6 +47,13 @@ object PathTests extends TestSuite{ assert((base / "baseOnly").ext == "") assert((base / "baseOnly.").ext == "") } + + test("emptyExt"){ + os.root.ext ==> "" + os.rel.ext ==> "" + os.sub.ext ==> "" + os.up.ext ==> "" + } } test("RelPath"){ From ca3aa0c15df9a0928c4e622ecee91f4462188f5c Mon Sep 17 00:00:00 2001 From: Li Haoyi Date: Wed, 8 Dec 2021 18:36:30 +0800 Subject: [PATCH 2/2] . --- os/src/Path.scala | 5 ++++- os/test/src/PathTests.scala | 11 +++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/os/src/Path.scala b/os/src/Path.scala index 6e573d97..b34a3da6 100644 --- a/os/src/Path.scala +++ b/os/src/Path.scala @@ -198,7 +198,7 @@ trait BasePathImpl extends BasePath{ else last.slice(0, li) } - def last: String = lastOpt.getOrElse("empty path has no last segment") + def last: String = lastOpt.getOrElse(throw PathError.LastOnEmptyPath()) def lastOpt: Option[String] } @@ -215,6 +215,9 @@ object PathError{ case class NoRelativePath(src: RelPath, base: RelPath) extends IAE(s"Can't relativize relative paths $src from $base") + + case class LastOnEmptyPath() + extends IAE("empty path has no last segment") } /** diff --git a/os/test/src/PathTests.scala b/os/test/src/PathTests.scala index 5908edb5..d96a664d 100644 --- a/os/test/src/PathTests.scala +++ b/os/test/src/PathTests.scala @@ -54,6 +54,17 @@ object PathTests extends TestSuite{ os.sub.ext ==> "" os.up.ext ==> "" } + + test("emptyLast"){ + intercept[PathError.LastOnEmptyPath](os.root.last).getMessage ==> + "empty path has no last segment" + intercept[PathError.LastOnEmptyPath](os.rel.last).getMessage ==> + "empty path has no last segment" + intercept[PathError.LastOnEmptyPath](os.sub.last).getMessage ==> + "empty path has no last segment" + intercept[PathError.LastOnEmptyPath](os.up.last).getMessage ==> + "empty path has no last segment" + } } test("RelPath"){