From 951d35902c8d160441478126a32a0723c11278f1 Mon Sep 17 00:00:00 2001 From: Ms2ger Date: Fri, 14 Feb 2014 13:20:48 +0100 Subject: [PATCH] Add str.rs. --- src/components/util/str.rs | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 src/components/util/str.rs diff --git a/src/components/util/str.rs b/src/components/util/str.rs new file mode 100644 index 00000000000..13272d8ec42 --- /dev/null +++ b/src/components/util/str.rs @@ -0,0 +1,21 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +pub type DOMString = ~str; + +pub fn null_str_as_empty(s: &Option) -> DOMString { + // We don't use map_default because it would allocate ~"" even for Some. + match *s { + Some(ref s) => s.clone(), + None => ~"" + } +} + +pub fn null_str_as_empty_ref<'a>(s: &'a Option) -> &'a str { + match *s { + Some(ref s) => s.as_slice(), + None => &'a "" + } +} +