{"record":{"id":"f4edf2c961d87f9a","repo":"egametang/ET","slug":"string-mode-0-strtext-mode","errorCode":null,"errorMessage":"string mode < 0: {strText} {mode}","messagePattern":"string mode < 0: (.+?) (.+?)","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"Packages/cn.etetet.core/Scripts/Core/Share/Helper/StringHashHelper.cs","lineNumber":29,"sourceCode":"            const uint seed = 1313; // 31 131 1313 13131 131313 etc..\n            \n            ulong hash = 0;\n            for (int i = 0; i < str.Length; ++i)\n            {\n                char c = str[i];\n                byte high = (byte)(c >> 8);\n                byte low = (byte)(c & byte.MaxValue);\n                hash = hash * seed + high;\n                hash = hash * seed + low;\n            }\n            return (long)hash;\n        }\n\n        public static int Mode(this string strText, int mode)\n        {\n            if (mode <= 0)\n            {\n                throw new Exception($\"string mode < 0: {strText} {mode}\");\n            }\n            return (int)((ulong)strText.GetLongHashCode() % (uint)mode);\n        }\n    }\n}","sourceCodeStart":11,"sourceCodeEnd":34,"githubUrl":"https://github.com/egametang/ET/blob/5cab01f7a8bee5f49f4781eebe9e2b1c6d7ebe0f/Packages/cn.etetet.core/Scripts/Core/Share/Helper/StringHashHelper.cs#L11-L34","documentation":"StringHashHelper.Mode(string, int) computes strText.GetLongHashCode() % mode to pick a bucket. It throws when mode is zero or negative because modulo by non-positive is undefined, so the bucket count must be at least one.","triggerScenarios":"Calling text.Mode(count) where count is 0 (empty collection) or negative, e.g. sharding across servers when the server list is empty, or routing by a hash bucket sized from an uninitialized config value.","commonSituations":"Server/cluster list empty at startup, a config count field left at 0, or a list filtered down to zero elements before being used as the mode.","solutions":["Guard callers: only call Mode when the bucket count is >= 1.","Fix the data source feeding mode (config, server count) so it is never 0.","Prefer throwing an ArgumentException with the parameter name for a usage contract."],"exampleFix":"// before\nint bucket = key.Mode(servers.Count); // throws when Count==0\n// after\nif (servers.Count <= 0) throw new InvalidOperationException(\"no servers to route to\");\nint bucket = key.Mode(servers.Count);","handlingStrategy":"validation","validationCode":"if (string.IsNullOrEmpty(text)) throw new ArgumentException(nameof(text));\nif (mode <= 0) throw new ArgumentOutOfRangeException(nameof(mode), \"mode must be >= 1\");\nint bucket = text.Mode(mode);","typeGuard":"public static bool IsValidMode(int mode) => mode > 0;","tryCatchPattern":"null","preventionTips":["Treat mode as a count: never pass an empty collection's Count.","Default config counts to 1, never 0.","Guard at the routing layer where the server/bucket list is built."],"tags":["string","hashing","argument","csharp"],"backgroundTag":null,"analyzedSha":"5cab01f7a8bee5f49f4781eebe9e2b1c6d7ebe0f","analyzedAt":"2026-08-13T21:10:40.377Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}