Skip to content

Instantly share code, notes, and snippets.

@oxinabox
Last active September 14, 2016 10:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save oxinabox/3593341867bc31131a92fd38d5387353 to your computer and use it in GitHub Desktop.
Save oxinabox/3593341867bc31131a92fd38d5387353 to your computer and use it in GitHub Desktop.
A method to Normalize Paths, by removing ".." ie φ
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"using Base.Test"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"@root_str (macro with 1 method)"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"abstract AbstractPathComp\n",
"immutable RelPathRoot <: AbstractPathComp; end\n",
"const Iᵣ=RelPathRoot()\n",
"\n",
"immutable RelPathComp <: AbstractPathComp\n",
" v::Symbol\n",
"end\n",
"\n",
"\n",
"macro p_str(x)\n",
" :(RelPathComp(Symbol($x)))\n",
"end\n",
"const φ=p\"..\"\n",
"\n",
"immutable AbsolutePathRoot <: AbstractPathComp\n",
" v::Symbol\n",
"end\n",
"macro root_str(x)\n",
" :(AbsolutePathRoot(Symbol($x)))\n",
"end\n"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"normparts (generic function with 7 methods)"
]
},
"execution_count": 20,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"normparts() = RelPathComp[]\n",
"normparts(x)=RelPathComp[x]\n",
"normparts(ir::RelPathRoot) = RelPathRoot[ir]\n",
"normparts(a::AbsolutePathRoot) = AbsolutePathRoot[a]\n",
"\n",
"function normparts(x...)::Vector{RelPathComp}\n",
" #This is going to be a bit slow since julia does not have TCO\n",
" #But for explanitive purposes\n",
" @assert x[1]!=φ\n",
" if x[2]==φ \n",
" return normparts(x[3:end]...)\n",
" else \n",
" [x[1]; normparts(x[2:end]...)] \n",
" end\n",
"end\n",
"\n",
"\n",
"function normparts(ir::RelPathRoot, x...)::Vector{AbstractPathComp}\n",
" x=collect(x)\n",
" if !all(x.==φ)\n",
" ii = findfirst(x.!=φ)\n",
" return [ir; x[1:ii-1]; normparts(x[ii:end]...)]\n",
" else\n",
" return [ir; x]\n",
" end \n",
"end\n",
"\n",
"function normparts(a::AbsolutePathRoot, x...)::Vector{AbstractPathComp}\n",
" normed_parts = normparts(Iᵣ, x...)[2:end] #Run it as if relpath\n",
" if !all(normed_parts.==φ)\n",
" jj = findfirst(normed_parts.!=φ)\n",
" return [a; normed_parts[jj:end]]\n",
" else \n",
" [a]\n",
" end\n",
"end"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Test Summary: | Pass Total\n",
" Relative Paths | 6 6\n",
"Test Summary: | Pass Total\n",
" Absolute Paths | 6 6\n"
]
}
],
"source": [
"@testset \"Relative Paths\" begin\n",
" @test normparts(Iᵣ) == [Iᵣ]\n",
" @test normparts(Iᵣ, φ,φ,φ) == [Iᵣ, φ,φ,φ]\n",
" @test normparts(Iᵣ, φ,φ,φ,p\"foo\") == [Iᵣ, φ,φ,φ,p\"foo\"]\n",
" @test normparts(Iᵣ,p\"foo\",p\"bar\") == [Iᵣ,p\"foo\",p\"bar\"]\n",
" @test normparts(Iᵣ, φ,φ,φ,p\"foo\",p\"bar\") == [Iᵣ, φ,φ,φ,p\"foo\",p\"bar\"]\n",
" @test normparts(Iᵣ, φ,φ,φ,p\"foo\",φ,p\"bar\") == [Iᵣ, φ,φ,φ,p\"bar\"]\n",
"end\n",
"\n",
"@testset \"Absolute Paths\" begin\n",
" @test normparts(root\"C:\") == [root\"C:\"]\n",
" @test normparts(root\"C:\", φ,φ,φ) == [root\"C:\"]\n",
" @test normparts(root\"C:\", φ,φ,φ,p\"foo\") == [root\"C:\",p\"foo\"]\n",
" @test normparts(root\"C:\", φ,φ,p\"foo\",φ,p\"bar\") == [root\"C:\",p\"bar\"]\n",
" @test normparts(root\"C:\", φ,φ,p\"foo\",φ,p\"bar\") == [root\"C:\",p\"bar\"]\n",
" @test normparts(root\"C:\", φ,φ,φ,p\"foo\",φ,p\"bar\") == [root\"C:\",p\"bar\"]\n",
"end;"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Julia 0.5.0-rc4",
"language": "julia",
"name": "julia-0.5"
},
"language_info": {
"file_extension": ".jl",
"mimetype": "application/julia",
"name": "julia",
"version": "0.5.0"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment