Skip to content

Commit

Permalink
Don't use regular expressions
Browse files Browse the repository at this point in the history
This is both faster and simpler. I don't know if the speedup came from avoiding regular expressions or avoiding strings.
  • Loading branch information
tfausak committed Nov 6, 2016
1 parent 36284d3 commit 8c314a1
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 15 deletions.
29 changes: 20 additions & 9 deletions library/Rattletrap/ClassAttributeMap.hs
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,17 @@ import Rattletrap.Cache
import Rattletrap.ClassMapping
import Rattletrap.CompressedWord
import Rattletrap.Data
import Rattletrap.Int32
import Rattletrap.List
import Rattletrap.Text
import Rattletrap.Utility
import Rattletrap.Word32

import qualified Data.Bimap as Bimap
import qualified Data.List as List
import qualified Data.Map as Map
import qualified Data.Maybe as Maybe
import qualified Data.Set as Set
import qualified Data.Text as Text

data ClassAttributeMap = ClassAttributeMap
{ classAttributeMapObjectMap :: Map.Map Word32 Text
Expand Down Expand Up @@ -176,14 +177,24 @@ getClassName rawObjectName = Map.lookup (normalizeObjectName rawObjectName) obje

normalizeObjectName :: Text -> Text
normalizeObjectName objectName =
stringToText
(replace
"_[0-9]+$"
""
(replace
"^[A-Z_a-z0-9]+[.]TheWorld:"
"TheWorld:"
(textToString objectName)))
let name = textValue objectName
crowdActor = Text.pack "TheWorld:PersistentLevel.CrowdActor_TA"
crowdManager = Text.pack "TheWorld:PersistentLevel.CrowdManager_TA"
boostPickup = Text.pack "TheWorld:PersistentLevel.VehiclePickup_Boost_TA"
mapScoreboard = Text.pack "TheWorld:PersistentLevel.InMapScoreboard_TA"
toText text =
Text
(Int32 (fromIntegral (Text.length text + 1)))
(Text.snoc text '\x00')
in if Text.isInfixOf crowdActor name
then toText crowdActor
else if Text.isInfixOf crowdManager name
then toText crowdManager
else if Text.isInfixOf boostPickup name
then toText boostPickup
else if Text.isInfixOf mapScoreboard name
then toText mapScoreboard
else objectName

objectClasses :: Map.Map Text Text
objectClasses =
Expand Down
5 changes: 0 additions & 5 deletions library/Rattletrap/Utility.hs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import qualified Data.Binary.Bits.Get as BinaryBit
import qualified Data.Bits as Bits
import qualified Data.ByteString.Lazy as ByteString
import qualified Data.Word as Word
import qualified Text.Regex as Regex

getRemainingBits :: BinaryBit.BitGet [Bool]
getRemainingBits = do
Expand All @@ -25,10 +24,6 @@ padBytes size bytes =
, ByteString.replicate (fromIntegral size - ByteString.length bytes) 0x00
]

replace :: String -> String -> String -> String
replace needle replacement haystack =
Regex.subRegex (Regex.mkRegex needle) haystack replacement

reverseByte :: Word.Word8 -> Word.Word8
reverseByte byte =
Bits.shiftR (byte Bits..&. Bits.bit 7) 7 +
Expand Down
1 change: 0 additions & 1 deletion package.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ library:
- bytestring >=0.10 && <0.11
- containers >=0.5 && <0.6
- data-binary-ieee754 >=0.4 && <0.5
- regex-compat >=0.95 && <0.96
- text >=1.2 && <1.3
- vector >=0.11 && <0.12
other-modules: Paths_rattletrap
Expand Down

0 comments on commit 8c314a1

Please sign in to comment.