Mark Char and Text values to distinguish them from Strings

Also get rid of the unwords and unlines functions which are confusing
and can more easily be reproduced with (join " ") and (join "\n")
This commit is contained in:
Evan Czaplicki 2013-10-10 12:16:47 -07:00
parent 20bbb71359
commit 2c080b6b0d
7 changed files with 40 additions and 41 deletions

View file

@ -62,7 +62,7 @@ internalImports name =
literal lit =
case lit of
Chr c -> string [c]
Chr c -> obj "_N.chr" <| string [c]
Str s -> string s
IntNum n -> IntLit () n
FloatNum n -> NumLit () n

View file

@ -53,7 +53,10 @@ Elm.Native.List.make = function(elm) {
}
function append(xs,ys) {
if (typeof xs === "string") { return xs.concat(ys); }
if (typeof xs === "string") {
if (xs.isText) return Utils.txt(xs.concat(ys));
return xs.concat(ys);
}
if (xs.ctor === '[]') { return ys; }
var root = Cons(xs._0, Nil);
var curr = root;

View file

@ -21,9 +21,10 @@ Elm.Native.Show.make = function(elm) {
return v ? "True" : "False";
} else if (type === "number") {
return v+"";
} else if (v.isChar && v instanceof String) {
return "'" + addSlashes(v) + "'";
} else if (type === "string") {
var quote = v.length === 1 ? "'" : '"';
return quote + addSlashes(v) + quote;
return '"' + addSlashes(v) + '"';
} else if (type === "object" && '_' in v) {
var output = [];
for (var k in v._) {

View file

@ -9,7 +9,7 @@ Elm.Native.String.make = function(elm) {
var Char = Elm.Char.make(elm);
var Maybe = Elm.Maybe.make(elm);
var JS = Elm.JavaScript.make(elm);
var Tuple2 = Elm.Native.Utils.make(elm).Tuple2;
var Utils = Elm.Native.Utils.make(elm);
function isEmpty(str) {
return str.length === 0;
@ -18,8 +18,8 @@ Elm.Native.String.make = function(elm) {
return chr + str;
}
function uncons(str) {
var chr;
return (chr = str[0]) ? Maybe.Just(Tuple2(chr, str.slice(1)))
var hd;
return (hd = str[0]) ? Maybe.Just(Utils.Tuple2(Utils.chr(hd), str.slice(1)))
: Maybe.Nothing;
}
function length(str) {
@ -54,11 +54,11 @@ Elm.Native.String.make = function(elm) {
function join(sep, strs) {
return JS.fromList(strs).join(sep);
}
function repeat(n, chr) {
function repeat(n, str) {
var result = '';
while (n > 0) {
if (n & 1) result += chr;
n >>= 1, chr += chr;
if (n & 1) result += str;
n >>= 1, str += str;
}
return result;
}
@ -103,15 +103,9 @@ Elm.Native.String.make = function(elm) {
function words(str) {
return JS.toList(str.split(/\s+/g));
}
function unwords(str) {
return JS.fromList(str).join(' ');
}
function lines(str) {
return JS.toList(str.split(/\r\n|\r|\n/g));
}
function unlines(str) {
return JS.fromList(str).join('\n');
}
function toUpper(str) {
return str.toUpperCase();
@ -219,9 +213,7 @@ Elm.Native.String.make = function(elm) {
trimRight: trimRight,
words: words,
unwords: unwords,
lines: lines,
unlines: unlines,
toUpper: toUpper,
toLower: toLower,

View file

@ -5,7 +5,7 @@ Elm.Native.Text.make = function(elm) {
if (elm.Native.Text.values) return elm.Native.Text.values;
var JS = Elm.JavaScript.make(elm);
var htmlHeight = Elm.Native.Utils.make(elm).htmlHeight;
var Utils = Elm.Native.Utils.make(elm);
var Color = Elm.Native.Color.make(elm);
var Element = Elm.Graphics.Element.make(elm);
var show = Elm.Native.Show.make(elm).show;
@ -51,15 +51,16 @@ Elm.Native.Text.make = function(elm) {
return arr.join('<br/>');
}
function toText(str) { return properEscape(JS.fromString(str)); }
function toText(str) { return Utils.txt(properEscape(JS.fromString(str))); }
function addTag(tag) { return function(text) {
return '<' + tag + ' style="padding:0;margin:0">' + text + '</' + tag + '>';
function addTag(tag) {
return function(text) {
return Utils.txt('<' + tag + '>' + text + '</' + tag + '>');
}
}
}
function addStyle(style, value, text) {
return "<span style='" + style + ":" + value + "'>" + text + "</span>";
return Utils.txt("<span style='" + style + ":" + value + "'>" + text + "</span>");
}
function typeface(name, text) {
@ -75,8 +76,8 @@ Elm.Native.Text.make = function(elm) {
var bold = addTag('b');
function extract(c) {
if (c._3 === 1) { return 'rgb(' + c._0 + ',' + c._1 + ',' + c._2 + ')'; }
return 'rgba(' + c._0 + ',' + c._1 + ',' + c._2 + ',' + c._3 + ')';
if (c._3 === 1) { return 'rgb(' + c._0 + ', ' + c._1 + ', ' + c._2 + ')'; }
return 'rgba(' + c._0 + ', ' + c._1 + ', ' + c._2 + ', ' + c._3 + ')';
}
function color(c, text) {
return addStyle('color', extract(c), text);
@ -87,7 +88,7 @@ Elm.Native.Text.make = function(elm) {
return addStyle('text-decoration', 'line-through', text);
}
function link(href, text) {
return "<a href='" + toText(href) + "'>" + text + "</a>";
return Utils.txt("<a href='" + toText(href) + "'>" + text + "</a>");
}
function position(pos) {
@ -96,7 +97,7 @@ Elm.Native.Text.make = function(elm) {
_0: '<div style="padding:0;margin:0;text-align:' +
pos + '">' + text + '</div>'
};
var p = A2(htmlHeight, 0, text);
var p = A2(Utils.htmlHeight, 0, text);
return A3(Element.newElement, p._0, p._1, e);
}
}
@ -132,5 +133,4 @@ Elm.Native.Text.make = function(elm) {
asText : asText
};
};

View file

@ -60,6 +60,18 @@ Elm.Native.Utils.make = function(elm) {
var Tuple0 = { ctor: "_Tuple0" };
function Tuple2(x,y) { return { ctor:"_Tuple2", _0:x, _1:y } }
function chr(c) {
var x = new String(c);
x.isChar = true;
return x;
}
function txt(str) {
var t = new String(str);
t.isText = true;
return t;
}
var count = 0;
function guid(_) { return count++ }
@ -149,6 +161,8 @@ Elm.Native.Utils.make = function(elm) {
compare:F2(compare),
Tuple0:Tuple0,
Tuple2:Tuple2,
chr:chr,
txt:txt,
copy: copy,
remove: remove,
replace: replace,

View file

@ -6,7 +6,7 @@ module String where
map, filter, foldl, foldr, any, all, repeat
# Split and Join
@docs split, join, words, unwords, lines, unlines
@docs split, join, words, lines
# Get Substrings
@docs sub, left, right, dropLeft, dropRight
@ -23,7 +23,6 @@ Cosmetic operations such as padding with extra characters or trimming whitespace
@docs toUpper, toLower,
pad, padLeft, padRight,
trim, trimLeft, trimRight
-}
import Native.String
@ -147,21 +146,11 @@ trimRight = Native.String.trimRight
words : String -> [String]
words = Native.String.words
{-|
-}
unwords : [String] -> String
unwords = Native.String.unwords
{-|
-}
lines : String -> [String]
lines = Native.String.lines
{-|
-}
unlines : [String] -> String
unlines = Native.String.unlines
{-|
-}
toUpper : String -> String