Skip to content

Commit f364a67

Browse files
Copilothossain-khan
andcommitted
Fix code formatting and linting issues after performance optimizations
Co-authored-by: hossain-khan <[email protected]>
1 parent 77a7537 commit f364a67

File tree

3 files changed

+32
-34
lines changed

3 files changed

+32
-34
lines changed

lib/src/main/kotlin/dev/hossain/json5kt/JSON5Lexer.kt

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package dev.hossain.json5kt
22

3-
import kotlin.math.pow
4-
53
/**
64
* Lexer for JSON5 syntax
75
* Breaks JSON5 text into tokens for the parser
@@ -297,7 +295,7 @@ class JSON5Lexer(
297295
/**
298296
* Optimized string reading with pre-sized buffer and efficient escape handling.
299297
* Performance improvements:
300-
* - Pre-sized StringBuilder to reduce allocations
298+
* - Pre-sized StringBuilder to reduce allocations
301299
* - Fast path for strings without escapes (but maintains position tracking accuracy)
302300
* - Optimized escape sequence processing
303301
*/
@@ -661,7 +659,7 @@ class JSON5Lexer(
661659
// Handle decimal notation - optimized with pre-sizing
662660
// Estimate capacity based on typical number lengths (reduces allocations)
663661
val buffer = StringBuilder(16)
664-
662+
665663
if (isNegative) {
666664
buffer.append('-')
667665
}
@@ -735,7 +733,7 @@ class JSON5Lexer(
735733
private fun parseHexToDouble(hexStr: String): Double {
736734
// Fast path for empty/invalid input
737735
if (hexStr.isEmpty()) return 0.0
738-
736+
739737
// Fast path for small hex numbers (most common case)
740738
// Can represent up to 15 hex digits precisely in a Long
741739
if (hexStr.length <= 15) {
@@ -752,7 +750,7 @@ class JSON5Lexer(
752750
var result = 0.0
753751
val len = hexStr.length
754752
var power = 1.0
755-
753+
756754
// Process from right to left in 8-digit chunks to minimize allocations
757755
var end = len
758756
while (end > 0) {
@@ -762,7 +760,7 @@ class JSON5Lexer(
762760
power *= 4294967296.0 // 16^8 as constant (0x100000000)
763761
end = start
764762
}
765-
763+
766764
return result
767765
} catch (e: NumberFormatException) {
768766
// Fallback for very large numbers - simplified approach
@@ -779,7 +777,7 @@ class JSON5Lexer(
779777
*/
780778
private fun readIdentifier(): Token {
781779
val startColumn = column
782-
780+
783781
// Pre-size buffer for typical identifier length
784782
val buffer = StringBuilder(16)
785783

@@ -806,7 +804,7 @@ class JSON5Lexer(
806804
}
807805

808806
advance() // Skip 'u'
809-
807+
810808
// Read 4 hex digits directly without StringBuilder for better performance
811809
var hexValue = 0
812810
repeat(4) {
@@ -832,7 +830,7 @@ class JSON5Lexer(
832830
}
833831

834832
val ident = buffer.toString()
835-
833+
836834
// Fast literal matching using when expression (more efficient than multiple if conditions)
837835
return when (ident) {
838836
"true" -> Token.BooleanToken(true, line, startColumn)

lib/src/main/kotlin/dev/hossain/json5kt/JSON5Parser.kt

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -155,19 +155,20 @@ internal object JSON5Parser {
155155

156156
while (true) {
157157
// Parse the property name with optimized token handling
158-
val key = when (token.type) {
159-
TokenType.STRING -> (token as Token.StringToken).stringValue
160-
TokenType.IDENTIFIER -> (token as Token.IdentifierToken).identifierValue
161-
TokenType.PUNCTUATOR -> {
162-
if ((token as Token.PunctuatorToken).punctuator == "}") {
163-
break // This is for handling empty objects or trailing commas
164-
} else {
165-
throw JSON5Exception("Expected property name or '}'", token.line, token.column)
158+
val key =
159+
when (token.type) {
160+
TokenType.STRING -> (token as Token.StringToken).stringValue
161+
TokenType.IDENTIFIER -> (token as Token.IdentifierToken).identifierValue
162+
TokenType.PUNCTUATOR -> {
163+
if ((token as Token.PunctuatorToken).punctuator == "}") {
164+
break // This is for handling empty objects or trailing commas
165+
} else {
166+
throw JSON5Exception("Expected property name or '}'", token.line, token.column)
167+
}
166168
}
169+
TokenType.EOF -> throw JSON5Exception.invalidEndOfInput(token.line, token.column)
170+
else -> throw JSON5Exception("Expected property name or '}'", token.line, token.column)
167171
}
168-
TokenType.EOF -> throw JSON5Exception.invalidEndOfInput(token.line, token.column)
169-
else -> throw JSON5Exception("Expected property name or '}'", token.line, token.column)
170-
}
171172

172173
// Expect a colon - optimized token handling
173174
token = lexer.nextToken()
@@ -215,9 +216,6 @@ internal object JSON5Parser {
215216
return result
216217
}
217218

218-
/**
219-
* Optimized array parsing with efficient list allocation.
220-
*/
221219
/**
222220
* Highly optimized array parsing with reduced allocations and faster token processing.
223221
* Performance improvements:

lib/src/main/kotlin/dev/hossain/json5kt/JSON5Serializer.kt

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -180,19 +180,21 @@ internal object JSON5Serializer {
180180
val propValue = serializeValue(value, newIndent)
181181

182182
// Build property string more efficiently
183-
val property = if (gap.isNotEmpty()) {
184-
"$linePrefix$propName$colonSeparator$propValue"
185-
} else {
186-
"$propName$colonSeparator$propValue"
187-
}
183+
val property =
184+
if (gap.isNotEmpty()) {
185+
"$linePrefix$propName$colonSeparator$propValue"
186+
} else {
187+
"$propName$colonSeparator$propValue"
188+
}
188189
properties.add(property)
189190
}
190191

191-
val joined = if (gap.isNotEmpty()) {
192-
properties.joinToString(",\n")
193-
} else {
194-
properties.joinToString(",")
195-
}
192+
val joined =
193+
if (gap.isNotEmpty()) {
194+
properties.joinToString(",\n")
195+
} else {
196+
properties.joinToString(",")
197+
}
196198

197199
stack.removeAt(stack.size - 1)
198200

0 commit comments

Comments
 (0)