Решение на Нормализация на пътища от Недялко Дяков

Обратно към всички решения

Към профила на Недялко Дяков

Резултати

  • 10 точки от тестове
  • 0 бонус точки
  • 10 точки общо
  • 8 успешни тест(а)
  • 0 неуспешни тест(а)

Код

package main
import (
"regexp"
)
// Directory Separator
// "/" for Linux/OSX/FreeBSD/etc.
// "\\" should work for Windows, not tested.
const DS = "/"
// Prepends or/and appends missing Directory Separators
func normilizePath(path string) string {
if len(path) == 0 || path[0] != DS[0] {
path = string(DS[0]) + path
}
if path[len(path)-1] != DS[0] {
path = path + string(DS[0])
}
return path
}
// Parses the path
func parsePath(path string) string {
path = normilizePath(path)
// Remove single dots as they interrupt the next
// replacement.
regex, err := regexp.Compile(DS + `\.` + DS)
if err != nil {
return "Zero regex won't compile, FIX IT!"
}
for regex.MatchString(path) == true {
path = regex.ReplaceAllLiteralString(path, DS)
}
// Removing /somedir/../ pairs.
// It will parse multiline paths as well.
// p.s. some black magic involved.
regex, err = regexp.Compile(`(?sU)` + DS + `\.*?[^` + DS + `\.]+\.*[^` + DS + `]*` + DS + `\.{2}` + DS)
if err != nil {
return "First regex won't compile, FIX IT!"
}
for regex.MatchString(path) == true {
path = regex.ReplaceAllLiteralString(path, DS)
}
// Cleaning up remaining dots
regex, err = regexp.Compile(DS + `\.{1,2}` + DS)
if err != nil {
return "Second regex won't compile, FIX IT!"
}
for regex.MatchString(path) == true {
path = regex.ReplaceAllLiteralString(path, DS)
}
return path
}

Лог от изпълнението

PASS
ok  	_/tmp/d20131015-29033-18haw73	0.011s
PASS
ok  	_/tmp/d20131015-29033-18haw73	0.011s
PASS
ok  	_/tmp/d20131015-29033-18haw73	0.011s
PASS
ok  	_/tmp/d20131015-29033-18haw73	0.011s
PASS
ok  	_/tmp/d20131015-29033-18haw73	0.011s
PASS
ok  	_/tmp/d20131015-29033-18haw73	0.011s
PASS
ok  	_/tmp/d20131015-29033-18haw73	0.011s
PASS
ok  	_/tmp/d20131015-29033-18haw73	0.011s

История (3 версии и 2 коментара)

Недялко обнови решението на 13.10.2013 02:54 (преди над 4 години)

+package main
+
+import (
+ "regexp"
+)
+
+// Directory Separator
+// "/" for Linux/OSX/FreeBSD/etc.
+// "\\" should work for Windows, not tested.
+const DS = "/"
+
+// Prepends or/and appends missing Directory Separators
+func normilizePath(path string) string {
+ if len(path) == 0 || path[0] != DS[0] {
+ path = string(DS[0]) + path
+ }
+
+ if path[len(path)-1] != DS[0] {
+ path = path + string(DS[0])
+ }
+ return path
+}
+
+// Parses the path
+func parsePath(path string) string {
+ path = normilizePath(path)
+ // Removing /somedir/../ pairs.
+ // It will parse multiline paths as well.
+ // p.s. some black magic involved.
+ regex, err := regexp.Compile(`(?sU)` + DS + `\.*?[^` + DS + `\.]+\.*[^` + DS + `]*` + DS + `\.{2}` + DS)
+ if err != nil {
+ return "First regex won't compile, FIX IT!"
+ }
+ for regex.MatchString(path) == true {
+ path = regex.ReplaceAllLiteralString(path, DS)
+ }
+
+ // Cleaning up remaining dots
+ regex, err = regexp.Compile(DS + `\.{1,2}` + DS)
+ if err != nil {
+ return "Second regex won't compile, FIX IT!"
+ }
+
+ for regex.MatchString(path) == true {
+ path = regex.ReplaceAllLiteralString(path, DS)
+ }
+
+ return path
+}

Недялко обнови решението на 13.10.2013 03:04 (преди над 4 години)

package main
import (
"regexp"
)
// Directory Separator
// "/" for Linux/OSX/FreeBSD/etc.
// "\\" should work for Windows, not tested.
const DS = "/"
// Prepends or/and appends missing Directory Separators
func normilizePath(path string) string {
if len(path) == 0 || path[0] != DS[0] {
path = string(DS[0]) + path
}
if path[len(path)-1] != DS[0] {
path = path + string(DS[0])
}
+
return path
}
// Parses the path
func parsePath(path string) string {
path = normilizePath(path)
+
// Removing /somedir/../ pairs.
// It will parse multiline paths as well.
// p.s. some black magic involved.
regex, err := regexp.Compile(`(?sU)` + DS + `\.*?[^` + DS + `\.]+\.*[^` + DS + `]*` + DS + `\.{2}` + DS)
if err != nil {
return "First regex won't compile, FIX IT!"
}
for regex.MatchString(path) == true {
path = regex.ReplaceAllLiteralString(path, DS)
}
// Cleaning up remaining dots
regex, err = regexp.Compile(DS + `\.{1,2}` + DS)
if err != nil {
return "Second regex won't compile, FIX IT!"
}
-
for regex.MatchString(path) == true {
path = regex.ReplaceAllLiteralString(path, DS)
}
return path
}

Недялко обнови решението на 13.10.2013 15:02 (преди над 4 години)

package main
import (
"regexp"
)
// Directory Separator
// "/" for Linux/OSX/FreeBSD/etc.
// "\\" should work for Windows, not tested.
const DS = "/"
// Prepends or/and appends missing Directory Separators
func normilizePath(path string) string {
if len(path) == 0 || path[0] != DS[0] {
path = string(DS[0]) + path
}
if path[len(path)-1] != DS[0] {
path = path + string(DS[0])
}
return path
}
// Parses the path
func parsePath(path string) string {
path = normilizePath(path)
+ // Remove single dots as they interrupt the next
+ // replacement.
+ regex, err := regexp.Compile(DS + `\.` + DS)
+ if err != nil {
+ return "Zero regex won't compile, FIX IT!"
+ }
+ for regex.MatchString(path) == true {
+ path = regex.ReplaceAllLiteralString(path, DS)
+ }
+
// Removing /somedir/../ pairs.
// It will parse multiline paths as well.
// p.s. some black magic involved.
- regex, err := regexp.Compile(`(?sU)` + DS + `\.*?[^` + DS + `\.]+\.*[^` + DS + `]*` + DS + `\.{2}` + DS)
+ regex, err = regexp.Compile(`(?sU)` + DS + `\.*?[^` + DS + `\.]+\.*[^` + DS + `]*` + DS + `\.{2}` + DS)
if err != nil {
return "First regex won't compile, FIX IT!"
}
for regex.MatchString(path) == true {
path = regex.ReplaceAllLiteralString(path, DS)
}
// Cleaning up remaining dots
regex, err = regexp.Compile(DS + `\.{1,2}` + DS)
if err != nil {
return "Second regex won't compile, FIX IT!"
}
for regex.MatchString(path) == true {
path = regex.ReplaceAllLiteralString(path, DS)
}
return path
}