FireMonkey 예제 자료
LZstring 자바스크립트 compressToEncoded부분을 델파이로 번역 끝(소스 업로드)
kimsyo11
2021. 12. 13. 00:48
728x90
아래의 내용이 자바스크립트 내용이며 더 아래로 내려가시면
델파이 파스칼 언어로 번역한것이 나옵니다
사용 개발 환경 프로그램 : Rad Studio 10.3
번역한 델파이 코드는 밑에 있습니다
function _compress(uncompressed, bitsPerChar, getCharFromInt) {
if (uncompressed == null) return "";
var i, value,
context_dictionary= {},
context_dictionaryToCreate= {},
context_c="",
context_wc="",
context_w="",
context_enlargeIn= 2, // Compensate for the first entry which should not count
context_dictSize= 3,
context_numBits= 2,
context_data=[],
context_data_val=0,
context_data_position=0,
ii;
for (ii = 0; ii < uncompressed.length; ii += 1) {
context_c = uncompressed.charAt(ii);
if (!Object.prototype.hasOwnProperty.call(context_dictionary,context_c)) {
context_dictionary[context_c] = context_dictSize++;
context_dictionaryToCreate[context_c] = true;
}
context_wc = context_w + context_c;
if (Object.prototype.hasOwnProperty.call(context_dictionary,context_wc)) {
context_w = context_wc;
} else {
if (Object.prototype.hasOwnProperty.call(context_dictionaryToCreate,context_w)) {
if (context_w.charCodeAt(0)<256) {
for (i=0 ; i<context_numBits ; i++) {
context_data_val = (context_data_val << 1);
if (context_data_position == bitsPerChar-1) {
context_data_position = 0;
context_data.push(getCharFromInt(context_data_val));
context_data_val = 0;
} else {
context_data_position++;
}
}
value = context_w.charCodeAt(0);
for (i=0 ; i<8 ; i++) {
context_data_val = (context_data_val << 1) | (value&1);
if (context_data_position == bitsPerChar-1) {
context_data_position = 0;
context_data.push(getCharFromInt(context_data_val));
context_data_val = 0;
} else {
context_data_position++;
}
value = value >> 1;
}
} else {
value = 1;
for (i=0 ; i<context_numBits ; i++) {
context_data_val = (context_data_val << 1) | value;
if (context_data_position ==bitsPerChar-1) {
context_data_position = 0;
context_data.push(getCharFromInt(context_data_val));
context_data_val = 0;
} else {
context_data_position++;
}
value = 0;
}
value = context_w.charCodeAt(0);
for (i=0 ; i<16 ; i++) {
context_data_val = (context_data_val << 1) | (value&1);
if (context_data_position == bitsPerChar-1) {
context_data_position = 0;
context_data.push(getCharFromInt(context_data_val));
context_data_val = 0;
} else {
context_data_position++;
}
value = value >> 1;
}
}
context_enlargeIn--;
if (context_enlargeIn == 0) {
context_enlargeIn = Math.pow(2, context_numBits);
context_numBits++;
}
delete context_dictionaryToCreate[context_w];
} else {
value = context_dictionary[context_w];
for (i=0 ; i<context_numBits ; i++) {
context_data_val = (context_data_val << 1) | (value&1);
if (context_data_position == bitsPerChar-1) {
context_data_position = 0;
context_data.push(getCharFromInt(context_data_val));
context_data_val = 0;
} else {
context_data_position++;
}
value = value >> 1;
}
}
context_enlargeIn--;
if (context_enlargeIn == 0) {
context_enlargeIn = Math.pow(2, context_numBits);
context_numBits++;
}
// Add wc to the dictionary.
context_dictionary[context_wc] = context_dictSize++;
context_w = String(context_c);
}
}
// Output the code for w.
if (context_w !== "") {
if (Object.prototype.hasOwnProperty.call(context_dictionaryToCreate,context_w)) {
if (context_w.charCodeAt(0)<256) {
for (i=0 ; i<context_numBits ; i++) {
context_data_val = (context_data_val << 1);
if (context_data_position == bitsPerChar-1) {
context_data_position = 0;
context_data.push(getCharFromInt(context_data_val));
context_data_val = 0;
} else {
context_data_position++;
}
}
value = context_w.charCodeAt(0);
for (i=0 ; i<8 ; i++) {
context_data_val = (context_data_val << 1) | (value&1);
if (context_data_position == bitsPerChar-1) {
context_data_position = 0;
context_data.push(getCharFromInt(context_data_val));
context_data_val = 0;
} else {
context_data_position++;
}
value = value >> 1;
}
} else {
value = 1;
for (i=0 ; i<context_numBits ; i++) {
context_data_val = (context_data_val << 1) | value;
if (context_data_position == bitsPerChar-1) {
context_data_position = 0;
context_data.push(getCharFromInt(context_data_val));
context_data_val = 0;
} else {
context_data_position++;
}
value = 0;
}
value = context_w.charCodeAt(0);
for (i=0 ; i<16 ; i++) {
context_data_val = (context_data_val << 1) | (value&1);
if (context_data_position == bitsPerChar-1) {
context_data_position = 0;
context_data.push(getCharFromInt(context_data_val));
context_data_val = 0;
} else {
context_data_position++;
}
value = value >> 1;
}
}
context_enlargeIn--;
if (context_enlargeIn == 0) {
context_enlargeIn = Math.pow(2, context_numBits);
context_numBits++;
}
delete context_dictionaryToCreate[context_w];
} else {
value = context_dictionary[context_w];
for (i=0 ; i<context_numBits ; i++) {
context_data_val = (context_data_val << 1) | (value&1);
if (context_data_position == bitsPerChar-1) {
context_data_position = 0;
context_data.push(getCharFromInt(context_data_val));
context_data_val = 0;
} else {
context_data_position++;
}
value = value >> 1;
}
}
context_enlargeIn--;
if (context_enlargeIn == 0) {
context_enlargeIn = Math.pow(2, context_numBits);
context_numBits++;
}
}
// Mark the end of the stream
value = 2;
for (i=0 ; i<context_numBits ; i++) {
context_data_val = (context_data_val << 1) | (value&1);
if (context_data_position == bitsPerChar-1) {
context_data_position = 0;
context_data.push(getCharFromInt(context_data_val));
context_data_val = 0;
} else {
context_data_position++;
}
value = value >> 1;
}
// Flush the last char
while (true) {
context_data_val = (context_data_val << 1);
if (context_data_position == bitsPerChar-1) {
context_data.push(getCharFromInt(context_data_val));
break;
}
else context_data_position++;
}
return context_data.join('');
}
위내용을 델파이로 번역하였습니다
↓ 번역한 델파이 번역 코드
델파이 10.4.1 이하버전에서는
안드로이드 context_c := uncompressed[I]; 이였으나 델파이 버전 올라가면서
안드로이드 context_c := uncompressed[I+1]; 로 변경해주셔야 합니다
{
kimsyo5140
kimsyo5140@naver.com
}
uses Math, System.JSON;
const keyStrUriSafe = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+-$';
function _Compress(uncompressed : string; bitsPerChar : integer; getCharFromInt : string) : string;
var value : integer;
context_dictionary, context_dictionaryToCreate : TJSONObject;
context_c, context_wc, context_w : string;
context_data : TStringList;
LJSONValue : TJSONValue;
function TestBool(JSonOjbect : TJSONObject; TmpStr : string) : boolean;
begin
Result := False;
for var I : integer := 0 to JSonOjbect.Count -1 do
if JSonOjbect.Pairs[I].JsonString.Value = tmpstr then
begin
Result := true;
Break;
end;
end;
begin
context_dictionary := TJSONObject.Create;
context_dictionaryToCreate := TJSONObject.Create;
context_data := TStringList.Create;
var context_enlargeIn : Single := 2;
var context_dictSize : integer := 3;
var context_numBits : integer := 2;
var context_data_val : integer := 0;
var context_data_position : integer := 0;
if uncompressed = '' then
begin
Result :='';
Exit;
end;
for var I : integer := 0 to uncompressed.Length -1 do
begin
{$IFDEF ANDROID}
context_c := uncompressed[I];
{$ELSE}
context_c := uncompressed[I+1];
{$ENDIF}
if not TestBool(context_dictionary,context_c) then
begin
context_dictionary.AddPair(TJSONPair.Create(context_c, TJSONString.Create(context_dictSize.ToString)));
context_dictSize := context_dictSize + 1;
context_dictionaryToCreate.AddPair(TJSONPair.Create(context_c, TJSONString.Create('True')));
end;
context_wc := context_w + context_c;
if TestBool(context_dictionary,context_wc) then
context_w := context_wc
else begin
if TestBool(context_dictionaryToCreate,context_w) then
begin
if Ord(context_w.Chars[0]) < 256 then
begin
for var I1 : integer := 0 to context_numBits -1 do
begin
context_data_val := (context_data_val shl 1);
if context_data_position = bitsPerChar-1 then
begin
context_data_position := 0;
context_data.Add(getCharFromInt.Chars[context_data_val]);
context_data_val := 0;
end else
context_data_position := context_data_position + 1;
end;
value := Ord(context_w.Chars[0]);
for var I2 : integer := 0 to 7 do
begin
context_data_val := (context_data_val shl 1) or (value and 1);
if (context_data_position = bitsPerChar-1) then
begin
context_data_position := 0;
context_data.Add(getCharFromInt.Chars[context_data_val]);
context_data_val := 0;
end else
context_data_position := context_data_position + 1;
value := value shr 1;
end;
end else begin
value := 1;
for var I3 : integer := 0 to context_numBits -1 do
begin
context_data_val := (context_data_val shl 1) or value;
if (context_data_position = bitsPerChar-1) then
begin
context_data_position := 0;
context_data.Add(getCharFromInt.Chars[context_data_val]);
context_data_val := 0;
end else
context_data_position := context_data_position + 1;
value := 0;
end;
value := Ord(context_w.Chars[0]);
for var I4 : integer := 0 to 15 do
begin
context_data_val := (context_data_val shl 1) or (value and 1);
if (context_data_position = bitsPerChar-1) then
begin
context_data_position := 0;
context_data.Add(getCharFromInt.Chars[context_data_val]);
context_data_val := 0;
end else
context_data_position := context_data_position + 1;
value := value shr 1;
end;
end;
context_enlargeIn := context_enlargeIn -1;
if context_enlargeIn = 0 then
begin
context_enlargeIn := Math.Power(2, context_numBits);
context_numBits := context_numBits + 1;
end;
context_dictionaryToCreate.RemovePair(context_w);
end else begin
value := context_dictionary.GetValue(context_w).Value.ToInteger;
for var I5 : integer := 0 to context_numBits -1 do
begin
context_data_val := (context_data_val shl 1) or (value and 1);
if (context_data_position = bitsPerChar -1) then
begin
context_data_position := 0;
context_data.Add(getCharFromInt.Chars[context_data_val]);
context_data_val := 0;
end else
context_data_position := context_data_position + 1;
value := value shr 1;
end;
end;
context_enlargeIn := context_enlargeIn-1;
if (context_enlargeIn = 0) then
begin
context_enlargeIn := Math.Power(2, context_numBits);
context_numBits := context_numBits +1;
end;
context_dictionary.AddPair(TJSONPair.Create(context_wc, TJSONString.Create(context_dictSize.ToString)));
context_dictSize := context_dictSize +1;
context_w := context_c;
end;
end;
if not (context_w = '') then
begin
if TestBool(context_dictionaryToCreate,context_w) then
begin
if (ord(context_w.Chars[0]) < 256) then
begin
for var I6 : integer := 0 to context_numBits -1 do
begin
context_data_val := (context_data_val shl 1);
if (context_data_position = bitsPerChar-1) then
begin
context_data_position := 0;
context_data.Add(getCharFromInt.Chars[context_data_val]);
context_data_val := 0;
end else
context_data_position := context_data_position +1;
end;
value := ord(context_w.Chars[0]);
for var I7 : integer := 0 to 7 do
begin
context_data_val := (context_data_val shl 1) or (value and 1);
if (context_data_position = bitsPerChar-1) then
begin
context_data_position := 0;
context_data.Add(getCharFromInt.Chars[context_data_val]);
context_data_val := 0;
end else
context_data_position := context_data_position +1;
value := value shr 1;
end;
end else begin
value := 1;
for var I8 : integer := 0 to context_numBits-1 do
begin
context_data_val := (context_data_val shl 1) or value;
if (context_data_position = bitsPerChar-1) then
begin
context_data_position := 0;
context_data.Add(getCharFromInt.Chars[context_data_val]);
context_data_val := 0;
end else
context_data_position := context_data_position +1;
value := 0;
end;
value := ord(context_w.Chars[0]);
for var I9 : integer := 0 to 15 do
begin
context_data_val := (context_data_val shl 1) or (value and 1);
if (context_data_position = bitsPerChar-1) then
begin
context_data_position := 0;
context_data.Add(getCharFromInt.Chars[context_data_val]);
context_data_val := 0;
end else
context_data_position := context_data_position +1;
value := value shr 1;
end;
end;
context_enlargeIn := context_enlargeIn-1;
if (context_enlargeIn = 0) then
begin
context_enlargeIn := math.Power(2, context_numBits);
context_numBits := context_numBits+1;
end;
context_dictionaryToCreate.RemovePair(context_w);
end else begin
value := context_dictionary.GetValue(context_w).Value.ToInteger;
for var I10 : integer := 0 to context_numBits-1 do
begin
context_data_val := (context_data_val shl 1) or (value and 1);
if (context_data_position = bitsPerChar-1) then
begin
context_data_position := 0;
context_data.Add(getCharFromInt.Chars[context_data_val]);
context_data_val := 0;
end else
context_data_position := context_data_position +1;
value := value shr 1;
end;
end;
context_enlargeIn := context_enlargeIn-1;
if (context_enlargeIn = 0) then
begin
context_enlargeIn := math.Power(2, context_numBits);
context_numBits := context_numBits +1;
end;
end;
value := 2;
for var I11 : integer := 0 to context_numBits-1 do
begin
context_data_val := (context_data_val shl 1) or (value and 1);
if (context_data_position = bitsPerChar-1) then
begin
context_data_position := 0;
context_data.Add(getCharFromInt.Chars[context_data_val]);
context_data_val := 0;
end else
context_data_position := context_data_position + 1;
value := value shr 1;
end;
while True do
begin
context_data_val := (context_data_val shl 1);
if (context_data_position = bitsPerChar-1) then
begin
context_data.Add(getCharFromInt.Chars[context_data_val]);
break;
end else
context_data_position := context_data_position + 1;
end;
for var I : integer := 0 to context_data.Count -1 do
Result := Result + context_data.Strings[I];
end;
사용법 예
var TmpStr : string := 'TEST';
ShowMessage(_Compress(TmpStr ,6,keyStrUriSafe));
안드로이드에도 적용이 가능하게 해놨습니다
728x90