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,308 @@
/* ARM assembly Raspberry PI */
/* program substring.s */
/* Constantes */
.equ STDOUT, 1 @ Linux output console
.equ EXIT, 1 @ Linux syscall
.equ WRITE, 4 @ Linux syscall
.equ BUFFERSIZE, 100
/* Initialized data */
.data
szMessString: .asciz "Result : "
szString1: .asciz "abcdefghijklmnopqrstuvwxyz"
szStringStart: .asciz "abcdefg"
szCarriageReturn: .asciz "\n"
/* UnInitialized data */
.bss
szSubString: .skip 500 @ buffer result
/* code section */
.text
.global main
main:
ldr r0,iAdrszString1 @ address input string
ldr r1,iAdrszSubString @ address output string
mov r2,#22 @ location
mov r3,#4 @ length
bl subStringNbChar @ starting from n characters in and of m length
ldr r0,iAdrszMessString @ display message
bl affichageMess
ldr r0,iAdrszSubString @ display substring result
bl affichageMess
ldr r0,iAdrszCarriageReturn @ display line return
bl affichageMess
@
ldr r0,iAdrszString1
ldr r1,iAdrszSubString
mov r2,#15 @ location
bl subStringEnd @starting from n characters in, up to the end of the string
ldr r0,iAdrszMessString @ display message
bl affichageMess
ldr r0,iAdrszSubString
bl affichageMess
ldr r0,iAdrszCarriageReturn @ display line return
bl affichageMess
@
ldr r0,iAdrszString1
ldr r1,iAdrszSubString
bl subStringMinus @ whole string minus last character
ldr r0,iAdrszMessString @ display message
bl affichageMess
ldr r0,iAdrszSubString
bl affichageMess
ldr r0,iAdrszCarriageReturn @ display line return
bl affichageMess
@
ldr r0,iAdrszString1
ldr r1,iAdrszSubString
mov r2,#'c' @ start character
mov r3,#5 @ length
bl subStringStChar @starting from a known character within the string and of m length
cmp r0,#-1 @ error ?
beq 2f
ldr r0,iAdrszMessString @ display message
bl affichageMess
ldr r0,iAdrszSubString
bl affichageMess
ldr r0,iAdrszCarriageReturn @ display line return
bl affichageMess
@
2:
ldr r0,iAdrszString1
ldr r1,iAdrszSubString
ldr r2,iAdrszStringStart @ sub string to start
mov r3,#10 @ length
bl subStringStString @ starting from a known substring within the string and of m length
cmp r0,#-1 @ error ?
beq 3f
ldr r0,iAdrszMessString @ display message
bl affichageMess
ldr r0,iAdrszSubString
bl affichageMess
ldr r0,iAdrszCarriageReturn @ display line return
bl affichageMess
3:
100: @ standard end of the program
mov r0, #0 @ return code
mov r7, #EXIT @ request to exit program
svc 0 @ perform system call
iAdrszMessString: .int szMessString
iAdrszString1: .int szString1
iAdrszSubString: .int szSubString
iAdrszStringStart: .int szStringStart
iAdrszCarriageReturn: .int szCarriageReturn
/******************************************************************/
/* sub strings index start number of characters */
/******************************************************************/
/* r0 contains the address of the input string */
/* r1 contains the address of the output string */
/* r2 contains the start index */
/* r3 contains numbers of characters to extract */
/* r0 returns number of characters or -1 if error */
subStringNbChar:
push {r1-r5,lr} @ save registers
mov r4,#0 @ counter byte output string
1:
ldrb r5,[r0,r2] @ load byte string input
cmp r5,#0 @ zero final ?
beq 2f
strb r5,[r1,r4] @ store byte output string
add r2,#1 @ increment counter
add r4,#1
cmp r4,r3 @ end ?
blt 1b @ no -> loop
2:
mov r5,#0
strb r5,[r1,r4] @ load byte string 2
mov r0,r4
100:
pop {r1-r5,lr} @ restaur registers
bx lr @ return
/******************************************************************/
/* sub strings index start at end of string */
/******************************************************************/
/* r0 contains the address of the input string */
/* r1 contains the address of the output string */
/* r2 contains the start index */
/* r0 returns number of characters or -1 if error */
subStringEnd:
push {r1-r5,lr} @ save registers
mov r4,#0 @ counter byte output string
1:
ldrb r5,[r0,r2] @ load byte string 1
cmp r5,#0 @ zero final ?
beq 2f
strb r5,[r1,r4]
add r2,#1
add r4,#1
b 1b @ loop
2:
mov r5,#0
strb r5,[r1,r4] @ load byte string 2
mov r0,r4
100:
pop {r1-r5,lr} @ restaur registers
bx lr
/******************************************************************/
/* whole string minus last character */
/******************************************************************/
/* r0 contains the address of the input string */
/* r1 contains the address of the output string */
/* r0 returns number of characters or -1 if error */
subStringMinus:
push {r1-r5,lr} @ save registers
mov r2,#0 @ counter byte input string
mov r4,#0 @ counter byte output string
1:
ldrb r5,[r0,r2] @ load byte string
cmp r5,#0 @ zero final ?
beq 2f
strb r5,[r1,r4]
add r2,#1
add r4,#1
b 1b @ loop
2:
sub r4,#1
mov r5,#0
strb r5,[r1,r4] @ load byte string 2
mov r0,r4
100:
pop {r1-r5,lr} @ restaur registers
bx lr
/******************************************************************/
/* starting from a known character within the string and of m length */
/******************************************************************/
/* r0 contains the address of the input string */
/* r1 contains the address of the output string */
/* r2 contains the character */
/* r3 contains the length
/* r0 returns number of characters or -1 if error */
subStringStChar:
push {r1-r5,lr} @ save registers
mov r6,#0 @ counter byte input string
mov r4,#0 @ counter byte output string
1:
ldrb r5,[r0,r6] @ load byte string
cmp r5,#0 @ zero final ?
streqb r5,[r1,r4]
moveq r0,#-1
beq 100f
cmp r5,r2
beq 2f
add r6,#1
b 1b @ loop
2:
strb r5,[r1,r4]
add r6,#1
add r4,#1
cmp r4,r3
bge 3f
ldrb r5,[r0,r6] @ load byte string
cmp r5,#0
bne 2b
3:
mov r5,#0
strb r5,[r1,r4] @ load byte string 2
mov r0,r4
100:
pop {r1-r5,lr} @ restaur registers
bx lr
/******************************************************************/
/* starting from a known substring within the string and of m length */
/******************************************************************/
/* r0 contains the address of the input string */
/* r1 contains the address of the output string */
/* r2 contains the address of string to start */
/* r3 contains the length
/* r0 returns number of characters or -1 if error */
subStringStString:
push {r1-r8,lr} @ save registers
mov r7,r0 @ save address
mov r8,r1 @ counter byte string
mov r1,r2
bl searchSubString
cmp r0,#-1
beq 100f
mov r6,r0 @ counter byte input string
mov r4,#0
1:
ldrb r5,[r7,r6] @ load byte string
strb r5,[r8,r4]
cmp r5,#0 @ zero final ?
moveq r0,r4
beq 100f
add r4,#1
cmp r4,r3
addlt r6,#1
blt 1b @ loop
mov r5,#0
strb r5,[r8,r4]
mov r0,r4
100:
pop {r1-r8,lr} @ restaur registers
bx lr
/******************************************************************/
/* search a substring in the string */
/******************************************************************/
/* r0 contains the address of the input string */
/* r1 contains the address of substring */
/* r0 returns index of substring in string or -1 if not found */
searchSubString:
push {r1-r6,lr} @ save registers
mov r2,#0 @ counter byte input string
mov r3,#0 @ counter byte string
mov r6,#-1 @ index found
ldrb r4,[r1,r3]
1:
ldrb r5,[r0,r2] @ load byte string
cmp r5,#0 @ zero final ?
moveq r0,#-1 @ yes returns error
beq 100f
cmp r5,r4 @ compare character
beq 2f
mov r6,#-1 @ no equals - > raz index
mov r3,#0 @ and raz counter byte
add r2,#1 @ and increment counter byte
b 1b @ and loop
2: @ characters equals
cmp r6,#-1 @ first characters equals ?
moveq r6,r2 @ yes -> index begin in r6
add r3,#1 @ increment counter substring
ldrb r4,[r1,r3] @ and load next byte
cmp r4,#0 @ zero final ?
beq 3f @ yes -> end search
add r2,#1 @ else increment counter string
b 1b @ and loop
3:
mov r0,r6
100:
pop {r1-r6,lr} @ restaur registers
bx lr
/******************************************************************/
/* display text with size calculation */
/******************************************************************/
/* r0 contains the address of the message */
affichageMess:
push {r0,r1,r2,r7,lr} @ save 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"
svc #0 @ call system
pop {r0,r1,r2,r7,lr} @ restaur registers
bx lr @ return

