Unverified Commit 3e94ebc7 authored by Conor Shipp's avatar Conor Shipp Committed by GitHub

Add Reverse Text Block (#1998)

Co-authored-by: default avatarEvan W. Patton <ewpatton@mit.edu>
parent 40f66fc1
......@@ -497,3 +497,18 @@ Blockly.Blocks['text_is_string'] = {
},
typeblock: [{translatedName: Blockly.Msg.LANG_TEXT_TEXT_IS_STRING_TITLE}]
};
Blockly.Blocks['text_reverse'] = {
// String reverse.
category: 'Text',
helpUrl: Blockly.Msg.LANG_TEXT_REVERSE_HELPURL,
init: function () {
this.setColour(Blockly.TEXT_CATEGORY_HUE);
this.setOutput(true, Blockly.Blocks.Utilities.YailTypeToBlocklyType("text", Blockly.Blocks.Utilities.OUTPUT));
this.appendValueInput('VALUE')
.setCheck(Blockly.Blocks.Utilities.YailTypeToBlocklyType("text", Blockly.Blocks.Utilities.INPUT))
.appendField(Blockly.Msg.LANG_TEXT_REVERSE_INPUT);
this.setTooltip(Blockly.Msg.LANG_TEXT_REVERSE_TOOLTIP);
},
typeblock: [{translatedName: Blockly.Msg.LANG_TEXT_REVERSE_INPUT}]
};
......@@ -318,3 +318,19 @@ Blockly.Yail['text_is_string'] = function() {
code = code + Blockly.Yail.YAIL_DOUBLE_QUOTE + "is a string?" + Blockly.Yail.YAIL_DOUBLE_QUOTE + Blockly.Yail.YAIL_CLOSE_COMBINATION;
return [ code, Blockly.Yail.ORDER_ATOMIC ];
};
Blockly.Yail['text_reverse'] = function() {
// String reverse.
var argument = Blockly.Yail.valueToCode(this, 'VALUE', Blockly.Yail.ORDER_NONE) || "\"\"";
var code = Blockly.Yail.YAIL_CALL_YAIL_PRIMITIVE + "string-reverse"
+ Blockly.Yail.YAIL_SPACER;
code = code + Blockly.Yail.YAIL_OPEN_COMBINATION
+ Blockly.Yail.YAIL_LIST_CONSTRUCTOR + Blockly.Yail.YAIL_SPACER
+ argument + Blockly.Yail.YAIL_CLOSE_COMBINATION;
code = code + Blockly.Yail.YAIL_SPACER + Blockly.Yail.YAIL_QUOTE
+ Blockly.Yail.YAIL_OPEN_COMBINATION + "text"
+ Blockly.Yail.YAIL_CLOSE_COMBINATION + Blockly.Yail.YAIL_SPACER;
code = code + Blockly.Yail.YAIL_DOUBLE_QUOTE + "reverse"
+ Blockly.Yail.YAIL_DOUBLE_QUOTE + Blockly.Yail.YAIL_CLOSE_COMBINATION;
return [ code, Blockly.Yail.ORDER_ATOMIC ];
};
......@@ -758,6 +758,10 @@ Blockly.Msg.en.switch_language_to_english = {
Blockly.Msg.LANG_TEXT_TEXT_IS_STRING_INPUT_THING = 'thing';
Blockly.Msg.LANG_TEXT_TEXT_IS_STRING_TOOLTIP = 'Returns true if <code>thing</code> is a string.';
Blockly.Msg.LANG_TEXT_REVERSE_HELPURL = '/reference/blocks/text.html#reverse';
Blockly.Msg.LANG_TEXT_REVERSE_INPUT = 'reverse';
Blockly.Msg.LANG_TEXT_REVERSE_TOOLTIP = 'Reverse the given text.';
// Lists Blocks.
Blockly.Msg.LANG_CATEGORY_LISTS = 'Lists';
//Blockly.Msg.LANG_LISTS_CREATE_EMPTY_HELPURL = 'http://en.wikipedia.org/wiki/Linked_list.html#Empty_lists';
......
......@@ -1813,7 +1813,10 @@ Blockly.Versioning.AllUpgradeMaps =
28: "noUpgrade",
// AI2: Added "for each in dictionary" block.
29: "noUpgrade"
29: "noUpgrade",
// AI2: In BLOCKS_LANGUAGE_VERSION 30, The Reverse Text block was added
30: "noUpgrade"
}, // End Language upgraders
......
......@@ -1872,6 +1872,21 @@
(define (string-to-lower-case s)
(String:toLowerCase (s:toString)))
(define (unicode-string->list str :: <string>) :: <list>
(let loop ((result :: <list> '()) (i :: <int> (string-length str)))
(set! i (- i 1))
(if (< i 0) result
(if (and (>= i 1)
(let ((c (string-ref str i))
(c1 (string-ref str (- i 1))))
(and (char>=? c #\xD800) (char<=? c #\xDFFF)
(char>=? c1 #\xD800) (char<=? c1 #\xDFFF))))
(loop (make <pair> (string-ref str i) (make <pair> (string-ref str (- i 1)) result)) (- i 1))
(loop (make <pair> (string-ref str i) result) i)))))
(define (string-reverse s)
(list->string (reverse (unicode-string->list s))))
;;; returns a string that is the number formatted with a
;;; specified number of decimal places
(define (format-as-decimal number places)
......
......@@ -77,6 +77,12 @@ public class YailEvalTest extends TestCase {
scheme.eval(thunkify(schemeString)).toString());
}
public void testStringReverse() throws Throwable {
assertEquals("raboof", (scheme.eval("(string-reverse \"foobar\")")).toString());
assertEquals("\uD83D\uDE43\uD83D\uDE0F\uD83E\uDD29\uD83D\uDE02\uD83D\uDC4F\uD83D\uDE03\uD83D\uDC4D\uD83D\uDE18",
(scheme.eval("(string-reverse \"\uD83D\uDE18\uD83D\uDC4D\uD83D\uDE03\uD83D\uDC4F\uD83D\uDE02\uD83E\uDD29\uD83D\uDE0F\uD83D\uDE43\")")).toString());
}
/**
* This is mostly here as a workaround for some strange behavior with top-level try-catch
* expressions in Kawa.
......
......@@ -499,8 +499,10 @@ public class YaVersion {
// - FORM_COMPONENT_VERSION was incremented to 27.
// For YOUNG_ANDROID_VERSION 201:
// - CANVAS_COMPONENT_VERSION was incremented to 13
// For YOUNG_ANDROID_VERSION 202:
// - BLOCKS_LANGUAGE_VERSION was incremented to 30
public static final int YOUNG_ANDROID_VERSION = 201;
public static final int YOUNG_ANDROID_VERSION = 202;
// ............................... Blocks Language Version Number ...............................
......@@ -576,8 +578,10 @@ public class YaVersion {
// - The dictionaries blocks were added.
// For BLOCKS_LANGUAGE_VERSION 29
// - The for-each-in-dictionary block was added.
// For BLOCKS_LANGUAGE_VERSION 30:
// - The Reverse Text block was added
public static final int BLOCKS_LANGUAGE_VERSION = 29;
public static final int BLOCKS_LANGUAGE_VERSION = 30;
// ................................. Target SDK Version Number ..................................
......
......@@ -145,6 +145,7 @@
<li><a href="#replaceall">replace all</a></li>
<li><a href="#obfuscatetext">obfuscated text</a></li>
<li><a href="#isstring">is a string?</a></li>
<li><a href="#reverse">reverse</a></li>
</ul>
<h3 id="string">” “ (string block)</h3>
......@@ -273,6 +274,12 @@
<p>Returns true if <em>thing</em> is a text object, otherwise false.</p>
<h3 id="reverse">reverse</h3>
<p><img src="images/text/text_reverse.png" alt="" /></p>
<p>Reverse the given text. For example, “reverse” would become “esrever”.</p>
</article>
<script>
// Handle redirection to documentation based on locale query parameter (if specified)
......
......@@ -22,6 +22,7 @@ layout: documentation
* [replace all](#replaceall)
* [obfuscated text](#obfuscatetext)
* [is a string?](#isstring)
* [reverse](#reverse)
### " " (string block) {#string}
......@@ -148,3 +149,9 @@ Produces text, like a text block. The difference is that th etext is not easily
![](images/text/isstring.png)
Returns true if *thing* is a text object, otherwise false.
### reverse {#reverse}
![](images/text/text_reverse.png)
Reverse the given text. For example, "reverse" would become "esrever".
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment