From 332ba8ed7e200d709377d97fceeb2b71e9bf03fe Mon Sep 17 00:00:00 2001 From: Irmen de Jong Date: Thu, 14 Dec 2023 01:41:23 +0100 Subject: [PATCH] don't give error when returning uword value in subroutine that returns STR --- compiler/src/prog8/compiler/astprocessing/AstChecker.kt | 2 ++ docs/source/programming.rst | 7 +++++++ docs/source/syntaxreference.rst | 7 +------ 3 files changed, 10 insertions(+), 6 deletions(-) diff --git a/compiler/src/prog8/compiler/astprocessing/AstChecker.kt b/compiler/src/prog8/compiler/astprocessing/AstChecker.kt index cbacbc48e..f240bd38d 100644 --- a/compiler/src/prog8/compiler/astprocessing/AstChecker.kt +++ b/compiler/src/prog8/compiler/astprocessing/AstChecker.kt @@ -124,6 +124,8 @@ internal class AstChecker(private val program: Program, } } else if(valueDt.isIterable && expectedReturnValues[0]==DataType.UWORD) { // you can return a string or array when an uword (pointer) is returned + } else if(valueDt istype DataType.UWORD && expectedReturnValues[0]==DataType.STR) { + // you can return a uword pointer when the return type is a string } else { errors.err("type $valueDt of return value doesn't match subroutine's return type ${expectedReturnValues[0]}",returnStmt.value!!.position) diff --git a/docs/source/programming.rst b/docs/source/programming.rst index b69befb71..8c4519877 100644 --- a/docs/source/programming.rst +++ b/docs/source/programming.rst @@ -391,6 +391,13 @@ Using the ``in`` operator you can easily check if a character is present in a st example: ``if '@' in email_address {....}`` (however this gives no clue about the location in the string where the character is present, if you need that, use the ``string.find()`` library function instead) +**Caution:** +This checks *all* elements in the string with the length as it was initially declared. +Even when a string was changed and is terminated early with a 0-byte early, +the containment check with ``in`` will still look at all character positions in the initial string. +Consider using ``string.find`` followed by ``if_cs`` (for instance) to do a "safer" search +for a character in such strings (one that stops at the first 0 byte) + .. hint:: Strings/arrays and uwords (=memory address) can often be interchanged. diff --git a/docs/source/syntaxreference.rst b/docs/source/syntaxreference.rst index 7ca6f5eda..e674b1787 100644 --- a/docs/source/syntaxreference.rst +++ b/docs/source/syntaxreference.rst @@ -611,16 +611,11 @@ containment check: ``in`` txt.print("cc is one of the values") } - str email_address = "?????????" + str email_address = "name@test.com" if '@' in email_address { txt.print("email address seems ok") } - .. caution:: - This check compares the needle against *all* elements in the haystack. - For byte arrays and strings(!), this means it considers *all* elements in the array or string with the length as it was declared. - Even when a string was changed and is terminated early with a 0-byte early. - Consider using ``string.find`` followed by ``if_cs`` (for instance) to do a "safer" containment check in such strings. address of: ``&`` This is a prefix operator that can be applied to a string or array variable or literal value.