Skip to content

Commit ed545e3

Browse files
pmderodatraph-amiard
authored andcommitted
Langkit_Support.Text: add To_UTF8 and From_UTF8
TN: RA02-064
1 parent c50f689 commit ed545e3

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

langkit/support/langkit_support-text.adb

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,4 +181,24 @@ package body Langkit_Support.Text is
181181
return Text_Result;
182182
end Decode;
183183

184+
-------------
185+
-- To_UTF8 --
186+
-------------
187+
188+
function To_UTF8
189+
(Text : Text_Type) return Ada.Strings.UTF_Encoding.UTF_8_String is
190+
begin
191+
return Encode (Text, "UTF-8");
192+
end To_UTF8;
193+
194+
---------------
195+
-- From_UTF8 --
196+
---------------
197+
198+
function From_UTF8
199+
(S : Ada.Strings.UTF_Encoding.UTF_8_String) return Text_Type is
200+
begin
201+
return Decode (S, "UTF-8");
202+
end From_UTF8;
203+
184204
end Langkit_Support.Text;

langkit/support/langkit_support-text.ads

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
-- <http://www.gnu.org/licenses/>. --
2222
------------------------------------------------------------------------------
2323

24+
with Ada.Strings.UTF_Encoding;
2425
with Ada.Strings.Wide_Wide_Unbounded;
2526
with Ada.Unchecked_Deallocation;
2627

@@ -68,6 +69,14 @@ package Langkit_Support.Text is
6869
function Decode (S : String; Charset : String) return Text_Type;
6970
-- Likewise, but convert a string to text
7071

72+
function To_UTF8
73+
(Text : Text_Type) return Ada.Strings.UTF_Encoding.UTF_8_String;
74+
-- Encode the given text into an UTF-8 string
75+
76+
function From_UTF8
77+
(S : Ada.Strings.UTF_Encoding.UTF_8_String) return Text_Type;
78+
-- Decode the given UTF-8 string into text
79+
7180
type Text_Access is access all Text_Type;
7281
type Text_Cst_Access is access constant Text_Type;
7382

0 commit comments

Comments
 (0)