summary refs log tree commit diff stats
path: root/compiler/options.nim
diff options
context:
space:
mode:
Diffstat (limited to 'compiler/options.nim')
-rw-r--r--compiler/options.nim29
1 files changed, 19 insertions, 10 deletions
diff --git a/compiler/options.nim b/compiler/options.nim
index e50535b8e..2b607a48b 100644
--- a/compiler/options.nim
+++ b/compiler/options.nim
@@ -209,17 +209,26 @@ proc getGeneratedPath: string =
   result = if nimcacheDir.len > 0: nimcacheDir else: gProjectPath.shortenDir /
                                                          genSubDir
 
+proc getPackageName(path: string): string =
+  var q = 1
+  var b = 0
+  if path[len(path)-1] in {dirsep, altsep}: q = 2
+  for i in countdown(len(path)-q, 0):
+    if path[i] in {dirsep, altsep}:
+      if b == 0: b = i
+      else:
+        let x = path.substr(i+1, b-1)
+        case x.normalize
+        of "lib", "src", "source", "package", "pckg", "library": b = i
+        else: return x
+
 proc withPackageName*(path: string): string =
-  var x = path
-  while true:
-    x = parentDir(x)
-    if x.len == 0: break
-    case x.normalize
-    of "lib", "src", "source", "package", "pckg", "library": discard
-    else:
-      let (path, file, ext) = path.splitFile
-      return (path / (x & '_' & file)) & ext
-  result = path
+  let x = path.getPackageName
+  if x.isNil:
+    result = path
+  else:
+    let (p, file, ext) = path.splitFile
+    result = (p / (x & '_' & file)) & ext
 
 proc toGeneratedFile*(path, ext: string): string = 
   ## converts "/home/a/mymodule.nim", "rod" to "/home/a/nimcache/mymodule.rod"