mirror of
https://github.com/Ed94/Odin.git
synced 2026-08-02 20:58:15 +00:00
Fix new package path
This commit is contained in:
+133
-132
@@ -6,75 +6,75 @@ import "core:unicode/utf8"
|
||||
|
||||
// returns everything preceding the last path element
|
||||
dir :: proc(path: string, new := false, allocator := context.allocator) -> string {
|
||||
if path == "" do return "";
|
||||
if path == "" do return "";
|
||||
|
||||
for i := len(path) - 1; i >= 0; i -= 1 {
|
||||
if path[i] == '/' || path[i] == '\\' {
|
||||
if path[:i] == "" {
|
||||
// path is root
|
||||
return new ? strings.new_string(SEPARATOR_STRING, allocator) : SEPARATOR_STRING;
|
||||
} else {
|
||||
return new ? strings.new_string(path[:i], allocator) : path[:i];
|
||||
}
|
||||
}
|
||||
}
|
||||
for i := len(path) - 1; i >= 0; i -= 1 {
|
||||
if path[i] == '/' || path[i] == '\\' {
|
||||
if path[:i] == "" {
|
||||
// path is root
|
||||
return new ? strings.clone(SEPARATOR_STRING, allocator) : SEPARATOR_STRING;
|
||||
} else {
|
||||
return new ? strings.clone(path[:i], allocator) : path[:i];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// path doesn't contain any folder structure
|
||||
return "";
|
||||
// path doesn't contain any folder structure
|
||||
return "";
|
||||
}
|
||||
|
||||
// returns the final path element
|
||||
base :: proc(path: string, new := false, allocator := context.allocator) -> string {
|
||||
if path == "" do return "";
|
||||
if path == "" do return "";
|
||||
|
||||
end := len(path) - 1;
|
||||
end := len(path) - 1;
|
||||
|
||||
for i := end; i >= 0; i -= 1 {
|
||||
switch path[i] {
|
||||
case '/', '\\':
|
||||
if i != end {
|
||||
return new ? strings.new_string(path[i+1:], allocator) : path[i+1:];
|
||||
} else {
|
||||
end = i; // we don't want trailing slashes
|
||||
}
|
||||
}
|
||||
}
|
||||
for i := end; i >= 0; i -= 1 {
|
||||
switch path[i] {
|
||||
case '/', '\\':
|
||||
if i != end {
|
||||
return new ? strings.clone(path[i+1:], allocator) : path[i+1:];
|
||||
} else {
|
||||
end = i; // we don't want trailing slashes
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// path doesn't contain any folder structure, return entire path
|
||||
return new ? strings.new_string(path, allocator) : path;
|
||||
// path doesn't contain any folder structure, return entire path
|
||||
return new ? strings.clone(path, allocator) : path;
|
||||
}
|
||||
|
||||
// returns the final path element, excluding the file extension if there is one
|
||||
name :: proc(path: string, new := false, allocator := context.allocator) -> string {
|
||||
if path == "" do return "";
|
||||
if path == "" do return "";
|
||||
|
||||
dot := len(path);
|
||||
end := dot - 1;
|
||||
dot := len(path);
|
||||
end := dot - 1;
|
||||
|
||||
for i := end; i >= 0; i -= 1 {
|
||||
switch path[i] {
|
||||
case '.': dot = (dot == end ? i : dot);
|
||||
case '/', '\\': return new ? strings.new_string(path[i+1:dot], allocator) : path[i+1:dot];
|
||||
}
|
||||
}
|
||||
for i := end; i >= 0; i -= 1 {
|
||||
switch path[i] {
|
||||
case '.': dot = (dot == end ? i : dot);
|
||||
case '/', '\\': return new ? strings.clone(path[i+1:dot], allocator) : path[i+1:dot];
|
||||
}
|
||||
}
|
||||
|
||||
// path doesn't contain any folder structure or file extensions; assumed to be a valid file name
|
||||
return new ? strings.new_string(path, allocator) : path;
|
||||
// path doesn't contain any folder structure or file extensions; assumed to be a valid file name
|
||||
return new ? strings.clone(path, allocator) : path;
|
||||
}
|
||||
|
||||
// returns the file extension, if there is one
|
||||
ext :: proc(path: string, new := false, allocator := context.allocator) -> string {
|
||||
if path == "" do return "";
|
||||
if path == "" do return "";
|
||||
|
||||
for i := len(path)-1; i >= 0; i -= 1 {
|
||||
switch path[i] {
|
||||
case '/', '\\': return "";
|
||||
case '.': return new ? strings.new_string(path[i+1:], allocator) : path[i+1:];
|
||||
}
|
||||
}
|
||||
for i := len(path)-1; i >= 0; i -= 1 {
|
||||
switch path[i] {
|
||||
case '/', '\\': return "";
|
||||
case '.': return new ? strings.clone(path[i+1:], allocator) : path[i+1:];
|
||||
}
|
||||
}
|
||||
|
||||
// path does not include a file extension
|
||||
return "";
|
||||
// path does not include a file extension
|
||||
return "";
|
||||
}
|
||||
|
||||
|
||||
@@ -82,119 +82,120 @@ rel :: proc{rel_between, rel_current};
|
||||
|
||||
// returns the relative path from one path to another
|
||||
rel_between :: proc(from, to: string, allocator := context.allocator) -> string {
|
||||
if from == "" || to == "" do return "";
|
||||
if from == "" || to == "" do return "";
|
||||
|
||||
from = full(from, context.temp_allocator);
|
||||
to = full(to, context.temp_allocator);
|
||||
from, to := from, to;
|
||||
from = full(from, context.temp_allocator);
|
||||
to = full(to, context.temp_allocator);
|
||||
|
||||
from_is_dir := is_dir(from);
|
||||
to_is_dir := is_dir(to);
|
||||
from_is_dir := is_dir(from);
|
||||
to_is_dir := is_dir(to);
|
||||
|
||||
index, slash := 0, 0;
|
||||
index, slash := 0, 0;
|
||||
|
||||
for {
|
||||
if index >= len(from) {
|
||||
if index >= len(to) || (from_is_dir && index < len(to) && (to[index] == '/' || to[index] == '\\')) {
|
||||
slash = index;
|
||||
}
|
||||
for {
|
||||
if index >= len(from) {
|
||||
if index >= len(to) || (from_is_dir && index < len(to) && (to[index] == '/' || to[index] == '\\')) {
|
||||
slash = index;
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
else if index >= len(to) {
|
||||
if index >= len(from) || (to_is_dir && index < len(from) && (from[index] == '/' || from[index] == '\\')) {
|
||||
slash = index;
|
||||
}
|
||||
break;
|
||||
}
|
||||
else if index >= len(to) {
|
||||
if index >= len(from) || (to_is_dir && index < len(from) && (from[index] == '/' || from[index] == '\\')) {
|
||||
slash = index;
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
lchar, skip := utf8.decode_rune_in_string(from[index:]);
|
||||
rchar, _ := utf8.decode_rune_in_string(to[index:]);
|
||||
lchar, skip := utf8.decode_rune_in_string(from[index:]);
|
||||
rchar, _ := utf8.decode_rune_in_string(to[index:]);
|
||||
|
||||
if (lchar == '/' || lchar == '\\') && (rchar == '/' || lchar == '\\') {
|
||||
slash = index;
|
||||
}
|
||||
else if lchar != rchar {
|
||||
break;
|
||||
}
|
||||
if (lchar == '/' || lchar == '\\') && (rchar == '/' || lchar == '\\') {
|
||||
slash = index;
|
||||
}
|
||||
else if lchar != rchar {
|
||||
break;
|
||||
}
|
||||
|
||||
index += skip;
|
||||
}
|
||||
index += skip;
|
||||
}
|
||||
|
||||
if slash < 1 {
|
||||
// there is no common path, use the absolute `to` path
|
||||
return strings.new_string(to, allocator);
|
||||
}
|
||||
if slash < 1 {
|
||||
// there is no common path, use the absolute `to` path
|
||||
return strings.clone(to, allocator);
|
||||
}
|
||||
|
||||
from_slashes, to_slashes := 0, 0;
|
||||
from_slashes, to_slashes := 0, 0;
|
||||
|
||||
if slash < len(from) {
|
||||
from = from[slash+1:];
|
||||
|
||||
if from_is_dir {
|
||||
from_slashes += 1;
|
||||
}
|
||||
}
|
||||
else {
|
||||
from = "";
|
||||
}
|
||||
if slash < len(from) {
|
||||
from = from[slash+1:];
|
||||
|
||||
if slash < len(to) {
|
||||
to = to[slash+1:];
|
||||
if from_is_dir {
|
||||
from_slashes += 1;
|
||||
}
|
||||
}
|
||||
else {
|
||||
from = "";
|
||||
}
|
||||
|
||||
if to_is_dir {
|
||||
to_slashes += 1;
|
||||
}
|
||||
}
|
||||
else {
|
||||
to = "";
|
||||
}
|
||||
if slash < len(to) {
|
||||
to = to[slash+1:];
|
||||
|
||||
for char in from {
|
||||
if char == '/' || char == '\\' {
|
||||
from_slashes += 1;
|
||||
}
|
||||
}
|
||||
if to_is_dir {
|
||||
to_slashes += 1;
|
||||
}
|
||||
}
|
||||
else {
|
||||
to = "";
|
||||
}
|
||||
|
||||
for char in to {
|
||||
if char == '/' || char == '\\' {
|
||||
to_slashes += 1;
|
||||
}
|
||||
}
|
||||
for char in from {
|
||||
if char == '/' || char == '\\' {
|
||||
from_slashes += 1;
|
||||
}
|
||||
}
|
||||
|
||||
if from_slashes == 0 {
|
||||
buffer := make([]byte, 2 + len(to), allocator);
|
||||
for char in to {
|
||||
if char == '/' || char == '\\' {
|
||||
to_slashes += 1;
|
||||
}
|
||||
}
|
||||
|
||||
buffer[0] = '.';
|
||||
buffer[1] = SEPARATOR;
|
||||
copy(buffer[2:], ([]byte)(to));
|
||||
if from_slashes == 0 {
|
||||
buffer := make([]byte, 2 + len(to), allocator);
|
||||
|
||||
return string(buffer);
|
||||
}
|
||||
else {
|
||||
buffer := make([]byte, from_slashes*3 + len(to), allocator);
|
||||
buffer[0] = '.';
|
||||
buffer[1] = SEPARATOR;
|
||||
copy(buffer[2:], to);
|
||||
|
||||
for i in 0..from_slashes-1 {
|
||||
buffer[i*3+0] = '.';
|
||||
buffer[i*3+1] = '.';
|
||||
buffer[i*3+2] = SEPARATOR;
|
||||
}
|
||||
return string(buffer);
|
||||
}
|
||||
else {
|
||||
buffer := make([]byte, from_slashes*3 + len(to), allocator);
|
||||
|
||||
copy(buffer[from_slashes*3:], ([]byte)(to));
|
||||
for i in 0..<from_slashes {
|
||||
buffer[i*3+0] = '.';
|
||||
buffer[i*3+1] = '.';
|
||||
buffer[i*3+2] = SEPARATOR;
|
||||
}
|
||||
|
||||
return string(buffer);
|
||||
}
|
||||
copy(buffer[from_slashes*3:], to);
|
||||
|
||||
return "";
|
||||
return string(buffer);
|
||||
}
|
||||
|
||||
return "";
|
||||
}
|
||||
|
||||
// returns the relative path from the current directory to another path
|
||||
rel_current :: proc(to: string, allocator := context.temp_allocator) -> string {
|
||||
return inline rel_between(current(context.temp_allocator), to, allocator);
|
||||
rel_current :: proc(to: string, allocator := context.allocator) -> string {
|
||||
return rel_between(current(context.allocator), to, allocator);
|
||||
}
|
||||
|
||||
|
||||
// splits the path elements into slices of the original path string
|
||||
split :: proc(s: string, allocator := context.temp_allocator) -> []string #no_bounds_check {
|
||||
return inline strings.split(s, []string{"\\", "/"}, true, allocator);
|
||||
split :: proc(s: string, allocator := context.allocator) -> []string {
|
||||
return strings.split_multi(s, []string{"\\", "/"}, true, allocator);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user