From 709106cda833117e90666bb4cb09323d30bb8bdf Mon Sep 17 00:00:00 2001 From: Martin Nilsson <nilsson@opera.com> Date: Mon, 18 Aug 2014 20:12:05 +0200 Subject: [PATCH] Improved cast method. --- lib/modules/ADT.pmod/Interval.pike | 4 +--- lib/modules/ADT.pmod/Queue.pike | 9 ++++----- lib/modules/ADT.pmod/Relation.pmod/Binary.pike | 10 ++++------ lib/modules/ADT.pmod/Set.pike | 3 --- lib/modules/ADT.pmod/Stack.pike | 12 +++++------- lib/modules/Geography.pmod/Position.pike | 15 ++++++++------- lib/modules/Locale.pmod/module.pmod | 1 - lib/modules/MIME.pmod/module.pmod | 9 ++------- lib/modules/Parser.pmod/XML.pmod/Tree.pmod | 1 - lib/modules/Search.pmod/Utils.pmod | 1 - lib/modules/Stdio.pmod/FakeFile.pike | 6 ++---- 11 files changed, 26 insertions(+), 45 deletions(-) diff --git a/lib/modules/ADT.pmod/Interval.pike b/lib/modules/ADT.pmod/Interval.pike index 3bdf6e43df..bd645f75fd 100644 --- a/lib/modules/ADT.pmod/Interval.pike +++ b/lib/modules/ADT.pmod/Interval.pike @@ -247,9 +247,7 @@ mixed beginning() { return start; } mixed end() { return stop; } protected mixed cast(string type) { - switch (type) { - case "array": + if( type=="array" ) return ({ start, stop }); - } return UNDEFINED; } diff --git a/lib/modules/ADT.pmod/Queue.pike b/lib/modules/ADT.pmod/Queue.pike index fe5e8cb8a9..01c70a01ef 100644 --- a/lib/modules/ADT.pmod/Queue.pike +++ b/lib/modules/ADT.pmod/Queue.pike @@ -74,11 +74,10 @@ void flush() } //! It is possible to cast ADT.Queue to an array. -protected mixed cast(string to) { - switch(to) { - case "object": return this; - case "array": return l+({}); - } +protected mixed cast(string to) +{ + if( to=="array" ) + return l+({}); return UNDEFINED; } diff --git a/lib/modules/ADT.pmod/Relation.pmod/Binary.pike b/lib/modules/ADT.pmod/Relation.pmod/Binary.pike index bb525b8237..18d5142c9a 100644 --- a/lib/modules/ADT.pmod/Relation.pmod/Binary.pike +++ b/lib/modules/ADT.pmod/Relation.pmod/Binary.pike @@ -309,11 +309,9 @@ protected class _get_iterator { } } -mixed cast(string to) { - switch(to) { - case "mapping": +mixed cast(string to) +{ + if( to=="mapping" ) return copy_value(val); - default: - return UNDEFINED; - } + return UNDEFINED; } diff --git a/lib/modules/ADT.pmod/Set.pike b/lib/modules/ADT.pmod/Set.pike index 97eb408ef4..1c2d32bde1 100644 --- a/lib/modules/ADT.pmod/Set.pike +++ b/lib/modules/ADT.pmod/Set.pike @@ -248,9 +248,6 @@ protected mixed cast(string to) { switch(to) { - case "object": - return this; - case "array": return indices(set); diff --git a/lib/modules/ADT.pmod/Stack.pike b/lib/modules/ADT.pmod/Stack.pike index 4de3ce6c3f..fe66b76b1b 100644 --- a/lib/modules/ADT.pmod/Stack.pike +++ b/lib/modules/ADT.pmod/Stack.pike @@ -141,13 +141,11 @@ this_program `+(this_program s) { return ns; } -protected mixed cast(string to) { - switch(to) { - case "array": - return _values(); - default: - return UNDEFINED; - } +protected mixed cast(string to) +{ + if( to=="array" ) + return _values(); + return UNDEFINED; } string _sprintf(int t) { diff --git a/lib/modules/Geography.pmod/Position.pike b/lib/modules/Geography.pmod/Position.pike index 9c0d5f08af..b5ab7c0d5c 100644 --- a/lib/modules/Geography.pmod/Position.pike +++ b/lib/modules/Geography.pmod/Position.pike @@ -570,13 +570,14 @@ array(float) ECEF() { protected string|array cast(string to) { - if (to[..4]=="array") - return ({lat,long}); - - if (to[..5]=="string") - return latitude()+" "+longitude(); - - return UNDEFINED; + switch(to) + { + case "array": + return ({lat,long}); + case "string": + return latitude()+" "+longitude(); + } + return UNDEFINED; } //! diff --git a/lib/modules/Locale.pmod/module.pmod b/lib/modules/Locale.pmod/module.pmod index 5392de336e..a1e61b9404 100644 --- a/lib/modules/Locale.pmod/module.pmod +++ b/lib/modules/Locale.pmod/module.pmod @@ -563,7 +563,6 @@ class DeferredLocale( protected string project, protected mixed cast(string to) { if(to=="string") return lookup(); - if(to=="mixed" || to=="object") return this; return UNDEFINED; } diff --git a/lib/modules/MIME.pmod/module.pmod b/lib/modules/MIME.pmod/module.pmod index ec3a986383..5ecbafef98 100644 --- a/lib/modules/MIME.pmod/module.pmod +++ b/lib/modules/MIME.pmod/module.pmod @@ -135,14 +135,9 @@ protected class StringRange } protected mixed cast(string type) { - switch(type) { - case "string": + if( type == "string" ) return data[start..end-1]; - case "object": - return this_object(); - default: - return UNDEFINED; - } + return UNDEFINED; } protected int _search(string frag, int|void pos) { diff --git a/lib/modules/Parser.pmod/XML.pmod/Tree.pmod b/lib/modules/Parser.pmod/XML.pmod/Tree.pmod index d5fed645fd..0086ccc53c 100644 --- a/lib/modules/Parser.pmod/XML.pmod/Tree.pmod +++ b/lib/modules/Parser.pmod/XML.pmod/Tree.pmod @@ -1028,7 +1028,6 @@ protected class VirtualNode { //! It is possible to cast a node to a string, which will return //! @[render_xml()] for that node. protected mixed cast(string to) { - if(to=="object") return this; if(to=="string") return render_xml(); return UNDEFINED; } diff --git a/lib/modules/Search.pmod/Utils.pmod b/lib/modules/Search.pmod/Utils.pmod index 07691e2ad5..2703349178 100644 --- a/lib/modules/Search.pmod/Utils.pmod +++ b/lib/modules/Search.pmod/Utils.pmod @@ -149,7 +149,6 @@ class ProfileEntry { protected mixed cast(string to) { switch(to) { - case "object": return this_object(); case "array": return indices(vals); case "multiset": return (multiset)indices(vals); default: diff --git a/lib/modules/Stdio.pmod/FakeFile.pike b/lib/modules/Stdio.pmod/FakeFile.pike index ac676a12a1..be870ca95d 100644 --- a/lib/modules/Stdio.pmod/FakeFile.pike +++ b/lib/modules/Stdio.pmod/FakeFile.pike @@ -320,10 +320,8 @@ string _sprintf(int t) { //! A FakeFile can be casted to a string. protected mixed cast(string to) { - switch(to) { - case "string": return data; - case "object": return this; - } + if( to == "string" ) + return data; return UNDEFINED; } -- GitLab