September Morn Update

This commit is contained in:
Ingy döt Net 2019-09-12 10:33:56 -07:00
parent 4e2d22a71d
commit aac6731f2c
6856 changed files with 141342 additions and 21127 deletions

View file

@ -0,0 +1 @@
--- {}

View file

@ -0,0 +1,83 @@
/* ARM assembly Raspberry PI */
/* program strEmpty.s */
/* Constantes */
.equ STDOUT, 1 @ Linux output console
.equ EXIT, 1 @ Linux syscall
.equ WRITE, 4 @ Linux syscall
/* Initialized data */
.data
szNotEmptyString: .asciz "String is not empty. \n"
szEmptyString: .asciz "String is empty. \n"
@ empty string
szString: .asciz "" @ with zero final
szString1: .asciz "A" @ with zero final
/* UnInitialized data */
.bss
/* code section */
.text
.global main
main: /* entry of program */
push {fp,lr} /* saves 2 registers */
@ load string
ldr r1,iAdrszString
ldrb r0,[r1] @ load first byte of string
cmp r0,#0 @ compar with zero ?
bne 1f
ldr r0,iAdrszEmptyString
bl affichageMess
b 2f
1:
ldr r0,iAdrszNotEmptyString
bl affichageMess
/* second string */
2:
@ load string 1
ldr r1,iAdrszString1
ldrb r0,[r1] @ load first byte of string
cmp r0,#0 @ compar with zero ?
bne 3f
ldr r0,iAdrszEmptyString
bl affichageMess
b 100f
3:
ldr r0,iAdrszNotEmptyString
bl affichageMess
b 100f
100: /* standard end of the program */
mov r0, #0 @ return code
pop {fp,lr} @restaur 2 registers
mov r7, #EXIT @ request to exit program
swi 0 @ perform the system call
iAdrszString: .int szString
iAdrszString1: .int szString1
iAdrszNotEmptyString: .int szNotEmptyString
iAdrszEmptyString: .int szEmptyString
/******************************************************************/
/* display text with size calculation */
/******************************************************************/
/* r0 contains the address of the message */
affichageMess:
push {fp,lr} /* save registres */
push {r0,r1,r2,r7} /* save others registers */
mov r2,#0 /* counter length */
1: /* loop length calculation */
ldrb r1,[r0,r2] /* read octet start position + index */
cmp r1,#0 /* if 0 its over */
addne r2,r2,#1 /* else add 1 in the length */
bne 1b /* and loop */
/* so here r2 contains the length of the message */
mov r1,r0 /* address message in r1 */
mov r0,#STDOUT /* code to write to the standard output Linux */
mov r7, #WRITE /* code call system "write" */
swi #0 /* call systeme */
pop {r0,r1,r2,r7} /* restaur others registers */
pop {fp,lr} /* restaur des 2 registres */
bx lr /* return */

View file

@ -0,0 +1,3 @@
10 LET A$=""
20 IF A$="" THEN PRINT "The string is empty."
30 IF A$<>"" THEN PRINT "The string is not empty."

View file

