1 /* 2 * hunt-proton: AMQP Protocol library for D programming language. 3 * 4 * Copyright (C) 2018-2019 HuntLabs 5 * 6 * Website: https://www.huntlabs.net/ 7 * 8 * Licensed under the Apache-2.0 License. 9 * 10 */ 11 12 module hunt.proton.engine.impl.StringUtils; 13 14 import hunt.proton.amqp.Binary; 15 import std.format; 16 class StringUtils 17 { 18 /** 19 * Converts the Binary to a quoted string. 20 * 21 * @param bin the Binary to convert 22 * @param stringLength the maximum length of stringified content (excluding the quotes, and truncated indicator) 23 * @param appendIfTruncated appends "...(truncated)" if not all of the payload is present in the string 24 * @return the converted string 25 */ 26 public static string toQuotedString(Binary bin,int stringLength,bool appendIfTruncated) 27 { 28 if(bin is null) 29 { 30 return "\"\""; 31 } 32 33 byte[] binData = bin.getArray(); 34 int binLength = bin.getLength(); 35 int offset = bin.getArrayOffset(); 36 37 string str ; 38 str ~= ("\""); 39 40 int size = 0; 41 bool truncated = false; 42 for (int i = 0; i < binLength; i++) 43 { 44 byte c = binData[offset + i]; 45 46 if (c > 31 && c < 127 && c != '\\') 47 { 48 if (size + 1 <= stringLength) 49 { 50 size += 1; 51 str ~= (cast(char) c); 52 } 53 else 54 { 55 truncated = true; 56 break; 57 } 58 } 59 else 60 { 61 if (size + 4 <= stringLength) 62 { 63 size += 4; 64 str ~= (format("\\x%02x", c)); 65 } 66 else 67 { 68 truncated = true; 69 break; 70 } 71 } 72 } 73 74 str ~= ("\""); 75 76 if (truncated && appendIfTruncated) 77 { 78 str ~= ("...(truncated)"); 79 } 80 81 return str; 82 } 83 }