code independent from /az option (0-based arrays)

This forum is meant for examples of X# code.

Post Reply
User avatar
wriedmann
Posts: 3754
Joined: Mon Nov 02, 2015 5:07 pm
Location: Italy

code independent from /az option (0-based arrays)

Post by wriedmann »

The best option to write code independent from the /az compiler option (use 0-based arrays) is to use foreach:

Code: Select all

local aData as string[]
	
aData := <string>{ "one", "two", "three", "four", "five", "six", "seven" }
foreach cString as string in aData
    System.Console.WriteLine( String.Format( "member is {0}", cString ) )
next


but if you cannot for some reason, the following code works:

Code: Select all

local aData as string[]
local nLen as int
local nI as int
	
aData := <string>{ "one", "two", "three", "four", "five", "six", "seven" }
nLen := aData:Length - 1 + __ARRAYBASE__
for nI := ( 0 + __ARRAYBASE__ ) upto nLen
   System.Console.WriteLine( String.Format( "member {0} is {1}", nI:ToString(), aData[nI] ) )
next
Wolfgang Riedmann
Meran, South Tyrol, Italy
wolfgang@riedmann.it
https://www.riedmann.it - https://docs.xsharp.it
FFF
Posts: 1580
Joined: Fri Sep 25, 2015 4:52 pm
Location: Germany

code independent from /az option (0-based arrays)

Post by FFF »

Curious:is there a hidden reason for the "dummy"-Addition (0+__Arraybase__)?
Regards
Karl
(on Win8.1/64, Xide32 2.20, X#2.20.0.3)
User avatar
wriedmann
Posts: 3754
Joined: Mon Nov 02, 2015 5:07 pm
Location: Italy

code independent from /az option (0-based arrays)

Post by wriedmann »

Hi Karl,

yes - stupdity <g>. __ARRAYBASE__ alone would be more than enough.

Wolfgang
Wolfgang Riedmann
Meran, South Tyrol, Italy
wolfgang@riedmann.it
https://www.riedmann.it - https://docs.xsharp.it
User avatar
robert
Posts: 4518
Joined: Fri Aug 21, 2015 10:57 am
Location: Netherlands

code independent from /az option (0-based arrays)

Post by robert »

karl, Wolfgang,
The compiler will optimize (fold) the expression 0 + __ARRAYBASE__ into one numeric constant, regardless of the setting of /az.


Robert
XSharp Development Team
The Netherlands
robert@xsharp.eu
User avatar
wriedmann
Posts: 3754
Joined: Mon Nov 02, 2015 5:07 pm
Location: Italy

code independent from /az option (0-based arrays)

Post by wriedmann »

Hello,

when trying to write a version of Proper() that is independent from the /az compiler option, I had to learn that a string is NOT an array, and therefore all indexes are 0-based:

Code: Select all

cString[cString:Length]
will give a runtime error, whereas

Code: Select all

cString[0]
not.

And this is my Proper() extension method:

Code: Select all

static class StringExtensions
static method Proper( self cString as string ) as string   
  local cReturn as string
  local aChars as char[]
  local lNewWord as logic
  local nI as int
  local nLen as int   
  local cCurrent as Char
	
  if String.IsNullOrEmpty( cString )
    return cString
  endif                         
  nLen := cString:Length
  aChars := char[]{ nLen }
  lNewWord := true      
  --nLen
  for nI := 0 upto nLen
    cCurrent := cString[nI]
    if char.IsLetterOrDigit( cCurrent )
      if lNewWord
        lNewWord := false
        cCurrent := Char.ToUpper( cCurrent )
      else
        cCurrent := Char.ToLower( cCurrent )
      endif
    else
      lNewWord := true
    endif
    aChars[nI+__ARRAYBASE__] := cCurrent
  next
  cReturn := string{ aChars }
	
  return cReturn      
	
end class
Wolfgang
Wolfgang Riedmann
Meran, South Tyrol, Italy
wolfgang@riedmann.it
https://www.riedmann.it - https://docs.xsharp.it
Post Reply