2020-10-21 22:49:08 +02:00
|
|
|
# Copyright 2020 Mattia Giambirtone
|
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
2020-10-21 22:34:04 +02:00
|
|
|
|
2020-08-24 09:00:23 +02:00
|
|
|
# This module implements the interface for strings in JAPL.
|
|
|
|
# Strings are manually-allocated arrays of characters, and are
|
|
|
|
# therefore immutable from the user's perspective. They are
|
|
|
|
# natively ASCII encoded, but soon they will support for unicode.
|
|
|
|
|
2020-10-23 17:14:55 +02:00
|
|
|
import japlvalue
|
2020-09-03 19:24:18 +02:00
|
|
|
import strformat
|
2020-08-23 12:10:08 +02:00
|
|
|
import ../memory
|
2020-08-21 19:10:33 +02:00
|
|
|
|
|
|
|
|
2020-09-03 19:24:18 +02:00
|
|
|
proc stringify*(s: ptr String): string =
|
|
|
|
result = ""
|
|
|
|
for i in 0..<s.len:
|
|
|
|
result = result & (&"{s.str[i]}")
|
2020-08-21 19:10:33 +02:00
|
|
|
|
|
|
|
|
2020-09-03 19:24:18 +02:00
|
|
|
proc isFalsey*(s: ptr String): bool =
|
2020-08-23 12:10:08 +02:00
|
|
|
result = s.len == 0
|
2020-08-21 19:10:33 +02:00
|
|
|
|
|
|
|
|
2020-09-03 19:24:18 +02:00
|
|
|
proc hash*(self: ptr String): uint32 =
|
2020-08-26 11:41:39 +02:00
|
|
|
result = 2166136261u32
|
2020-08-24 11:25:26 +02:00
|
|
|
var i = 0
|
|
|
|
while i < self.len:
|
|
|
|
result = result xor uint32 self.str[i]
|
|
|
|
result *= 16777619
|
|
|
|
i += 1
|
|
|
|
return result
|
|
|
|
|
|
|
|
|
2020-10-22 16:05:39 +02:00
|
|
|
proc eq*(a: ptr String, b: ptr String): bool =
|
2020-08-25 10:24:50 +02:00
|
|
|
if a.len != b.len:
|
|
|
|
return false
|
|
|
|
elif a.hash != b.hash:
|
|
|
|
return false
|
|
|
|
for i in 0..a.len - 1:
|
|
|
|
if a.str[i] != b.str[i]:
|
|
|
|
return false
|
|
|
|
return true
|
|
|
|
|
|
|
|
|
|
|
|
proc newString*(str: string): ptr String =
|
2020-08-24 09:00:23 +02:00
|
|
|
# TODO -> Unicode
|
2020-10-17 12:35:32 +02:00
|
|
|
result = allocateObj(String, ObjectType.String)
|
2020-08-25 11:10:14 +02:00
|
|
|
result.str = allocate(UncheckedArray[char], char, len(str))
|
2020-08-23 12:10:08 +02:00
|
|
|
for i in 0..len(str) - 1:
|
2020-08-25 11:10:14 +02:00
|
|
|
result.str[i] = str[i]
|
2020-08-25 10:24:50 +02:00
|
|
|
result.len = len(str)
|
2020-09-03 19:24:18 +02:00
|
|
|
result.hashValue = result.hash()
|
2020-08-23 16:12:54 +02:00
|
|
|
|
|
|
|
|
2020-09-03 19:24:18 +02:00
|
|
|
proc typeName*(s: ptr String): string =
|
2020-08-23 23:48:38 +02:00
|
|
|
return "string"
|
2020-10-23 00:31:39 +02:00
|
|
|
|
|
|
|
|
|
|
|
proc asStr*(s: string): Value =
|
|
|
|
## Creates a string object
|
|
|
|
result = Value(kind: ValueType.Object, obj: newString(s))
|