Skip to content

Commit

Permalink
centralize path escaping in haxe.io.Path (non-public) and do not esca…
Browse files Browse the repository at this point in the history
…pe dots
  • Loading branch information
Simn committed Feb 28, 2015
1 parent 822f1c1 commit ac2055b
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 42 deletions.
2 changes: 1 addition & 1 deletion codegen.ml
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ let is_removable_field ctx f =

let escape_res_name name allow_dirs =
ExtString.String.replace_chars (fun chr ->
if (chr >= 'a' && chr <= 'z') || (chr >= 'A' && chr <= 'Z') || (chr >= '0' && chr <= '9') || chr = '_' then
if (chr >= 'a' && chr <= 'z') || (chr >= 'A' && chr <= 'Z') || (chr >= '0' && chr <= '9') || chr = '_' || chr = '.' then
Char.escaped chr
else if chr = '/' && allow_dirs then
"/"
Expand Down
18 changes: 4 additions & 14 deletions std/cs/_std/haxe/Resource.hx
Original file line number Diff line number Diff line change
Expand Up @@ -44,29 +44,19 @@ package haxe;
return content.copy();
}

private static function unescapeName( name : String ) : String
{
var regex = ~/-x([0-9]+)/g;
return regex.map(name, function(regex) return String.fromCharCode(Std.parseInt(regex.matched(1))));
}

private static function escapeName( name : String ) : String
{
var regex = ~/[^A-Za-z0-9_\/]/g;
return regex.map(name, function(v) return '-x' + v.matched(0).charCodeAt(0));
}

@:access(haxe.io.Path.escape)
public static function getString( name : String ) : String {
name = escapeName(name);
name = haxe.io.Path.escape(name, true);
var path = getPaths().get(name);
var str = cs.Lib.toNativeType(haxe.Resource).Assembly.GetManifestResourceStream(path);
if (str != null)
return new cs.io.NativeInput(str).readAll().toString();
return null;
}

@:access(haxe.io.Path.escape)
public static function getBytes( name : String ) : haxe.io.Bytes {
name = escapeName(name);
name = haxe.io.Path.escape(name, true);
var path = getPaths().get(name);
var str = cs.Lib.toNativeType(haxe.Resource).Assembly.GetManifestResourceStream(path);
if (str != null)
Expand Down
11 changes: 11 additions & 0 deletions std/haxe/io/Path.hx
Original file line number Diff line number Diff line change
Expand Up @@ -303,9 +303,20 @@ class Path {
/**
Returns true if the path is an absolute path, and false otherwise.
**/
@:require(haxe_ver >= 3.2)
public static function isAbsolute ( path : String ) : Bool {
if (StringTools.startsWith(path, '/')) return true;
if (path.charAt(1) == ':') return true;
return false;
}

private static function unescape( path : String ) : String {
var regex = ~/-x([0-9][0-9])/g;
return regex.map(path, function(regex) return String.fromCharCode(Std.parseInt(regex.matched(1))));
}

private static function escape( path : String, allowSlashes : Bool = false ) : String {
var regex = allowSlashes ? ~/[^A-Za-z0-9_\/\\\.]/g : ~/[^A-Za-z0-9_\.]/g;
return regex.map(path, function(v) return '-x' + v.matched(0).charCodeAt(0));
}
}
18 changes: 4 additions & 14 deletions std/java/_std/haxe/Resource.hx
Original file line number Diff line number Diff line change
Expand Up @@ -29,29 +29,19 @@ package haxe;
return content.copy();
}

private static function unescapeName( name : String ) : String
{
var regex = ~/-x([0-9]+)/g;
return regex.map(name, function(regex) return String.fromCharCode(Std.parseInt(regex.matched(1))));
}

private static function escapeName( name : String ) : String
{
var regex = ~/[^A-Za-z0-9_\/]/g;
return regex.map(name, function(v) return '-x' + v.matched(0).charCodeAt(0));
}

@:access(haxe.io.Path.escape)
public static function getString( name : String ) : String {
name = escapeName(name);
name = haxe.io.Path.escape(name, true);
var stream = cast(Resource, java.lang.Class<Dynamic>).getResourceAsStream("/" + name);
if (stream == null)
return null;
var stream = new java.io.NativeInput(stream);
return stream.readAll().toString();
}

@:access(haxe.io.Path.escape)
public static function getBytes( name : String ) : haxe.io.Bytes {
name = escapeName(name);
name = haxe.io.Path.escape(name, true);
var stream = cast(Resource, java.lang.Class<Dynamic>).getResourceAsStream("/" + name);
if (stream == null)
return null;
Expand Down
17 changes: 4 additions & 13 deletions std/php/_std/haxe/Resource.hx
Original file line number Diff line number Diff line change
Expand Up @@ -35,26 +35,17 @@ class Resource {
return untyped __call__('dirname', __php__('__FILE__'))+"/../../res";
}

private static function unescapeName( name : String ) : String
{
var regex = ~/-x([0-9]{2})/g;
return regex.map(name, function(regex) return String.fromCharCode(Std.parseInt(regex.matched(1))));
}

private static function escapeName( name : String ) : String {
var regex = ~/[^A-Za-z0-9_]/g;
return regex.map(name, function(v) return '-x' + v.matched(0).charCodeAt(0));
}

@:access(haxe.io.Path.escape)
static function getPath(name : String) : String {
return getDir()+'/'+escapeName(name);
return getDir()+'/'+haxe.io.Path.escape(name);
}

@:access(haxe.io.Path.unescape)
public static function listNames() : Array<String> {
var a = sys.FileSystem.readDirectory(getDir());
if(a[0] == '.') a.shift();
if(a[0] == '..') a.shift();
return a.map(function(s) return unescapeName(s));
return a.map(function(s) return haxe.io.Path.unescape(s));
}

public static function getString( name : String ) : String {
Expand Down

0 comments on commit ac2055b

Please sign in to comment.