June 2018 Update
This commit is contained in:
parent
ba8067c3b7
commit
22f33d4004
5278 changed files with 84726 additions and 14379 deletions
118
Task/Binary-strings/Java/binary-strings-1.java
Normal file
118
Task/Binary-strings/Java/binary-strings-1.java
Normal file
|
|
@ -0,0 +1,118 @@
|
|||
import java.io.ByteArrayOutputStream;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Arrays;
|
||||
|
||||
public class MutableByteString {
|
||||
|
||||
private byte[] bytes;
|
||||
private int length;
|
||||
|
||||
public MutableByteString(byte... bytes) {
|
||||
setInternal(bytes);
|
||||
}
|
||||
|
||||
public int length() {
|
||||
return length;
|
||||
}
|
||||
|
||||
public boolean isEmpty() {
|
||||
return length == 0;
|
||||
}
|
||||
|
||||
public byte get(int index) {
|
||||
return bytes[check(index)];
|
||||
}
|
||||
|
||||
public void set(byte[] bytes) {
|
||||
setInternal(bytes);
|
||||
}
|
||||
|
||||
public void set(int index, byte b) {
|
||||
bytes[check(index)] = b;
|
||||
}
|
||||
|
||||
public void append(byte b) {
|
||||
if (length >= bytes.length) {
|
||||
int len = 2 * bytes.length;
|
||||
if (len < 0)
|
||||
len = Integer.MAX_VALUE;
|
||||
bytes = Arrays.copyOf(bytes, len);
|
||||
}
|
||||
bytes[length] = b;
|
||||
length++;
|
||||
}
|
||||
|
||||
public MutableByteString substring(int from, int to) {
|
||||
return new MutableByteString(Arrays.copyOfRange(bytes, from, to));
|
||||
}
|
||||
|
||||
public void replace(byte[] from, byte[] to) {
|
||||
ByteArrayOutputStream copy = new ByteArrayOutputStream();
|
||||
if (from.length == 0) {
|
||||
for (byte b : bytes) {
|
||||
copy.write(to, 0, to.length);
|
||||
copy.write(b);
|
||||
}
|
||||
copy.write(to, 0, to.length);
|
||||
} else {
|
||||
for (int i = 0; i < length; i++) {
|
||||
if (regionEquals(i, from)) {
|
||||
copy.write(to, 0, to.length);
|
||||
i += from.length - 1;
|
||||
} else {
|
||||
copy.write(bytes[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
set(copy.toByteArray());
|
||||
}
|
||||
|
||||
public boolean regionEquals(int offset, MutableByteString other, int otherOffset, int len) {
|
||||
if (Math.max(offset, otherOffset) + len < 0)
|
||||
return false;
|
||||
if (offset + len > length || otherOffset + len > other.length())
|
||||
return false;
|
||||
for (int i = 0; i < len; i++) {
|
||||
if (bytes[offset + i] != other.get(otherOffset + i))
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public String toHexString() {
|
||||
char[] hex = new char[2 * length];
|
||||
for (int i = 0; i < length; i++) {
|
||||
hex[2 * i] = "0123456789abcdef".charAt(bytes[i] >> 4 & 0x0F);
|
||||
hex[2 * i + 1] = "0123456789abcdef".charAt(bytes[i] & 0x0F);
|
||||
}
|
||||
return new String(hex);
|
||||
}
|
||||
|
||||
public String toStringUtf8() {
|
||||
return new String(bytes, 0, length, StandardCharsets.UTF_8);
|
||||
}
|
||||
|
||||
private void setInternal(byte[] bytes) {
|
||||
this.bytes = bytes.clone();
|
||||
this.length = bytes.length;
|
||||
}
|
||||
|
||||
private boolean regionEquals(int offset, byte[] other) {
|
||||
int len = other.length;
|
||||
if (offset < 0 || offset + len < 0)
|
||||
return false;
|
||||
if (offset + len > length)
|
||||
return false;
|
||||
for (int i = 0; i < len; i++) {
|
||||
if (bytes[offset + i] != other[i])
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private int check(int index) {
|
||||
if (index < 0 || index >= length)
|
||||
throw new IndexOutOfBoundsException(String.valueOf(index));
|
||||
return index;
|
||||
}
|
||||
}
|
||||
60
Task/Binary-strings/Java/binary-strings-2.java
Normal file
60
Task/Binary-strings/Java/binary-strings-2.java
Normal file
|
|
@ -0,0 +1,60 @@
|
|||
import static org.hamcrest.CoreMatchers.is;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
public class MutableByteStringTest {
|
||||
|
||||
@Test
|
||||
public void testReplaceEmpty() {
|
||||
MutableByteString str = new MutableByteString("hello".getBytes(StandardCharsets.UTF_8));
|
||||
str.replace(new byte[]{}, new byte[]{'-'});
|
||||
|
||||
Assert.assertThat(str.toStringUtf8(), is("-h-e-l-l-o-"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testReplaceMultiple() {
|
||||
MutableByteString str = new MutableByteString("hello".getBytes(StandardCharsets.UTF_8));
|
||||
str.replace(new byte[]{'l'}, new byte[]{'1', '2', '3'});
|
||||
|
||||
Assert.assertThat(str.toStringUtf8(), is("he123123o"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testToHexString() {
|
||||
MutableByteString str = new MutableByteString("hello".getBytes(StandardCharsets.UTF_8));
|
||||
|
||||
Assert.assertThat(str.toHexString(), is("68656c6c6f"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAppend() {
|
||||
MutableByteString str = new MutableByteString("hello".getBytes(StandardCharsets.UTF_8));
|
||||
str.append((byte) ',');
|
||||
str.append((byte) ' ');
|
||||
str.append((byte) 'w');
|
||||
str.append((byte) 'o');
|
||||
str.append((byte) 'r');
|
||||
str.append((byte) 'l');
|
||||
str.append((byte) 'd');
|
||||
|
||||
Assert.assertThat(str.toStringUtf8(), is("hello, world"));
|
||||
}
|
||||
@Test
|
||||
public void testSubstring() {
|
||||
MutableByteString str = new MutableByteString("hello, world".getBytes(StandardCharsets.UTF_8));
|
||||
|
||||
Assert.assertThat(str.substring(0, 5).toStringUtf8(), is("hello"));
|
||||
Assert.assertThat(str.substring(7, 12).toStringUtf8(), is("world"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRegionEquals() {
|
||||
MutableByteString str = new MutableByteString("hello".getBytes(StandardCharsets.UTF_8));
|
||||
|
||||
Assert.assertThat(str.regionEquals(0, new MutableByteString(new byte[]{'h'}), 0, 1), is(true));
|
||||
Assert.assertThat(str.regionEquals(0, new MutableByteString(new byte[]{'h'}), 0, 2), is(false));
|
||||
}
|
||||
}
|
||||
60
Task/Binary-strings/Lingo/binary-strings.lingo
Normal file
60
Task/Binary-strings/Lingo/binary-strings.lingo
Normal file
|
|
@ -0,0 +1,60 @@
|
|||
-- String creation and destruction
|
||||
foo = "Hello world!" -- created by assignment; destruction via garbage collection
|
||||
|
||||
-- Strings are binary safe
|
||||
put numtochar(0) into char 6 of foo
|
||||
put chartonum(foo.char[6])
|
||||
-- 0
|
||||
put str.char[7..foo.length]
|
||||
-- "world!"
|
||||
|
||||
-- String cloning and copying
|
||||
bar = foo -- copies foo contents to bar
|
||||
|
||||
-- String comparison
|
||||
put (foo=bar) -- TRUE
|
||||
put (foo<>bar) -- FALSE
|
||||
|
||||
-- Check if a string is empty
|
||||
put (foo=EMPTY)
|
||||
put (foo="")
|
||||
put (foo.length=0)
|
||||
|
||||
-- Append a byte to a string
|
||||
put "X" after foo
|
||||
put chartonum(88) after foo
|
||||
|
||||
-- Extract a substring from a string
|
||||
put foo.char[3..5]
|
||||
|
||||
-- Replace every occurrence of a byte (or a string) in a string with another string
|
||||
|
||||
----------------------------------------
|
||||
-- Replace in string
|
||||
-- @param {string} stringToFind
|
||||
-- @param {string} stringToInsert
|
||||
-- @param {string} input
|
||||
-- @return {string}
|
||||
----------------------------------------
|
||||
on replaceAll (stringToFind, stringToInsert, input)
|
||||
output = ""
|
||||
findLen = stringToFind.length - 1
|
||||
repeat while TRUE
|
||||
currOffset = offset(stringToFind, input)
|
||||
if currOffset=0 then exit repeat
|
||||
put input.char[1..currOffset] after output
|
||||
delete the last char of output
|
||||
put stringToInsert after output
|
||||
delete input.char[1..(currOffset + findLen)]
|
||||
end repeat
|
||||
put input after output
|
||||
return output
|
||||
end
|
||||
|
||||
put replaceAll("o", "X", foo)
|
||||
|
||||
-- Join strings (4x the same result)
|
||||
foo = "Hello " & "world!"
|
||||
foo = "Hello" & numtochar(32) & "world!"
|
||||
foo = "Hello" & SPACE & "world!"
|
||||
foo = "Hello" && "world!"
|
||||
|
|
@ -41,7 +41,7 @@ class ByteStr {
|
|||
}
|
||||
|
||||
# A couple of operators for our new type:
|
||||
multi infix:<cmp>(ByteStr $x, ByteStr $y) { $x.bytes cmp $y.bytes }
|
||||
multi infix:<cmp>(ByteStr $x, ByteStr $y) { $x.bytes.join cmp $y.bytes.join }
|
||||
multi infix:<~> (ByteStr $x, ByteStr $y) { ByteStr.new(:bytes(|$x.bytes, |$y.bytes)) }
|
||||
|
||||
# create some byte strings (destruction not needed due to garbage collection)
|
||||
|
|
|
|||
18
Task/Binary-strings/Red/binary-strings.red
Normal file
18
Task/Binary-strings/Red/binary-strings.red
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
Red []
|
||||
s: copy "abc" ;; string creation
|
||||
|
||||
s: none ;; destruction
|
||||
t: "Abc"
|
||||
if t == "abc" [print "equal case"] ;; comparison , case sensitive
|
||||
if t = "abc" [print "equal (case insensitive)"] ;; comparison , case insensitive
|
||||
s: copy "" ;; copying string
|
||||
if empty? s [print "string is empty "] ;; check if string is empty
|
||||
append s #"a" ;; append byte
|
||||
substr: copy/part at "1234" 3 2 ;; ~ substr ("1234" ,3,2) , red has 1 based indices !
|
||||
?? substr
|
||||
s: replace/all "abcabcabc" "bc" "x" ;; replace all "bc" by "x"
|
||||
?? s
|
||||
s: append "hello " "world" ;; join 2 strings
|
||||
?? s
|
||||
s: rejoin ["hello " "world" " !"] ;; join multiple strings
|
||||
?? s
|
||||
|
|
@ -1,72 +1,71 @@
|
|||
use std::str;
|
||||
|
||||
fn main() {
|
||||
//Create new string
|
||||
let str = String::from("Hello world!");
|
||||
println!("{}", str);
|
||||
assert!(str == "Hello world!", "Incorrect string text");
|
||||
// Create new string
|
||||
let string = String::from("Hello world!");
|
||||
println!("{}", string);
|
||||
assert_eq!(string, "Hello world!", "Incorrect string text");
|
||||
|
||||
//Create and assign value to string
|
||||
// Create and assign value to string
|
||||
let mut assigned_str = String::new();
|
||||
assert!(assigned_str == "", "Incorrect string creation");
|
||||
assigned_str.push_str("Text has been assigned!");
|
||||
assert_eq!(assigned_str, "", "Incorrect string creation");
|
||||
assigned_str += "Text has been assigned!";
|
||||
println!("{}", assigned_str);
|
||||
assert!(assigned_str == "Text has been assigned!","Incorrect string text");
|
||||
assert_eq!(assigned_str, "Text has been assigned!","Incorrect string text");
|
||||
|
||||
//String comparison, compared lexicographically byte-wise
|
||||
//same as the asserts above
|
||||
if str == "Hello world!" && assigned_str == "Text has been assigned!" {
|
||||
// String comparison, compared lexicographically byte-wise same as the asserts above
|
||||
if string == "Hello world!" && assigned_str == "Text has been assigned!" {
|
||||
println!("Strings are equal");
|
||||
}
|
||||
|
||||
//Cloning -> str can still be used after cloning
|
||||
let clone_str = str.clone();
|
||||
println!("String is:{} and Clone string is: {}", str, clone_str);
|
||||
assert!(clone_str == str, "Incorrect string creation");
|
||||
// Cloning -> string can still be used after cloning
|
||||
let clone_str = string.clone();
|
||||
println!("String is:{} and Clone string is: {}", string, clone_str);
|
||||
assert_eq!(clone_str, string, "Incorrect string creation");
|
||||
|
||||
//Copying, str won't be usable anymore, accessing it will cause compiler failure
|
||||
let copy_str = str;
|
||||
// Copying, string won't be usable anymore, accessing it will cause compiler failure
|
||||
let copy_str = string;
|
||||
println!("String copied now: {}", copy_str);
|
||||
|
||||
//Check if string is empty
|
||||
// Check if string is empty
|
||||
let empty_str = String::new();
|
||||
assert!(empty_str.is_empty(), "Error, string should be empty");
|
||||
|
||||
//Append byte, Rust strings are a stream of UTF-8 bytes
|
||||
let byte_vec = vec![65]; //contains A
|
||||
// Append byte, Rust strings are a stream of UTF-8 bytes
|
||||
let byte_vec = [65]; // contains A
|
||||
let byte_str = str::from_utf8(&byte_vec).unwrap();
|
||||
assert!(byte_str == "A", "Incorrect byte append");
|
||||
assert_eq!(byte_str, "A", "Incorrect byte append");
|
||||
|
||||
//Substrings can be accessed through slices
|
||||
// Substrings can be accessed through slices
|
||||
let test_str = "Blah String";
|
||||
let mut sub_str = &test_str[0..11];
|
||||
assert!(sub_str == "Blah String", "Error in slicing");
|
||||
assert_eq!(sub_str, "Blah String", "Error in slicing");
|
||||
sub_str = &test_str[1..5];
|
||||
assert!(sub_str == "lah ", "Error in slicing");
|
||||
assert_eq!(sub_str, "lah ", "Error in slicing");
|
||||
sub_str = &test_str[3..];
|
||||
assert!(sub_str == "h String", "Error in slicing");
|
||||
assert_eq!(sub_str, "h String", "Error in slicing");
|
||||
sub_str = &test_str[..2];
|
||||
assert!(sub_str == "Bl", "Error in slicing");
|
||||
assert_eq!(sub_str, "Bl", "Error in slicing");
|
||||
|
||||
//String replace, note string is immutable
|
||||
// String replace, note string is immutable
|
||||
let org_str = "Hello";
|
||||
assert!( org_str.replace("l", "a") == "Heaao", "Error in replacement");
|
||||
assert!( org_str.replace("ll", "r") == "Hero", "Error in replacement");
|
||||
assert_eq!(org_str.replace("l", "a"), "Heaao", "Error in replacement");
|
||||
assert_eq!(org_str.replace("ll", "r"), "Hero", "Error in replacement");
|
||||
|
||||
//Joining strings requires a string and an &str or a two string one of which needs an & for coercion
|
||||
// Joining strings requires a `String` and an &str or a two `String`s one of which needs an & for coercion
|
||||
let str1 = "Hi";
|
||||
let str2 = " There";
|
||||
let fin_str = str1.to_string() + str2;
|
||||
assert!( fin_str == "Hi There", "Error in concatenation");
|
||||
assert_eq!(fin_str, "Hi There", "Error in concatenation");
|
||||
|
||||
//Joining strings requires a string and an &str or two strings, one of which needs an & for coercion
|
||||
// Joining strings requires a `String` and an &str or two `Strings`s, one of which needs an & for coercion
|
||||
let str1 = "Hi";
|
||||
let str2 = " There";
|
||||
let fin_str = str1.to_string() + str2;
|
||||
assert!( fin_str == "Hi There", "Error in concatenation");
|
||||
assert_eq!(fin_str, "Hi There", "Error in concatenation");
|
||||
|
||||
//Splits -- note Rust supports passing patterns to splits
|
||||
// Splits -- note Rust supports passing patterns to splits
|
||||
let f_str = "Pooja and Sundar are up in Tumkur";
|
||||
let split_str: Vec<&str> = f_str.split(' ').collect();
|
||||
assert!( split_str == ["Pooja", "and", "Sundar", "are", "up", "in", "Tumkur"], "Error in string split");
|
||||
|
||||
let split_str: Vec<_> = f_str.split(' ').collect();
|
||||
assert_eq!(split_str, ["Pooja", "and", "Sundar", "are", "up", "in", "Tumkur"], "Error in string split");
|
||||
}
|
||||
|
|
|
|||
4
Task/Binary-strings/VBA/binary-strings-1.vba
Normal file
4
Task/Binary-strings/VBA/binary-strings-1.vba
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
'Set the string comparison method to Binary.
|
||||
Option Compare Binary ' That is, "AAA" is less than "aaa".
|
||||
' Set the string comparison method to Text.
|
||||
Option Compare Text ' That is, "AAA" is equal to "aaa".
|
||||
9
Task/Binary-strings/VBA/binary-strings-10.vba
Normal file
9
Task/Binary-strings/VBA/binary-strings-10.vba
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
Sub Join_Strings()
|
||||
Dim A$, B As String
|
||||
|
||||
A = "Hello"
|
||||
B = "world"
|
||||
Debug.Print A & " " & B 'return : Hello world
|
||||
'If you're sure that A and B are Strings, you can use + instead of & :
|
||||
Debug.Print A + " " + B 'return : Hello world
|
||||
End Sub
|
||||
5
Task/Binary-strings/VBA/binary-strings-2.vba
Normal file
5
Task/Binary-strings/VBA/binary-strings-2.vba
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
Sub Creation_String_FirstWay()
|
||||
Dim myString As String
|
||||
'Here, myString is created and equal ""
|
||||
|
||||
End Sub '==> Here the string is destructed !
|
||||
9
Task/Binary-strings/VBA/binary-strings-3.vba
Normal file
9
Task/Binary-strings/VBA/binary-strings-3.vba
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
Sub String_Assignment()
|
||||
Dim myString$
|
||||
'Here, myString is created and equal ""
|
||||
|
||||
'assignments :
|
||||
myString = vbNullString 'return : ""
|
||||
myString = "Hello World" 'return : "Hello World"
|
||||
myString = String(12, "A") 'return : "AAAAAAAAAAAA"
|
||||
End Sub
|
||||
28
Task/Binary-strings/VBA/binary-strings-4.vba
Normal file
28
Task/Binary-strings/VBA/binary-strings-4.vba
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
Sub String_Comparison_FirstWay()
|
||||
Dim A$, B$, C$
|
||||
|
||||
If A = B Then Debug.Print "A = B"
|
||||
|
||||
A = "creation": B = "destruction": C = "CREATION"
|
||||
|
||||
'test equality : (operator =)
|
||||
If A = B Then
|
||||
Debug.Print A & " = " & B
|
||||
'used to Sort : (operator < and >)
|
||||
ElseIf A > B Then
|
||||
Debug.Print A & " > " & B
|
||||
Else 'here : A < B
|
||||
Debug.Print A & " < " & B
|
||||
End If
|
||||
|
||||
'test if A is different from C
|
||||
If A <> C Then Debug.Print A & " and " & B & " are differents."
|
||||
'same test without case-sensitive
|
||||
If UCase(A) = UCase(C) Then Debug.Print A & " = " & C & " (no case-sensitive)"
|
||||
|
||||
'operator Like :
|
||||
If A Like "*ation" Then Debug.Print A & " Like *ation"
|
||||
If Not B Like "*ation" Then Debug.Print B & " Not Like *ation"
|
||||
'See Also :
|
||||
'https://docs.microsoft.com/en-us/dotnet/visual-basic/language-reference/operators/like-operator
|
||||
End Sub
|
||||
6
Task/Binary-strings/VBA/binary-strings-5.vba
Normal file
6
Task/Binary-strings/VBA/binary-strings-5.vba
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
Sub String_Clone_Copy()
|
||||
Dim A As String, B$
|
||||
A = "Hello world!"
|
||||
'cloning :
|
||||
B = A
|
||||
End Sub
|
||||
23
Task/Binary-strings/VBA/binary-strings-6.vba
Normal file
23
Task/Binary-strings/VBA/binary-strings-6.vba
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
Sub Check_Is_Empty()
|
||||
Dim A As String, B As Variant
|
||||
|
||||
Debug.Print IsEmpty(A) 'return False
|
||||
Debug.Print IsEmpty(Null) 'return False
|
||||
Debug.Print IsEmpty(B) 'return True ==> B is a Variant
|
||||
|
||||
Debug.Print A = vbNullString 'return True
|
||||
Debug.Print StrPtr(A) 'return 0 (zero)
|
||||
|
||||
'Press the OK button without enter a data in the InputBox :
|
||||
A = InputBox("Enter your own String : ")
|
||||
Debug.Print A = "" 'return True
|
||||
Debug.Print IsEmpty(A) 'return False
|
||||
Debug.Print StrPtr(A) = 0 'return False
|
||||
|
||||
'Press the cancel button (with or without enter a data in the InputBox)
|
||||
A = InputBox("Enter your own String : ")
|
||||
Debug.Print StrPtr(A) = 0 'return True
|
||||
Debug.Print IsEmpty(A) 'return False
|
||||
Debug.Print A = "" 'return True
|
||||
'Note : StrPtr is the only way to know if you cancel the inputbox
|
||||
End Sub
|
||||
5
Task/Binary-strings/VBA/binary-strings-7.vba
Normal file
5
Task/Binary-strings/VBA/binary-strings-7.vba
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
Sub Append_to_string()
|
||||
Dim A As String
|
||||
A = "Hello worl"
|
||||
Debug.Print A & Chr(100) 'return : Hello world
|
||||
End Sub
|
||||
6
Task/Binary-strings/VBA/binary-strings-8.vba
Normal file
6
Task/Binary-strings/VBA/binary-strings-8.vba
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
Sub ExtractFromString()
|
||||
Dim A$, B As String
|
||||
A = "Hello world"
|
||||
B = Mid(A, 3, 8)
|
||||
Debug.Print B 'return : llo worl
|
||||
End Sub
|
||||
7
Task/Binary-strings/VBA/binary-strings-9.vba
Normal file
7
Task/Binary-strings/VBA/binary-strings-9.vba
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
Sub ReplaceInString()
|
||||
Dim A$, B As String, C$
|
||||
A = "Hello world"
|
||||
B = Chr(108) ' "l"
|
||||
C = " "
|
||||
Debug.Print Replace(A, B, C) 'return : He o wor d
|
||||
End Sub
|
||||
Loading…
Add table
Add a link
Reference in a new issue