View file

@ -1,13 +1,9 @@
10 LET A$="abcdefghijklmnopqrstuvwxyz"
15 LET n=10: LET m=7
20 PRINT A$(n TO n+m-1)
30 PRINT A$(n TO )
40 PRINT A$( TO LEN (A$)-1)
50 FOR i=1 TO LEN (A$)
60 IF A$(i)="g" THEN PRINT A$(i TO i+m-1): LET i=LEN (A$): GO TO 70
70 NEXT i
80 LET B$="ijk"
90 FOR i=1 TO LEN (A$)-LEN (B$)+1
100 IF A$(i TO i+LEN (B$)-1)=B$ THEN PRINT A$(i TO i+m-1): LET i=LEN (A$)-LEN (B$)+1: GO TO 110
110 NEXT i
120 STOP
100 LET A$="abcdefghijklmnopqrstuvwxyz"
110 LET N=10:LET M=7
120 PRINT A$(N:N+M-1)
130 PRINT A$(N:)
140 PRINT A$(:LEN(A$)-1)
150 LET I=POS(A$,"g")
160 PRINT A$(I:I+M-1)
170 LET I=POS(A$,"ijk")
180 PRINT A$(I:I+M-1)

View file

@ -1,12 +1,13 @@
10 LET A$="abcdefghijklmnopqrstuvwxyz": LET la=LEN A$
20 LET n=10: LET m=7
30 PRINT A$(n TO n+m-1)
40 PRINT A$(n TO )
50 PRINT A$( TO la-1)
60 FOR i=1 TO la
70 IF A$(i)="g" THEN PRINT A$(i TO i+m-1): LET i=la
80 NEXT i
90 LET B$="ijk": LET lb=LEN b$
100 FOR i=1 TO la-lb+1
110 IF A$(i TO i+lb-1)=B$ THEN PRINT A$(i TO i+m-1): LET i=la-lb+1
120 NEXT i
10 LET A$="abcdefghijklmnopqrstuvwxyz"
15 LET n=10: LET m=7
20 PRINT A$(n TO n+m-1)
30 PRINT A$(n TO )
40 PRINT A$( TO LEN (A$)-1)
50 FOR i=1 TO LEN (A$)
60 IF A$(i)="g" THEN PRINT A$(i TO i+m-1): LET i=LEN (A$): GO TO 70
70 NEXT i
80 LET B$="ijk"
90 FOR i=1 TO LEN (A$)-LEN (B$)+1
100 IF A$(i TO i+LEN (B$)-1)=B$ THEN PRINT A$(i TO i+m-1): LET i=LEN (A$)-LEN (B$)+1: GO TO 110
110 NEXT i
120 STOP

