// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved. // SPDX-License-Identifier: Apache-2.0 using System; using System.Text; using Wrappers_Compile; using ibyteseq = Dafny.ISequence; using byteseq = Dafny.Sequence; using icharseq = Dafny.ISequence; using charseq = Dafny.Sequence; namespace UTF8 { public partial class __default { public static _IResult Encode(icharseq str) { UTF8Encoding utf8 = new UTF8Encoding(false, true); try { byte[] utf8Bytes = utf8.GetBytes(str.Elements); return Result.create_Success(byteseq.FromArray(utf8Bytes)); } catch(EncoderFallbackException e) { return DafnyFFI.CreateFailure( "Input contains invalid Unicode characters"); } catch(ArgumentNullException e) { return DafnyFFI.CreateFailure("Input is null"); } } public static _IResult Decode(ibyteseq bytes) { UTF8Encoding utf8 = new UTF8Encoding(false, true); try { string decoded = utf8.GetString(bytes.Elements); return Result.create_Success(charseq.FromString(decoded)); } catch(ArgumentException e) { // GetString is defined as returning ArgumentException, ArgumentNullException, DecoderFallbackException // Both ArgumentNullException and DecoderFallbackException are children of ArgumentException return DafnyFFI.CreateFailure("Input contains an invalid Unicode code point"); } } } }