From 14a19b2369d498c1540891f9621d4e6a83c31bd7 Mon Sep 17 00:00:00 2001 From: Lucas Bajolet Date: Thu, 10 May 2018 14:17:39 -0400 Subject: [PATCH] lib/core/text: add memset to `CString` Memset is an operation used for setting bytes to a particular value. Since it was not defined on CStrings, we add it. Signed-off-by: Lucas Bajolet --- lib/core/text/native.nit | 9 +++++++++ tests/sav/test_memset.res | 1 + tests/test_memset.nit | 15 +++++++++++++++ 3 files changed, 25 insertions(+) create mode 100644 tests/sav/test_memset.res create mode 100644 tests/test_memset.nit diff --git a/lib/core/text/native.nit b/lib/core/text/native.nit index f23b604..74f5f3f 100644 --- a/lib/core/text/native.nit +++ b/lib/core/text/native.nit @@ -30,6 +30,9 @@ in "C" `{ #ifndef be32toh #define be32toh(val) betoh32(val) #endif + +#include +#include `} redef class Byte @@ -313,4 +316,10 @@ extern class CString `{ char* `} fun lshift(sh, len, pos: Int) do copy_to(self, len, pos, pos - sh) end + + # Sets the contents of `self` to `value` for `len` bytes + fun memset(value, len: Int) `{ + assert(len >= 0); + memset(self, value, len); + `} end diff --git a/tests/sav/test_memset.res b/tests/sav/test_memset.res new file mode 100644 index 0000000..642c7f2 --- /dev/null +++ b/tests/sav/test_memset.res @@ -0,0 +1 @@ +AAAAAAA diff --git a/tests/test_memset.nit b/tests/test_memset.nit new file mode 100644 index 0000000..240a83c --- /dev/null +++ b/tests/test_memset.nit @@ -0,0 +1,15 @@ +# This file is part of NIT ( http://www.nitlanguage.org ). +# +# This file is free software, which comes along with NIT. This software is +# distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; +# without even the implied warranty of MERCHANTABILITY or FITNESS FOR A +# PARTICULAR PURPOSE. You can modify it is you want, provided this header +# is kept unaltered, and a notification of the changes is added. +# You are allowed to redistribute it and sell it, alone or is a part of +# another product. + +var in_value = b"BBBBBBB" + +in_value.items.memset(0x41, in_value.length) + +print in_value -- 1.7.9.5