View file

@ -0,0 +1,12 @@
10 LET A$="abcdefghijklmnopqrstuvwxyz": LET la=LEN A$
20 LET n=10: LET m=7
30 PRINT A$(n TO n+m-1)
40 PRINT A$(n TO )
50 PRINT A$( TO la-1)
60 FOR i=1 TO la
70 IF A$(i)="g" THEN PRINT A$(i TO i+m-1): LET i=la
80 NEXT i
90 LET B$="ijk": LET lb=LEN b$
100 FOR i=1 TO la-lb+1
110 IF A$(i TO i+lb-1)=B$ THEN PRINT A$(i TO i+m-1): LET i=la-lb+1
120 NEXT i

View file

@ -0,0 +1,25 @@
basestring$ = "The five boxing wizards jump quickly"
n% = 10
m% = 5
REM starting from n characters in and of m length:
substring$ = MID$(basestring$, n%, m%)
PRINT substring$
REM starting from n characters in, up to the end of the string:
substring$ = MID$(basestring$, n%)
PRINT substring$
REM whole string minus last character:
substring$ = LEFT$(basestring$)
PRINT substring$
REM starting from a known character within the string and of m length:
char$ = "w"
substring$ = MID$(basestring$, INSTR(basestring$, char$), m%)
PRINT substring$
REM starting from a known substring within the string and of m length:
find$ = "iz"
substring$ = MID$(basestring$, INSTR(basestring$, find$), m%)
PRINT substring$

View file

