1 /* 2 Copyright (c) 2018-2021 Timur Gafarov 3 4 Boost Software License - Version 1.0 - August 17th, 2003 5 6 Permission is hereby granted, free of charge, to any person or organization 7 obtaining a copy of the software and accompanying documentation covered by 8 this license (the "Software") to use, reproduce, display, distribute, 9 execute, and transmit the Software, and to prepare derivative works of the 10 Software, and to permit third-parties to whom the Software is furnished to 11 do so, all subject to the following: 12 13 The copyright notices in the Software and this entire statement, including 14 the above license grant, this restriction and the following disclaimer, 15 must be included in all copies of the Software, in whole or in part, and 16 all derivative works of the Software, unless such copies or derivative 17 works are solely in the form of machine-executable object code generated by 18 a source language processor. 19 20 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 21 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 22 FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 23 SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 24 FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 25 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 26 DEALINGS IN THE SOFTWARE. 27 */ 28 29 /** 30 * Generic encoding tools 31 * 32 * Description: 33 * This module works with any encoder and decoder structs that implement 34 * the following basic interfaces: 35 * --- 36 * struct Encoder 37 * { 38 * // Encodes a Unicode code point to user-provided buffer. 39 * // Should return bytes written or 0 at error 40 * size_t encode(uint codePoint, char[] buffer) 41 * } 42 * 43 * struct Decoder 44 * { 45 * // An input range that iterates characters of a string, 46 * // decoding them to Unicode code points 47 * auto decode(string input) 48 * } 49 * --- 50 * 51 * Copyright: Timur Gafarov 2018-2021. 52 * License: $(LINK2 boost.org/LICENSE_1_0.txt, Boost License 1.0). 53 * Authors: Timur Gafarov 54 */ 55 module dlib.text.encodings; 56 57 import dlib.container.array; 58 import dlib.text.utils; 59 60 public 61 { 62 import dlib.text.utf8; 63 import dlib.text.utf16; 64 } 65 66 /** 67 * Converts a string from one encoding to another. 68 * Decoder and encoder are specified at compile time 69 * 70 * Examples: 71 * --- 72 * string s = transcode!(UTF16Decoder, UTF8Encoder)(input); 73 * --- 74 */ 75 string transcode(Decoder, Encoder)(string input) 76 { 77 DynamicArray!char array; 78 79 auto decoder = Decoder(); 80 auto encoder = Encoder(); 81 82 foreach(c; decoder.decode(input)) 83 { 84 char[4] buffer; 85 size_t len = encoder.encode(c, buffer); 86 if (len) 87 array.append(buffer[0..len]); 88 } 89 90 auto output = copy(array.data); 91 array.free(); 92 return cast(string)output; 93 }