From 57dc5367195444c8e0cbe4087eeedbcb08b9ae41 Mon Sep 17 00:00:00 2001 From: Adrian Pop Date: Tue, 11 Feb 2020 04:47:56 +0100 Subject: [PATCH] add and export StringFunction, check for array index --- src/dangerous.jl | 9 +++++++++ src/exportmetaRuntime.jl | 2 ++ src/metaRuntime.jl | 40 ++++++++++++++++++++++++++++++++++++---- 3 files changed, 47 insertions(+), 4 deletions(-) diff --git a/src/dangerous.jl b/src/dangerous.jl index 57ba14c..d3da6e5 100644 --- a/src/dangerous.jl +++ b/src/dangerous.jl @@ -10,12 +10,18 @@ using ..MetaModelica """ O(1) """ function arrayGetNoBoundsChecking(arr::Array{A}, index::ModelicaInteger)::A where {A <: Any} + if index < 0 + println("arrayGetNoBoundsChecking: index < 0!") + end @inbounds arr[index] end """ O(1) """ function arrayUpdateNoBoundsChecking(arr::Array{A}, index::ModelicaInteger, newValue::A)::Array{A} where {A <: Any} local newArray::Array{A} = arr + if index < 0 + println("arrayUpdateNoBoundsChecking: index < 0!") + end @inbounds newArray[index] = newValue newArray end @@ -33,6 +39,9 @@ end """ O(1) """ function stringGetNoBoundsChecking(str::String, index::ModelicaInteger)::ModelicaInteger local ch::ModelicaInteger + if index < 0 + println("stringGetNoBoundsChecking: index < 0!") + end ch = @inbounds str[index] end diff --git a/src/exportmetaRuntime.jl b/src/exportmetaRuntime.jl index acf26c5..590e8c6 100644 --- a/src/exportmetaRuntime.jl +++ b/src/exportmetaRuntime.jl @@ -11,6 +11,7 @@ export arrayGet export arrayLength export arrayList export arrayUpdate +export boolNot export boolAnd export boolEq export boolOr @@ -114,3 +115,4 @@ export valueHashMod export valueSlots export _listAppend export getInstanceName +export StringFunction diff --git a/src/metaRuntime.jl b/src/metaRuntime.jl index b5568b1..ce9af09 100644 --- a/src/metaRuntime.jl +++ b/src/metaRuntime.jl @@ -397,11 +397,19 @@ end """ O(1) """ function stringGet(str::String, index::ModelicaInteger)::ModelicaInteger + if index < 0 + println("stringGet: index < 0!") + fail() + end str[index] end """ O(1) """ function stringGetStringChar(str::String, index::ModelicaInteger)::String + if index < 0 + println("stringGetStringChar: index < 0!") + fail() + end local ch::String = string(str[index]) ch end @@ -409,6 +417,10 @@ end """ O(n) """ function stringUpdateStringChar(str::String, newch::String, index::ModelicaInteger)::String local news::String = str + if index < 0 + println("stringUpdateStringChar: index < 0!") + fail() + end news[index] = newch[1] news end @@ -467,6 +479,10 @@ function stringHashSdbm(str::String)::ModelicaInteger end function substring(str::String, start #=start index, first character is 1 =#::ModelicaInteger, stop #= stop index, first character is 1 =#::ModelicaInteger)::String + if start < 0 + println("substring: start < 0!") + fail() + end local out = str[start:stop] out end @@ -483,6 +499,10 @@ end """ O(1) """ function arrayGet(arr::Array{A}, index::ModelicaInteger)::A where {A} + if index < 0 + println("arrayGet: index < 0!") + fail() + end arr[index] end @@ -512,10 +532,14 @@ end """ O(1) """ function arrayUpdate(arr::Array{A}, index::ModelicaInteger, newValue::B)::Array{A} where {A,B} local newArray #= same as the input array; used for folding =#::Array{A} = arr - if !(A <: B) - println("!(A<:B)") - @show A - @show B + #if !(A <: B) + # println("!(A<:B)") + # @show A + # @show B + #end + if index < 0 + println("arrayUpdate: index < 0!") + fail() end newArray[index] = newValue #= Defined in the runtime =# @@ -755,3 +779,11 @@ end function getInstanceName()::String "__NOT_IMPLEMENTED__" end + +function StringFunction(i::Int64)::String + intString(i) +end + +function StringFunction(r::Float64)::String + realString(r) +end