{"id":19353613,"url":"https://github.com/benwiley4000/string-length-the-hard-way","last_synced_at":"2025-07-06T22:09:50.483Z","repository":{"id":150523066,"uuid":"188730745","full_name":"benwiley4000/string-length-the-hard-way","owner":"benwiley4000","description":"What if you wanted to get the length of a string in Lua.. the hard way?","archived":false,"fork":false,"pushed_at":"2019-05-26T21:00:44.000Z","size":2,"stargazers_count":1,"open_issues_count":0,"forks_count":1,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-02-24T10:49:31.375Z","etag":null,"topics":["lua","pico-8","pico8","string"],"latest_commit_sha":null,"homepage":"","language":null,"has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/benwiley4000.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-05-26T20:55:42.000Z","updated_at":"2020-09-14T03:11:38.000Z","dependencies_parsed_at":"2023-07-29T07:00:54.624Z","dependency_job_id":null,"html_url":"https://github.com/benwiley4000/string-length-the-hard-way","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/benwiley4000/string-length-the-hard-way","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/benwiley4000%2Fstring-length-the-hard-way","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/benwiley4000%2Fstring-length-the-hard-way/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/benwiley4000%2Fstring-length-the-hard-way/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/benwiley4000%2Fstring-length-the-hard-way/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/benwiley4000","download_url":"https://codeload.github.com/benwiley4000/string-length-the-hard-way/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/benwiley4000%2Fstring-length-the-hard-way/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":263980177,"owners_count":23538922,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["lua","pico-8","pico8","string"],"created_at":"2024-11-10T04:43:30.992Z","updated_at":"2025-07-06T22:09:50.478Z","avatar_url":"https://github.com/benwiley4000.png","language":null,"funding_links":[],"categories":[],"sub_categories":[],"readme":"# string-length-the-hard-way\nWhat if you wanted to get the length of a string in Lua.. without using the `string.len` function or the length (`#`) operator? And make it run in either the standard Lua interpeter or in PICO-8?\n\nDon't ask me why I tried to do this...\n\n```lua\n-- PICO-8 and Standard Lua lib compatibility\nlocal _sub = string and string.sub or sub\nlocal _flr = math and math.floor or flr\n\nfunction string_length(str)\n  -- we have undefined behavior if the\n  -- data size is larger than this\n  -- (max number in PICO-8)\n  local search_max = 32767\n  local search_min = 1\n  -- binary search for index before empty string\n  while true do\n    local i = search_min + _flr((search_max + 1 - search_min) / 2)\n    local char = _sub(str, i, i)\n    if char == \"\" then\n      search_max = i - 1\n    else\n      local after = i + 1\n      local char_after = _sub(str, after, after)\n      if char_after == \"\" then\n        return i\n      end\n      search_min = after\n    end\n  end\nend\n```\n\nAnd it works!\n\n```lua\nstring_length(\"hello world\") -- 11\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbenwiley4000%2Fstring-length-the-hard-way","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbenwiley4000%2Fstring-length-the-hard-way","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbenwiley4000%2Fstring-length-the-hard-way/lists"}