Skip to content

Instantly share code, notes, and snippets.

@zoffixznet

zoffixznet/p6.p6 Secret

Created April 19, 2017 23:50
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 zoffixznet/734b3514d3684901ab7fe7581e27858b to your computer and use it in GitHub Desktop.
Save zoffixznet/734b3514d3684901ab7fe7581e27858b to your computer and use it in GitHub Desktop.
nqp::if(
$nofile,
nqp::stmts(
# This NQP chunklet does:
# /^ (<$volume_rx>?) (.*) /; (~$0, ~$1, '')
#
# The $vol-pos is the position in the $path where volume ends and
# rest of the path begins. We search the string for a drive letter,
# If we find it, then the we'll cut the $path at pos 2. If we don't,
# Search for a UNC path. We check if the $path starts with two
# slash/backslash chars. Then, we check the char after them is NOT
# a slash/backslash. Then we look for index of '/' and index of '\'
# and use the smaller index of the two. Now, all we do is search for
# index of '/' and index '\': if we find neither, our whole $path
# is the volume, otherwise use the smallest value as the cut point
# for the path. Lastly, we we found neither the drive letter nor
# the UNC path, then just return the $path as the dirname and use
# empty string for the volume. Simple!
nqp::if(
(nqp::eqat($path, ':', 1) # /^ <[A..Z a..z]> ':'/
&& ( (nqp::isge_i(($_ := nqp::ord($path)), 65) # drive letter
&& nqp::isle_i($_, 90))
|| (nqp::isge_i($_, 97) && nqp::isle_i($_, 122)))),
($vol-pos = 2)),
nqp::if(
(nqp::iseq_i(($_ := nqp::ord($path)), 92), # /^<[\\/]>
|| nqp::iseq_i($_, 47))
&& (nqp::iseq_i(($_ := nqp::ord($path, 1)), 92), # /^.<[\\/]>
|| nqp::iseq_i($_, 47))
&& (nqp::isne_i(($_ := nqp::ord($path, 2)), 92), # /^..<-[\\/]>
|| nqp::isne_i($_, 47))
&& nqp::stmts( # find position of first slash/backlash
( $_ := nqp::index($path, '/', 3)),
(my int $slash1 = nqp::index($path, 「\」, 3)),
nqp::if( # if we found both types of slashes, use the first one
nqp::islt_i($_, $slash1) && nqp::isne_i($_, -1),
($slash1 = $_)),
nqp::isne_i($slash1, -1)) # did find find any slashes?
&& nqp::stmts( # find position of next slash/backlash
($slash1 = nqp::add_i($slash1, 1)), # bump to next pos
( $_ := nqp::index($path, '/', $slash1)),
(my int $slash2 = nqp::index($path, 「\」, $slash1)),
nqp::if( # if we found both types of slashes, use the first one
nqp::islt_i($_, $slash1) && nqp::isne_i($_, -1),
($slash2 = $_)),
nqp::if( # if slash2 is after slash1, this isn't UNC path
nqp::iseq_i($slash2, $slash1),
0, # give False to our conditional
nqp::if( # if didn't find slashes, use str end as vol-pos
nqp::iseq_i($slash1, -1),
($slash1 = nqp::chars($path))))), # else, use $slash2 pos
($vol-pos = $slash2)),
( # return the...
nqp::substr($path, 0, $vol-pos), # volume...
nqp::substr($path, $vol-pos), # dirs...
'', # file
),
))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment