From aaa5cc1cc7cb44cba01e3a3c844e14dfc47ce470 Mon Sep 17 00:00:00 2001 From: Jim Gay Date: Tue, 24 Dec 2024 16:00:15 -0500 Subject: [PATCH] Add OrdString method to coerce objects This will avoid extra allocations in the scanner. --- CHANGELOG | 2 ++ lib/radius/ord_string.rb | 8 ++++++++ lib/radius/parser/scanner.rb | 4 ++-- 3 files changed, 12 insertions(+), 2 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index b10cb7e..09afbb9 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -2,6 +2,8 @@ == Unreleased +* Add OrdString coercion method to avoid extra allocations [saturnflyer] + == 0.8.0 * Ensure that the DelegatingOpenStruct is a true copy of the original [saturnflyer] * Avoid errors with frozen strings in Ruby 3.4+ [jeremyevans] diff --git a/lib/radius/ord_string.rb b/lib/radius/ord_string.rb index 12a1bfb..e311ed7 100644 --- a/lib/radius/ord_string.rb +++ b/lib/radius/ord_string.rb @@ -1,4 +1,12 @@ module Radius + module_function def OrdString(string_or_ord_string) + if string_or_ord_string.is_a?(OrdString) + string_or_ord_string + else + OrdString.new(string_or_ord_string) + end + end + class OrdString < String def [](*args) if args.size == 1 && args.first.is_a?(Integer) diff --git a/lib/radius/parser/scanner.rb b/lib/radius/parser/scanner.rb index fc4248c..2c96a28 100644 --- a/lib/radius/parser/scanner.rb +++ b/lib/radius/parser/scanner.rb @@ -10,7 +10,7 @@ def scanner_regex(prefix = nil) # Parses a given string and returns an array of nodes. # The nodes consist of strings and hashes that describe a Radius tag that was found. def operate(prefix, data) - data = Radius::OrdString.new data + data = Radius::OrdString(data) @nodes = [''] re = scanner_regex(prefix) @@ -55,4 +55,4 @@ def parse_attributes(text) # :nodoc: attr end end -end \ No newline at end of file +end