-
Notifications
You must be signed in to change notification settings - Fork 323
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a
common-polyglot-core-utils
project (#5855)
Adds a common project that allows sharing code between the `runtime` and `std-bits`. Due to classpath separation and the way it is compiled, the classes will be duplicated - we will have one copy for the `runtime` classpath and another copy as a small JAR for `Standard.Base` library. This is still much better than having the code duplicated - now at least we have a single source of truth for the shared implementations. Due to the copying we should not expand this project too much, but I encourage to put here any methods that would otherwise require us to copy the code itself. This may be a good place to put parts of the hashing logic to then allow sharing the logic between the `runtime` and the `MultiValueKey` in the `Table` library (cc: @Akirathan).
- Loading branch information
Showing
11 changed files
with
151 additions
and
105 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
...mon-polyglot-core-utils/src/main/java/org/enso/polyglot/common_utils/Core_Date_Utils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package org.enso.polyglot.common_utils; | ||
|
||
import java.time.format.DateTimeFormatter; | ||
import java.time.format.DateTimeFormatterBuilder; | ||
|
||
public class Core_Date_Utils { | ||
/** | ||
* Replace space with T in ISO date time string to make it compatible with ISO format. | ||
* @param dateString Raw date time string with either space or T as separator | ||
* @return ISO format date time string | ||
*/ | ||
public static String normaliseISODateTime(String dateString) { | ||
if (dateString != null && dateString.length() > 10 && dateString.charAt(10) == ' ') { | ||
var builder = new StringBuilder(dateString); | ||
builder.replace(10, 11, "T"); | ||
return builder.toString(); | ||
} | ||
|
||
return dateString; | ||
} | ||
|
||
/** @return default Date Time formatter for parsing a Date_Time. */ | ||
public static DateTimeFormatter defaultZonedDateTimeFormatter() { | ||
return new DateTimeFormatterBuilder().append(DateTimeFormatter.ISO_LOCAL_DATE_TIME) | ||
.optionalStart().parseLenient().appendOffsetId().optionalEnd() | ||
.optionalStart().appendLiteral('[').parseCaseSensitive().appendZoneRegionId().appendLiteral(']') | ||
.toFormatter(); | ||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
...mon-polyglot-core-utils/src/main/java/org/enso/polyglot/common_utils/Core_Text_Utils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package org.enso.polyglot.common_utils; | ||
|
||
import com.ibm.icu.text.BreakIterator; | ||
|
||
public class Core_Text_Utils { | ||
/** Computes the length of the string as the number of grapheme clusters it contains. */ | ||
public static int computeGraphemeLength(String text) { | ||
BreakIterator iter = BreakIterator.getCharacterInstance(); | ||
iter.setText(text); | ||
int len = 0; | ||
while (iter.next() != BreakIterator.DONE) { | ||
len++; | ||
} | ||
return len; | ||
} | ||
|
||
/** Pretty prints the string, escaping special characters. */ | ||
public static String prettyPrint(String str) { | ||
int len = str.length(); | ||
int outputLength = len + 2; // Precise if there are no special characters. | ||
|
||
// TODO This should be more extensible; while it's still a small fixed set, | ||
// a switch is probably fastest (unconfirmed) | ||
|
||
StringBuilder sb = new StringBuilder(outputLength); | ||
|
||
sb.append('\''); | ||
|
||
for (int i = 0; i < len; ++i) { | ||
char c = str.charAt(i); | ||
switch (c) { | ||
case '\\' -> sb.append("\\\\"); | ||
case '\'' -> sb.append("\\'"); | ||
case '\n' -> sb.append("\\n"); | ||
case '\t' -> sb.append("\\t"); | ||
case '\0' -> sb.append("\\0"); | ||
case '\u0007' -> sb.append("\\a"); | ||
case '\u0008' -> sb.append("\\b"); | ||
case '\u000c' -> sb.append("\\f"); | ||
case '\r' -> sb.append("\\r"); | ||
case '\u000B' -> sb.append("\\v"); | ||
case '\u001B' -> sb.append("\\e"); | ||
default -> sb.append(c); | ||
} | ||
} | ||
|
||
sb.append('\''); | ||
|
||
return sb.toString(); | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
...common-polyglot-core-utils/src/main/java/org/enso/polyglot/common_utils/package-info.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
/** | ||
* This package contains common utilities which can be used both by the engine runtime and the libraries. | ||
* <p> | ||
* This allows us to avoid duplicating code between the runtime and library projects for operations that need to be | ||
* accessible on both sides. | ||
* <p> | ||
* The utilities that belong here are mostly operations that are builtins of the Enso language but also need to be used | ||
* from our Java libraries where the cost of calling back to Enso methods is relatively high, so accessing the Java | ||
* implementations directly is desirable. The primary example of that is the algorithm for computing the length of a | ||
* string by counting the grapheme clusters. | ||
* <p> | ||
* Due to classpath separation, the class files of this package will be duplicated with one copy embedded in the engine | ||
* and another attached as `common-polyglot-core-utils.jar` placed in the `polyglot` directory of the Standard.Base | ||
* library. But it allows us to avoid duplicating the code, so we can have a single source of truth for each | ||
* implementation. | ||
* <p> | ||
* Due to the copying, the project should not be expanded too much, but all utilities which would end up being | ||
* duplicated are best moved here. | ||
*/ | ||
package org.enso.polyglot.common_utils; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.