Discussion:
Parsing namespace name strings
(too old to reply)
Alan Grunwald
2024-11-29 21:03:25 UTC
Permalink
I find I regularly want to know things like "what is first part of a
namespace name", "the first two parts" etc. I generally want to get the
same answer whether the name is a::b::c or ::a::b::c.

I can (and do) do this by getting a list of parts via something like

split [string map {"::" ":"} $name] ":"

but this is clunky - is there something like [namespace split] that
would return a list of parts?

For example, I'd like namespace split a::b::c to return {a b c}, and
namespace split ::d::e::f::g to return {d e f g}.

As a followup, if I write a proc namespaceSplit that does what I want,
is there a user-level way to modify the [namespace] command so that a
can execute namespaceSplit via [namespace split]?

Many thanks
Emiliano
2024-11-29 23:16:01 UTC
Permalink
On Fri, 29 Nov 2024 21:03:25 +0000
Post by Alan Grunwald
I find I regularly want to know things like "what is first part of a
namespace name", "the first two parts" etc. I generally want to get the
same answer whether the name is a::b::c or ::a::b::c.
I can (and do) do this by getting a list of parts via something like
split [string map {"::" ":"} $name] ":"
but this is clunky - is there something like [namespace split] that
would return a list of parts?
For example, I'd like namespace split a::b::c to return {a b c}, and
namespace split ::d::e::f::g to return {d e f g}.
Easy combining [namespace tail] and [namespace qualifiers]:

proc ns2list {ns} {
set l {}
while {$ns ne {}} {
lappend l [namespace tail $ns]
set ns [namespace qualifiers $ns]
}
lreverse $l
}

% ns2list foo::bar::
foo bar {}
% ns2list foo::bar
foo bar
% ns2list ::::foo::bar::
foo bar {}
% ns2list ::foo::bar
foo bar
% ns2list {}
%
Post by Alan Grunwald
As a followup, if I write a proc namespaceSplit that does what I want,
is there a user-level way to modify the [namespace] command so that a
can execute namespaceSplit via [namespace split]?
Since the [namespace] command is an ensemble, its easy to add a subcommand

% namespace ensemble configure namespace -map [dict merge [namespace ensemble configure namespace -map] {split ::ns2list}]
% namespace split ::foo::bar::baz
foo bar baz
Post by Alan Grunwald
Many thanks
Regards
--
Emiliano
Alan Grunwald
2024-11-30 13:12:34 UTC
Permalink
Post by Emiliano
On Fri, 29 Nov 2024 21:03:25 +0000
Post by Alan Grunwald
I find I regularly want to know things like "what is first part of a
namespace name", "the first two parts" etc. I generally want to get the
same answer whether the name is a::b::c or ::a::b::c.
I can (and do) do this by getting a list of parts via something like
split [string map {"::" ":"} $name] ":"
but this is clunky - is there something like [namespace split] that
would return a list of parts?
For example, I'd like namespace split a::b::c to return {a b c}, and
namespace split ::d::e::f::g to return {d e f g}.
proc ns2list {ns} {
set l {}
while {$ns ne {}} {
lappend l [namespace tail $ns]
set ns [namespace qualifiers $ns]
}
lreverse $l
}
foo bar {}
% ns2list foo::bar
foo bar
foo bar {}
% ns2list ::foo::bar
foo bar
% ns2list {}
%
Post by Alan Grunwald
As a followup, if I write a proc namespaceSplit that does what I want,
is there a user-level way to modify the [namespace] command so that a
can execute namespaceSplit via [namespace split]?
Since the [namespace] command is an ensemble, its easy to add a subcommand
% namespace ensemble configure namespace -map [dict merge [namespace ensemble configure namespace -map] {split ::ns2list}]
% namespace split ::foo::bar::baz
foo bar baz
Post by Alan Grunwald
Many thanks
Regards
Thanks Emiliano.

I did some testing after I posted last night, and I found that
manipulating the name with [string] commands ran faster than with
[namespace tail] and [namespace qualifiers]. I must admit that when
using the [namespace] subcommands, I was processing the list with
[linsert l 0 ...] rather than using [lappend l] and [lreverse] which
probably makes a difference.

Thanks also for the pointer about adding a new subcommand to the
namespace ensemble. I've never played with [namespace ensemble] before
and I can see why I was a little intimidated.
--
Alan
Loading...