@ -0,0 +1,113 @@
using System;
using System.Collections.Generic;
static class Program
{
// TL; DR:
public static void Foo()
{
string s;
// Assign empty string:
s = "";
// or
s = string.Empty;
// Check for empty string only (false if s is null):
if (s != null && s.Length == 0) { }
// Check for null or empty (most idiomatic .NET way):
if (string.IsNullOrEmpty(s)) { }
}
public static void Main()
{
// Equality is somewhat convoluted in .NET.
// The means above are the author's recommendation for each case.
// s is initialized to null. It is a variable of the System.String type that is a null reference and is not
// the empty string.
string s = null;
// Alias Console.WriteLine with a shorter name to make the demonstration code less verbose.
void P(bool x) => Console.WriteLine(x);
// Assign the empty string literal to s.
s = "";
// ' Assign String.Empty to s.
s = string.Empty;
// The empty string literal is the same object reference as String.Empty because of string interning, meaning the
// behavior of the two is identical.
// From this point on, "" will be used instead of String.Empty for brevity.
//#== operator (object)
// The == operator tests for reference equality when overload resolution fails to find an operator defined by
// either operand type. However, which strings are interned is a CLR implementation detail and may be unreliable
// when comparing non-empty strings. The equivalent in VB.NET would be s Is "".
// Note that there is no such operator as Object.op_Equality(Object, Object): the use of the == operator for
// types of type Object is a C# language feature.
P((object)s == "");
//#Object.ReferenceEquals(Object, Object)
// The previous line is semantically to the following, though it does not involve a method call.
P(object.ReferenceEquals(s, ""));
//#String.op_Equality(String, String)
// The equality operator of System.String is implemented as a call to String.Equals(String). Operators cannot be
// called with method syntax in C#.
P(s == "");
//#String.Equals(String, String)
// Call the static method defined on the String type, which first calls Object.ReferenceEquals and then, after
// verifying that both are strings of the same length, compares the strings character-by-character.
P(string.Equals(s, ""));
//#Object.Equals(Object, Object)
// First checks for reference equality and whether one or both of the arguments is null. It then invokes the
// instance Equals method of the left parameter.
P(object.Equals(s, ""));
//#String.Equals(String)
// The method is called with the string literal as the receiver because a NullReferenceException is thrown if s
// is null.
P("".Equals(s));
//#String.Length
// Check the Length property. The ?. (null-conditional) operator is used to avoid NullReferenceException. The Equals
// call above can also be done this way. Null propagation makes the equality operator return false if one operand
// is a Nullable<T> and does not have a value, making this result in false when s is null.
P(s?.Length == 0);
//#String.Length
// A more traditional version of the null-conditional using a guard clause.
// Both the null-conditional and this are noticeably (~4 times) faster than "".Equals(s). In general, it appears that
// for empty strings, using the length is faster than using an equality comparison.
P(s != null && s.Length == 0);
//#String.IsNullOrEmpty(String)
// Note that all of the other methods give false for null.
// A static method of System.String that returns true if the string is null or its length is zero.
P(string.IsNullOrEmpty(s));
//#System.Collections.Generic.EqualityComparer(Of String).Default.Equals(String, String)
// The EqualityComparer(Of T) class provides default implementations when an IEqualityComparer(Of T) is required.
// The implementation for String calls String.Equals(String).
P(EqualityComparer<string>.Default.Equals(s, ""));
Console.WriteLine();
// Each of the means described above, except testing for a non-empty string.
P((object)s != "");
P(!object.ReferenceEquals(s, ""));
P(s != "");
P(!string.Equals(s, ""));
P(!object.Equals(s, ""));
P(!"".Equals(s));
P(s?.Length != 0); // Still false when s is null!
P(s == null || s.Length != 0);
P(!string.IsNullOrEmpty(s));
P(!EqualityComparer<string>.Default.Equals(s, ""));
}
}

View file

@ -1,11 +1,12 @@
import extensions.
import extensions;
program = [
var s := emptyLiteralValue.
public program()
{
auto s := emptyString;
if (s isEmpty)
[ console printLine("'", s, "' is empty") ].
if (s.isEmpty())
{ console.printLine("'", s, "' is empty") };
if (s isNonempty)
[ console printLine("'", s, "' is not empty") ]
].
if (s.isNonempty())
{ console.printLine("'", s, "' is not empty") }
}

View file

@ -0,0 +1 @@
s := '';

View file

@ -0,0 +1,2 @@
s = ''
length(s) = 0

View file

@ -0,0 +1,3 @@
s <> ''
length(s) > 0
longBool(length(s))

View file

@ -1,4 +1,7 @@
str = "" -- create empty string
-- create an empty string 3 different ways
str = ""
str = ''
str = [[]]
-- test for empty string
if str == "" then
@ -9,3 +12,14 @@ end
if str ~= "" then
print "The string is not empty"
end
-- several different ways to check the string's length
if string.len(str) == 0 then
print "The library function says the string is empty."
end
if str:len() == 0 then
print "The method call says the string is empty."
end
if #str == 0 then
print "The unary operator says the string is empty."
end

View file

@ -0,0 +1,5 @@
S = "", % assignment
( if S = "" then ... else ... ), % checking if a string is empty
( if not S = "" then ... else ... ), % checking if a string is not empty

View file

@ -0,0 +1,14 @@
Dim s As String
' Assign empty string:
s = ""
' or
s = String.Empty
' Check for empty string only (false if s is null):
If s IsNot Nothing AndAlso s.Length = 0 Then
End If
' Check for null or empty (most idiomatic .NET way):
If String.IsNullOrEmpty(s) Then
End If

View file

