on master help sign in
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")

commit2c080b6b0df2fc35801d6b6e35207ef74153fd35 authorEvan Czaplicki <info@elm-lang.org> dateThu 10 Oct 2013, 12:16 (13 years ago) parent20bbb71
7 files changed+40−41
compiler/Generate/JavaScript.hs+1−1 libraries/Native/List.js+4−1 libraries/Native/Show.js+3−2 libraries/Native/String.js+6−14 libraries/Native/Text.js+11−11 libraries/Native/Utils.js+14 libraries/String.elm+1−12
compiler/Generate/JavaScript.hs+1−1
@@ -62,7 +62,7 @@ internalImports name =
6262
6363literal lit =
6464 case lit of
65− Chr c -> string [c]
65+ Chr c -> obj "_N.chr" <| string [c]
6666 Str s -> string s
6767 IntNum n -> IntLit () n
6868 FloatNum n -> NumLit () n
libraries/Native/List.js+4−1
@@ -53,7 +53,10 @@ Elm.Native.List.make = function(elm) {
5353 }
5454
5555 function append(xs,ys) {
56− if (typeof xs === "string") { return xs.concat(ys); }
56+ if (typeof xs === "string") {
57+ if (xs.isText) return Utils.txt(xs.concat(ys));
58+ return xs.concat(ys);
59+ }
5760 if (xs.ctor === '[]') { return ys; }
5861 var root = Cons(xs._0, Nil);
5962 var curr = root;
libraries/Native/Show.js+3−2
@@ -21,9 +21,10 @@ Elm.Native.Show.make = function(elm) {
2121 return v ? "True" : "False";
2222 } else if (type === "number") {
2323 return v+"";
24+ } else if (v.isChar && v instanceof String) {
25+ return "'" + addSlashes(v) + "'";
2426 } else if (type === "string") {
25− var quote = v.length === 1 ? "'" : '"';
26− return quote + addSlashes(v) + quote;
27+ return '"' + addSlashes(v) + '"';
2728 } else if (type === "object" && '_' in v) {
2829 var output = [];
2930 for (var k in v._) {
libraries/Native/String.js+6−14
@@ -9,7 +9,7 @@ Elm.Native.String.make = function(elm) {
99 var Char = Elm.Char.make(elm);
1010 var Maybe = Elm.Maybe.make(elm);
1111 var JS = Elm.JavaScript.make(elm);
12− var Tuple2 = Elm.Native.Utils.make(elm).Tuple2;
12+ var Utils = Elm.Native.Utils.make(elm);
1313
1414 function isEmpty(str) {
1515 return str.length === 0;
@@ -18,8 +18,8 @@ Elm.Native.String.make = function(elm) {
1818 return chr + str;
1919 }
2020 function uncons(str) {
21− var chr;
22− return (chr = str[0]) ? Maybe.Just(Tuple2(chr, str.slice(1)))
21+ var hd;
22+ return (hd = str[0]) ? Maybe.Just(Utils.Tuple2(Utils.chr(hd), str.slice(1)))
2323 : Maybe.Nothing;
2424 }
2525 function length(str) {
@@ -54,11 +54,11 @@ Elm.Native.String.make = function(elm) {
5454 function join(sep, strs) {
5555 return JS.fromList(strs).join(sep);
5656 }
57− function repeat(n, chr) {
57+ function repeat(n, str) {
5858 var result = '';
5959 while (n > 0) {
60− if (n & 1) result += chr;
61− n >>= 1, chr += chr;
60+ if (n & 1) result += str;
61+ n >>= 1, str += str;
6262 }
6363 return result;
6464 }
@@ -103,15 +103,9 @@ Elm.Native.String.make = function(elm) {
103103 function words(str) {
104104 return JS.toList(str.split(/\s+/g));
105105 }
106− function unwords(str) {
107− return JS.fromList(str).join(' ');
108− }
109106 function lines(str) {
110107 return JS.toList(str.split(/\r\n|\r|\n/g));
111108 }
112− function unlines(str) {
113− return JS.fromList(str).join('\n');
114− }
115109
116110 function toUpper(str) {
117111 return str.toUpperCase();
@@ -219,9 +213,7 @@ Elm.Native.String.make = function(elm) {
219213 trimRight: trimRight,
220214
221215 words: words,
222− unwords: unwords,
223216 lines: lines,
224− unlines: unlines,
225217
226218 toUpper: toUpper,
227219 toLower: toLower,
libraries/Native/Text.js+11−11
@@ -5,7 +5,7 @@ Elm.Native.Text.make = function(elm) {
55 if (elm.Native.Text.values) return elm.Native.Text.values;
66
77 var JS = Elm.JavaScript.make(elm);
8− var htmlHeight = Elm.Native.Utils.make(elm).htmlHeight;
8+ var Utils = Elm.Native.Utils.make(elm);
99 var Color = Elm.Native.Color.make(elm);
1010 var Element = Elm.Graphics.Element.make(elm);
1111 var show = Elm.Native.Show.make(elm).show;
@@ -51,15 +51,16 @@ Elm.Native.Text.make = function(elm) {
5151 return arr.join('<br/>');
5252 }
5353
54− function toText(str) { return properEscape(JS.fromString(str)); }
54+ function toText(str) { return Utils.txt(properEscape(JS.fromString(str))); }
5555
56− function addTag(tag) { return function(text) {
57− return '<' + tag + ' style="padding:0;margin:0">' + text + '</' + tag + '>';
56+ function addTag(tag) {
57+ return function(text) {
58+ return Utils.txt('<' + tag + '>' + text + '</' + tag + '>');
59+ }
5860 }
59− }
6061
6162 function addStyle(style, value, text) {
62− return "<span style='" + style + ":" + value + "'>" + text + "</span>";
63+ return Utils.txt("<span style='" + style + ":" + value + "'>" + text + "</span>");
6364 }
6465
6566 function typeface(name, text) {
@@ -75,8 +76,8 @@ Elm.Native.Text.make = function(elm) {
7576 var bold = addTag('b');
7677
7778 function extract(c) {
78− if (c._3 === 1) { return 'rgb(' + c._0 + ',' + c._1 + ',' + c._2 + ')'; }
79− return 'rgba(' + c._0 + ',' + c._1 + ',' + c._2 + ',' + c._3 + ')';
79+ if (c._3 === 1) { return 'rgb(' + c._0 + ', ' + c._1 + ', ' + c._2 + ')'; }
80+ return 'rgba(' + c._0 + ', ' + c._1 + ', ' + c._2 + ', ' + c._3 + ')';
8081 }
8182 function color(c, text) {
8283 return addStyle('color', extract(c), text);
@@ -87,7 +88,7 @@ Elm.Native.Text.make = function(elm) {
8788 return addStyle('text-decoration', 'line-through', text);
8889 }
8990 function link(href, text) {
90− return "<a href='" + toText(href) + "'>" + text + "</a>";
91+ return Utils.txt("<a href='" + toText(href) + "'>" + text + "</a>");
9192 }
9293
9394 function position(pos) {
@@ -96,7 +97,7 @@ Elm.Native.Text.make = function(elm) {
9697 _0: '<div style="padding:0;margin:0;text-align:' +
9798 pos + '">' + text + '</div>'
9899 };
99− var p = A2(htmlHeight, 0, text);
100+ var p = A2(Utils.htmlHeight, 0, text);
100101 return A3(Element.newElement, p._0, p._1, e);
101102 }
102103 }
@@ -132,5 +133,4 @@ Elm.Native.Text.make = function(elm) {
132133
133134 asText : asText
134135 };
135−
136136};
libraries/Native/Utils.js+14
@@ -60,6 +60,18 @@ Elm.Native.Utils.make = function(elm) {
6060 var Tuple0 = { ctor: "_Tuple0" };
6161 function Tuple2(x,y) { return { ctor:"_Tuple2", _0:x, _1:y } }
6262
63+ function chr(c) {
64+ var x = new String(c);
65+ x.isChar = true;
66+ return x;
67+ }
68+
69+ function txt(str) {
70+ var t = new String(str);
71+ t.isText = true;
72+ return t;
73+ }
74+
6375 var count = 0;
6476 function guid(_) { return count++ }
6577
@@ -149,6 +161,8 @@ Elm.Native.Utils.make = function(elm) {
149161 compare:F2(compare),
150162 Tuple0:Tuple0,
151163 Tuple2:Tuple2,
164+ chr:chr,
165+ txt:txt,
152166 copy: copy,
153167 remove: remove,
154168 replace: replace,
libraries/String.elm+1−12
@@ -6,7 +6,7 @@ module String where
66 map, filter, foldl, foldr, any, all, repeat
77
88# Split and Join
9−@docs split, join, words, unwords, lines, unlines
9+@docs split, join, words, lines
1010
1111# Get Substrings
1212@docs sub, left, right, dropLeft, dropRight
@@ -23,7 +23,6 @@ Cosmetic operations such as padding with extra characters or trimming whitespace
2323@docs toUpper, toLower,
2424 pad, padLeft, padRight,
2525 trim, trimLeft, trimRight
26−
2726-}
2827
2928import Native.String
@@ -147,21 +146,11 @@ trimRight = Native.String.trimRight
147146words : String -> [String]
148147words = Native.String.words
149148
150−{-|
151−-}
152−unwords : [String] -> String
153−unwords = Native.String.unwords
154−
155149{-|
156150-}
157151lines : String -> [String]
158152lines = Native.String.lines
159153
160−{-|
161−-}
162−unlines : [String] -> String
163−unlines = Native.String.unlines
164−
165154{-|
166155-}
167156toUpper : String -> String