@ -1,16 +1,16 @@
import extensions.
import extensions;
program =
[
var s := "0123456789".
var n := 3.
var m := 2.
var c := $51.
var z := "345".
public program()
{
var s := "0123456789";
var n := 3;
var m := 2;
var c := $51;
var z := "345";
console writeLine(s Substring:m at:n).
console writeLine(s Substring(s length - n) at:n).
console writeLine(s Substring(s length - 1) at:0).
console writeLine(s Substring:m at(s indexOf:c at:0)).
console writeLine(s Substring:m at(s indexOf:z at:0)).
].
console.writeLine(s.Substring(n, m));
console.writeLine(s.Substring(n, s.Length - n));
console.writeLine(s.Substring(0, s.Length - 1));
console.writeLine(s.Substring(s.indexOf(0, c), m));
console.writeLine(s.Substring(s.indexOf(0, z), m))
}

View file

@ -0,0 +1,5 @@
s[n..n+m]
s[n..high(nativeUInt)]
s[1..length(s)-1]
s[pos(c, s)..pos(c, s)+m]
s[pos(p, s)..pos(p, s)+m]

View file

@ -1,10 +1,21 @@
def str = 'abcdefgh'
def n = 2
def m = 3
// #1
println str[n..n+m-1]
/* or */
println str[n..<(n+m)]
// #2
println str[n..-1]
// #3
println str[0..-2]
// #4
def index1 = str.indexOf('d')
println str[index1..index1+m-1]
/* or */
println str[index1..<(index1+m)]
// #5
def index2 = str.indexOf('de')
println str[index2..index2+m-1]
/* or */
println str[index2..<(index2+m)]

View file

@ -1,8 +1,10 @@
import qualified Data.Text as T
(Text, take, drop, init, breakOn, pack, unpack)
{-# LANGUAGE OverloadedStrings #-}
fromNforM :: Int -> Int -> T.Text -> T.Text
fromNforM n m s = T.take m (T.drop n s)
import qualified Data.Text as T (Text, take, drop, init, breakOn)
import qualified Data.Text.IO as O (putStrLn)
fromMforN :: Int -> Int -> T.Text -> T.Text
fromMforN n m s = T.take m (T.drop n s)
fromNtoEnd :: Int -> T.Text -> T.Text
fromNtoEnd = T.drop
@ -10,21 +12,20 @@ fromNtoEnd = T.drop
allButLast :: T.Text -> T.Text
allButLast = T.init
fromCharForM :: T.Text -> Int -> T.Text -> T.Text
fromCharForM needle m haystack = T.take m $ snd (T.breakOn needle haystack)
fromCharForN, fromStringForN :: Int -> T.Text -> T.Text -> T.Text
fromCharForN m needle haystack = T.take m $ snd $ T.breakOn needle haystack
fromStringForM :: T.Text -> Int -> T.Text -> T.Text
fromStringForM = fromCharForM
fromStringForN = fromCharForN
-- TEST -----------------------------------------------------------------------
-- TEST ---------------------------------------------------
main :: IO ()
main =
mapM_
(putStrLn . T.unpack)
([ fromNforM 9 10
O.putStrLn
([ fromMforN 9 10
, fromNtoEnd 20
, allButLast
, fromCharForM (T.pack "") 6
, fromStringForM (T.pack "大势") 6
, fromCharForN 6 ""
, fromStringForN 6 "大势"
] <*>
[T.pack "天地不仁仁者人也🐒话说天下大势分久必合🍑合久必分🔥"])
["天地不仁仁者人也🐒话说天下大势分久必合🍑合久必分🔥"])

View file

@ -0,0 +1,23 @@
Public Sub substring()
'(1) starting from n characters in and of m length;
'(2) starting from n characters in, up to the end of the string;
'(3) whole string minus last character;
'(4) starting from a known character within the string and of m length;
'(5) starting from a known substring within the string and of m length.
sentence = "the last thing the man said was the"
n = 10: m = 5
'(1)
Debug.Print Mid(sentence, n, 5)
'(2)
Debug.Print Right(sentence, Len(sentence) - n + 1)
'(3)
Debug.Print Left(sentence, Len(sentence) - 1)
'(4)
k = InStr(1, sentence, "m")
Debug.Print Mid(sentence, k, 5)
'(5)
k = InStr(1, sentence, "aid")
Debug.Print Mid(sentence, k, 5)
End Sub