@ -0,0 +1,130 @@
Option Strict On
Module Program
Sub Main()
' Equality is somewhat convoluted in .NET, and VB doesn't help by adding legacy means of comparison.
' The means above are the author's recommendation for each case.
' Some methods also return true if the string is Nothing/null; this is noted in the description for those that
' do.
' s is initialized to Nothing. It is a variable of the System.String type that is a null reference and is not
' the empty string.
Dim s As String = Nothing
' Alias Console.WriteLine with a shorter name to make the demonstration code less verbose.
Dim P = Sub(x As Boolean) Console.WriteLine(x)
' Assign the empty string literal to s.
s = ""
'' Assign String.Empty to s.
s = String.Empty
' The empty string literal is the same object reference as String.Empty because of string interning, meaning the
' behavior of the two is identical.
' From this point on, "" will be used instead of String.Empty for brevity.
'#Is operator
' The Is operator tests for reference equality. However, which strings are interned is a CLR implementation
' detail and may be unreliable when comparing non-empty strings. The equivalent in C# would be (object)s == "".
' Note that there is no such operator as Object.op_Equality(Object, Object): the use of the == operator for
' types of type Object is a C# language feature.
P(s Is "")
'#Object.ReferenceEquals(Object, Object)
' The previous line is semantically to the following, though it does not involve a method call.
P(Object.ReferenceEquals(s, ""))
'#= Operator
'True for Nothing.
' The VB.NET compiler does not use the System.String implementation of the equality operator. Instead, it emits
' a call to a method in the Visual Basic runtime, Operators.CompareString, which checks for reference equality
' before calling System.String.CompareOrdinal(String, String), which checks again for reference equality before
' comparing character-by-character.
P(s = "")
'#Microsoft.VisualBasic.CompilerServices.Operators.CompareString(String, String, Boolean)
'True for Nothing.
' Equivalent to the above line, though methods in the CompilerServices namespace are not meant for use by
' regular code.
' The third argument indicates whether to use a textual comparison (e.g. ignore case and diacritics).
P(0 = CompilerServices.Operators.CompareString(s, "", False))
'#Microsoft.VisualBasic.Strings.StrComp(String, String, [CompareMethod])
'True for Nothing.
' A wrapper around CompareString that is intended for use.
P(0 = StrComp(s, ""))
'#String.op_Equality(String, String)
' It is possible to directly call the equality operator of System.String, which is implemented as a call to
' String.Equals(String).
P(String.op_Equality(s, ""))
'#String.Equals(String, String)
' Call the static method defined on the String type.
' first calls Object.ReferenceEquals and then, after verifying that both are strings of the same length,
' compares the strings character-by-character.
P(String.Equals(s, ""))
'#Object.Equals(Object, Object)
' First checks for reference equality and whether one or both of the arguments is Nothing. It then invokes the
' instance Equals method of the left parameter.
P(Object.Equals(s, ""))
'#String.Equals(String)
' The method is called with the string literal as the receiver because a NullReferenceException is thrown if s
' is Nothing.
P("".Equals(s))
'#Microsoft.VisualBasic.Strings.Len(String)
'True for Nothing.
' Check the length using Microsoft.VisualBasic.Strings.Len(String). This method returns s?.Length (see below).
P(0 = Len(s))
'#String.Length
' Check the Length property. The ?. (null-conditional) operator is used to avoid NullReferenceException. The Equals
' call above can also be done this way.
' A method call must be added because the equality operator propagates Nothing/null (that is, the result of the
' expression is Nullable(Of Boolean)). This has the side effect of making it behave "correctly" for null.
P((s?.Length = 0).GetValueOrDefault())
' The If statement automatically unwraps nullable Booleans, however.
If s?.Length = 0 Then
End If
'#String.Length
' A more traditional version of the null-conditional using a guard clause.
' Both the null-conditional and this are noticeably (~4 times) faster than "".Equals(s). In general, it appears that
' for empty strings, using the length is faster than using an equality comparison.
P(s IsNot Nothing AndAlso s.Length = 0)
'#String.IsNullOrEmpty(String)
'True for Nothing
' A static method of System.String that returns true if the string is Nothing or its length is zero.
P(String.IsNullOrEmpty(s))
'#System.Collections.Generic.EqualityComparer(Of String).Default.Equals(String, String)
' The EqualityComparer(Of T) class provides default implementations when an IEqualityComparer(Of T) is required.
' The implementation for String calls String.Equals(String).
P(EqualityComparer(Of String).Default.Equals(s, ""))
Console.WriteLine()
' Each of the means described above, except testing for a non-empty string.
P(s IsNot "")
P(Not Object.ReferenceEquals(s, ""))
P(s <> "")
P(0 <> CompilerServices.Operators.CompareString(s, "", False))
P(0 <> StrComp(s, ""))
P(String.op_Inequality(s, ""))
P(Not String.Equals(s, ""))
P(Not Object.Equals(s, ""))
P(Not "".Equals(s))
P(Len(s) <> 0)
P((s?.Length <> 0).GetValueOrDefault())
P(s Is Nothing OrElse s.Length <> 0)
P(Not String.IsNullOrEmpty(s))
P(Not EqualityComparer(Of String).Default.Equals(s, ""))
End Sub
End Module