//TH-Header //***************************************************************************************** // Copyright (c) 2014 KURANT Project // All rights reserved. // // $FileName : strings.pkg // $ProjectName : The Hammer 2.0 // $Authors : Wil van Antwerpen, Michael Kurz, Sergey V. Natarov, Bernhard Ponemayr // $Created : 01.25.2014 01:08 // $Type : LGPL // // Contents: // //***************************************************************************************** //TH-RevisionStart //TH-RevisionEnd Use UI // Calling the function below will convert the number stored in src# to a // string containing dcp# decimals rounding excess decimals. Parameter dcp# // may be negative. The expression (NumToStr(1789,-3)) will evaluate to "2000". // The functions in this package all respects the value of global attribute // DF_DECIMAL_SEPARATOR. // Number converting functions: #Replace NUMTOSTR_THOUSANDS_SEPARATOR_OFF 0 #Replace NUMTOSTR_THOUSANDS_SEPARATOR_ON 1 Integer NumToStringConversionMode# Move NUMTOSTR_THOUSANDS_SEPARATOR_OFF To NumToStringConversionMode# Function NumToStr Global Number src# Integer dcp# Returns String Integer Pos# Number correction# String rval# radix# If dcp# Lt 0 Function_Return (NumToStr(src#*(10^dcp#),0)+Left("00000000",-dcp#)) Move (0.5/(10^dcp#)) To correction# // move (0.5/(10^dcp#)+src#) to src# // This does not always work with VDF 6.x!!! If src# Ge 0 Move (correction#+src#) To src# Else Move (src#-correction#) To src# Get_Attribute DF_DECIMAL_SEPARATOR To Pos# // Overload Character Pos# To radix# // End overload Move src# To rval# Ifnot (Pos(radix#,rval#)) Append rval# radix# Append rval# "00000000" Move (Pos(radix#,rval#)) To Pos# If dcp# Eq 0 Decrement Pos# Move (Left(rval#,Pos#+dcp#)) To rval# //If NumToStringConversionMode# Move (InsertThousandsSep(rval#)) To rval# Function_Return rval# End_Function // (ExtractWord("item1 item2"," ",0)) = "" // (ExtractWord("item1 item2"," ",1)) = "item1" // (ExtractWord("item1 item2"," ",2)) = "item2" // (ExtractWord("item1 item2"," ",3)) = "" // (ExtractWord(" item1 item2 "," ",x)) = // (ExtractWord("item1 item2"," ",x)) // // source delimiters item number Function ExtractWord Global String src# String dlm# Integer itm# Returns String Integer count# Pos# in_item# len# String rval# atom# Move "" To rval# Move 0 To count# Move 0 To in_item# Move (Length(src#)) To len# For Pos# From 1 To len# Mid src# To atom# 1 Pos# If in_item# Begin If atom# In dlm# Move 0 To in_item# Else If count# Eq itm# Append rval# atom# End Else Begin Ifnot atom# In dlm# Begin Increment count# Move 1 To in_item# If count# Eq itm# Move atom# To rval# End End Loop Function_Return rval# End_Function // source delimiters Function HowManyWords Global String src# String dlm# Returns Integer Integer count# Pos# in_item# len# String atom# Move 0 To count# Move 0 To in_item# Move (Length(src#)) To len# For Pos# From 1 To len# Mid src# To atom# 1 Pos# If in_item# Begin If atom# In dlm# Move 0 To in_item# End Else Begin Ifnot atom# In dlm# Begin Increment count# Move 1 To in_item# End End Loop Function_Return count# End_Function //> This function returns 1 if lsString is an integer and 2 if it //> is a number (the function does not currently handle thousand seps) Function StringIsNumber Global String lsString Integer liDecSep Returns Integer Integer liLen liPos liDecSepFound liRval String lsChar Move 1 To liRval Move 0 To liDecSepFound Move (Trim(lsString)) To lsString If (Left(lsString,1)="-") Move (Replace("-",lsString,"")) To lsString Move (Length(lsString)) To liLen For liPos From 1 To liLen Move (Mid(lsString,1,liPos)) To lsChar If (Ascii(lsChar)) Eq liDecSep Begin If liDecSepFound Function_Return 0 Move 1 To liDecSepFound End Else Ifnot ("0123456789" Contains lsChar) Function_Return 0 Loop Function_Return liRval End_Function Function StringIsInteger Global String lsString Returns Integer Integer liPos liLen Move (Trim(lsString)) To lsString If (Left(lsString,1)="-") Move (Replace("-",lsString,"")) To lsString Move (Length(lsString)) To liLen For liPos From 1 To liLen Ifnot (Pos(Mid(lsString,1,liPos),"0123456789")) Function_Return DFFALSE Loop Function_Return DFTRUE End_Function