Skip to content

Commit

Permalink
The only thing we're deflating now is my ego
Browse files Browse the repository at this point in the history
  • Loading branch information
ysthakur committed May 30, 2023
1 parent 76cc0eb commit 2856b03
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 45 deletions.
46 changes: 11 additions & 35 deletions dict_scripts/sort_script.py
@@ -1,20 +1,10 @@
import json
import os
import zlib


# Max length of a Java/Scala string literal
MAX_LEN = 65535


def deflate(data):
compress = zlib.compressobj(level=9)
deflated = compress.compress(data.encode())
deflated += compress.flush()
# Latin-1 will work with every byte value from 0-255 without giving errors
return deflated.decode("latin-1")


def scala_str(s):
"""Turn one of the dictionary strings into a Scala string literal"""
return '"""' + repr(s)[1 : len(s) - 1] + '"""'
Expand All @@ -23,7 +13,6 @@ def scala_str(s):
def chop(s):
"""Chop the dictionary strings into multiple pieces that are each short
enough to be valid Scala string literals"""
s = s.encode("utf-8")
pieces = [s[:MAX_LEN]]
s = s[MAX_LEN:]
while len(s) > MAX_LEN:
Expand All @@ -32,7 +21,7 @@ def chop(s):
# Have to turn it into Seq("foo","bar",...).mkString instead of simply
# "foo"+"bar"+... because the Scala compiler tries to be smart and turn
# it into a single string literal, which is too long
return "Seq(" + ",".join(scala_str(p.decode("utf-8").encode("latin-1").decode("latin-1")) for p in pieces) + ").mkString"
return "Seq(" + ",".join(scala_str(p) for p in pieces) + ").mkString"


def gen(shortlen, longlen):
Expand Down Expand Up @@ -61,30 +50,23 @@ def gen(shortlen, longlen):
root = os.path.dirname(curr_dir)
resources = os.path.join(root, "shared", "src", "main", "resources")

short_str = "\n".join(short)
long_str = "\n".join(long)

with open(
os.path.join(resources, "ShortDictionary.txt"), "w", encoding="utf-8"
) as out:
out.write("\n".join(short))
out.write("\n")
out.write(short_str + "\n")

with open(
os.path.join(resources, "LongDictionary.txt"), "w", encoding="utf-8"
) as out:
out.write("\n".join(long))
out.write("\n")

short_str = "\n".join(short)
long_str = "\n".join(long)
short_deflated = deflate(short_str)
long_deflated = deflate(long_str)

print(f"Concatenated: {len(short_str)} (short), {len(long_str)} (long)")
print(f"Deflated: {len(short_deflated)} (short), {len(long_deflated)} (long)")
out.write(long_str + "\n")

with open(
os.path.join(root, "js", "src", "main", "scala", "Dictionary.scala"),
"w",
encoding="latin-1",
encoding="utf-8",
) as out:
# Scala has a maximum string literal length (because of Java)
out.write(
Expand All @@ -93,16 +75,10 @@ def gen(shortlen, longlen):
// See dict_scripts/{os.path.basename(__file__)}
package vyxal
object Dictionary{{
val shortDictionary=decompress({len(short_str)},{chop(short_deflated)})
val longDictionary=decompress({len(long_str)},{chop(long_deflated)})
def decompress(len: Int, s: String): Seq[String] = {{
val bytes = s.getBytes(java.nio.charset.StandardCharsets.ISO_8859_1)
val inflater = new java.util.zip.Inflater()
inflater.setInput(bytes, 0, bytes.length)
val result = Array.fill[Byte](len)(0)
inflater.inflate(result)
inflater.end()
new String(result, 0, len, "UTF-8").split("\\n").toSeq
val shortDictionary=split({chop(short_str)})
val longDictionary=split({chop(long_str)})
def split(s: String): Seq[String] = {{
s.split("\\n").toSeq
}}
}}
"""
Expand Down

0 comments on commit 2856b03

Please sign in to comment.