Skip to content

Instantly share code, notes, and snippets.

@cstockton
Last active February 12, 2018 05:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cstockton/24b02c7fad2e0ed6a5f8c273cd9678ae to your computer and use it in GitHub Desktop.
Save cstockton/24b02c7fad2e0ed6a5f8c273cd9678ae to your computer and use it in GitHub Desktop.
// +build go1.10
package strutil
type Builder -> strings.Builder
// Copyright 2017 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Please see the license file inline:
//
// Copyright (c) 2009 The Go Authors. All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
// * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// +build !go1.10
package strutil
import (
"unicode/utf8"
"unsafe"
)
// A Builder is used to efficiently build a string using Write methods.
// It minimizes memory copying. The zero value is ready to use.
// Do not copy a non-zero Builder.
type Builder struct {
addr *Builder // of receiver, to detect copies by value
buf []byte
}
// noescape hides a pointer from escape analysis. noescape is
// the identity function but escape analysis doesn't think the
// output depends on the input. noescape is inlined and currently
// compiles down to zero instructions.
// USE CAREFULLY!
// This was copied from the runtime; see issues 23382 and 7921.
//go:nosplit
func noescape(p unsafe.Pointer) unsafe.Pointer {
x := uintptr(p)
return unsafe.Pointer(x ^ 0)
}
func (b *Builder) copyCheck() {
if b.addr == nil {
// This hack works around a failing of Go's escape analysis
// that was causing b to escape and be heap allocated.
// See issue 23382.
// TODO: once issue 7921 is fixed, this should be reverted to
// just "b.addr = b".
b.addr = (*Builder)(noescape(unsafe.Pointer(b)))
} else if b.addr != b {
panic("strings: illegal use of non-zero Builder copied by value")
}
}
// String returns the accumulated string.
func (b *Builder) String() string {
return *(*string)(unsafe.Pointer(&b.buf))
}
// Len returns the number of accumulated bytes; b.Len() == len(b.String()).
func (b *Builder) Len() int { return len(b.buf) }
// Reset resets the Builder to be empty.
func (b *Builder) Reset() {
b.addr = nil
b.buf = nil
}
// grow copies the buffer to a new, larger buffer so that there are at least n
// bytes of capacity beyond len(b.buf).
func (b *Builder) grow(n int) {
buf := make([]byte, len(b.buf), 2*cap(b.buf)+n)
copy(buf, b.buf)
b.buf = buf
}
// Grow grows b's capacity, if necessary, to guarantee space for
// another n bytes. After Grow(n), at least n bytes can be written to b
// without another allocation. If n is negative, Grow panics.
func (b *Builder) Grow(n int) {
b.copyCheck()
if n < 0 {
panic("strings.Builder.Grow: negative count")
}
if cap(b.buf)-len(b.buf) < n {
b.grow(n)
}
}
// Write appends the contents of p to b's buffer.
// Write always returns len(p), nil.
func (b *Builder) Write(p []byte) (int, error) {
b.copyCheck()
b.buf = append(b.buf, p...)
return len(p), nil
}
// WriteByte appends the byte c to b's buffer.
// The returned error is always nil.
func (b *Builder) WriteByte(c byte) error {
b.copyCheck()
b.buf = append(b.buf, c)
return nil
}
// WriteRune appends the UTF-8 encoding of Unicode code point r to b's buffer.
// It returns the length of r and a nil error.
func (b *Builder) WriteRune(r rune) (int, error) {
b.copyCheck()
if r < utf8.RuneSelf {
b.buf = append(b.buf, byte(r))
return 1, nil
}
l := len(b.buf)
if cap(b.buf)-l < utf8.UTFMax {
b.grow(utf8.UTFMax)
}
n := utf8.EncodeRune(b.buf[l:l+utf8.UTFMax], r)
b.buf = b.buf[:l+n]
return n, nil
}
// WriteString appends the contents of s to b's buffer.
// It returns the length of s and a nil error.
func (b *Builder) WriteString(s string) (int, error) {
b.copyCheck()
b.buf = append(b.buf, s...)
return len(s), nil
}
// experiment with cheaper stack traces.
type errorStackLazy struct {
err error
n int
pcs [32]uintptr
}
func newStackLazy(skip int, err error) error {
esl := new(errorStackLazy)
n := runtime.Callers(1+skip, esl.pcs[:])
if n == 0 {
return &emptyStack{err: err}
}
esl.n = n
return esl
}
func (e *errorStackLazy) Error() string {
return e.err.Error()
}
func (e *errorStackLazy) Format(st fmt.State, verb rune) {
frs := make([]runtime.Frame, 0, e.n)
cf := runtime.CallersFrames(e.pcs[:e.n])
for {
fr, ok := cf.Next()
if !ok {
break
}
if fr.Function == `` {
fr.Function = `unknown`
}
frs = append(frs, fr)
}
formatFrames(frs, st, verb)
}
func formatFrames(frs []runtime.Frame, st fmt.State, verb rune) {
var (
b strutil.Builder
n int
)
for _, l := range frs {
n += len(l.File)
}
b.Grow(n * 2)
switch {
case verb == 's':
for _, fr := range frs {
b.WriteByte('\n')
formatFrame(fr, &b, st, verb)
}
case verb == 'v':
switch {
case st.Flag('+'):
for _, fr := range frs {
b.WriteByte('\n')
formatFrame(fr, &b, st, verb)
}
}
}
fmt.Fprint(st, b.String())
}
func formatFrame(fr runtime.Frame, b *strutil.Builder, s fmt.State, verb rune) {
switch verb {
case 's':
switch {
case s.Flag('+'):
b.WriteString(fr.Function)
b.WriteString("\n\t")
b.WriteString(fr.File)
default:
b.WriteString(path.Base(fr.File))
}
case 'd':
b.WriteString(strutil.Itoa(fr.Line))
case 'n':
b.WriteString(fr.Function)
case 'v':
b.WriteString(fr.Function)
b.WriteString("\n\t")
b.WriteString(fr.File)
b.WriteByte(':')
b.WriteString(strutil.Itoa(fr.Line))
}
}
package strutil
import (
"strconv"
)
// Itoa is for zero-allocation Itoa for numbers under 10k, useful when it comes
// to formatting stack traces which may have many line numbers under 10k.
func Itoa(n int) string {
switch {
case n < 0:
fallthrough
default:
return strconv.Itoa(n)
case n < 10:
n = n * 2
return intStrLut2[n+1 : n+2]
case n < 100:
n = n % 100 * 2
return intStrLut2[n : n+2]
case n < 1000:
n = (n - 100) * 3
return intStrLut3[n : n+3]
case n < 10000:
n = (n - 1000) * 4
return intStrLut4[n : n+4]
}
}
var (
intStrLut2 = "00010203040506070809101112131415161718192021" +
"22232425262728293031323334353637383940414243444546474849" +
"50515253545556575859606162636465666768697071727374757677" +
"78798081828384858687888990919293949596979899"
intStrLut3 = "100101102103104105106107108109110111112113114115116" +
"117118119120121122123124125126127128129130131132133134135136137" +
"138139140141142143144145146147148149150151152153154155156157158" +
"159160161162163164165166167168169170171172173174175176177178179" +
"180181182183184185186187188189190191192193194195196197198199200" +
"201202203204205206207208209210211212213214215216217218219220221" +
"222223224225226227228229230231232233234235236237238239240241242" +
"243244245246247248249250251252253254255256257258259260261262263" +
"264265266267268269270271272273274275276277278279280281282283284" +
"285286287288289290291292293294295296297298299300301302303304305" +
"306307308309310311312313314315316317318319320321322323324325326" +
"327328329330331332333334335336337338339340341342343344345346347" +
"348349350351352353354355356357358359360361362363364365366367368" +
"369370371372373374375376377378379380381382383384385386387388389" +
"390391392393394395396397398399400401402403404405406407408409410" +
"411412413414415416417418419420421422423424425426427428429430431" +
"432433434435436437438439440441442443444445446447448449450451452" +
"453454455456457458459460461462463464465466467468469470471472473" +
"474475476477478479480481482483484485486487488489490491492493494" +
"495496497498499500501502503504505506507508509510511512513514515" +
"516517518519520521522523524525526527528529530531532533534535536" +
"537538539540541542543544545546547548549550551552553554555556557" +
"558559560561562563564565566567568569570571572573574575576577578" +
"579580581582583584585586587588589590591592593594595596597598599" +
"600601602603604605606607608609610611612613614615616617618619620" +
"621622623624625626627628629630631632633634635636637638639640641" +
"642643644645646647648649650651652653654655656657658659660661662" +
"663664665666667668669670671672673674675676677678679680681682683" +
"684685686687688689690691692693694695696697698699700701702703704" +
"705706707708709710711712713714715716717718719720721722723724725" +
"726727728729730731732733734735736737738739740741742743744745746" +
"747748749750751752753754755756757758759760761762763764765766767" +
"768769770771772773774775776777778779780781782783784785786787788" +
"789790791792793794795796797798799800801802803804805806807808809" +
"810811812813814815816817818819820821822823824825826827828829830" +
"831832833834835836837838839840841842843844845846847848849850851" +
"852853854855856857858859860861862863864865866867868869870871872" +
"873874875876877878879880881882883884885886887888889890891892893" +
"894895896897898899900901902903904905906907908909910911912913914" +
"915916917918919920921922923924925926927928929930931932933934935" +
"936937938939940941942943944945946947948949950951952953954955956" +
"957958959960961962963964965966967968969970971972973974975976977" +
"978979980981982983984985986987988989990991992993994995996997998999"
intStrLut4 = "1000100110021003100410051006100710081009101010111012" +
"10131014101510161017101810191020102110221023102410251026102710281029" +
"10301031103210331034103510361037103810391040104110421043104410451046" +
"10471048104910501051105210531054105510561057105810591060106110621063" +
"10641065106610671068106910701071107210731074107510761077107810791080" +
"10811082108310841085108610871088108910901091109210931094109510961097" +
"10981099110011011102110311041105110611071108110911101111111211131114" +
"11151116111711181119112011211122112311241125112611271128112911301131" +
"11321133113411351136113711381139114011411142114311441145114611471148" +
"11491150115111521153115411551156115711581159116011611162116311641165" +
"11661167116811691170117111721173117411751176117711781179118011811182" +
"11831184118511861187118811891190119111921193119411951196119711981199" +
"12001201120212031204120512061207120812091210121112121213121412151216" +
"12171218121912201221122212231224122512261227122812291230123112321233" +
"12341235123612371238123912401241124212431244124512461247124812491250" +
"12511252125312541255125612571258125912601261126212631264126512661267" +
"12681269127012711272127312741275127612771278127912801281128212831284" +
"12851286128712881289129012911292129312941295129612971298129913001301" +
"13021303130413051306130713081309131013111312131313141315131613171318" +
"13191320132113221323132413251326132713281329133013311332133313341335" +
"13361337133813391340134113421343134413451346134713481349135013511352" +
"13531354135513561357135813591360136113621363136413651366136713681369" +
"13701371137213731374137513761377137813791380138113821383138413851386" +
"13871388138913901391139213931394139513961397139813991400140114021403" +
"14041405140614071408140914101411141214131414141514161417141814191420" +
"14211422142314241425142614271428142914301431143214331434143514361437" +
"14381439144014411442144314441445144614471448144914501451145214531454" +
"14551456145714581459146014611462146314641465146614671468146914701471" +
"14721473147414751476147714781479148014811482148314841485148614871488" +
"14891490149114921493149414951496149714981499150015011502150315041505" +
"15061507150815091510151115121513151415151516151715181519152015211522" +
"15231524152515261527152815291530153115321533153415351536153715381539" +
"15401541154215431544154515461547154815491550155115521553155415551556" +
"15571558155915601561156215631564156515661567156815691570157115721573" +
"15741575157615771578157915801581158215831584158515861587158815891590" +
"15911592159315941595159615971598159916001601160216031604160516061607" +
"16081609161016111612161316141615161616171618161916201621162216231624" +
"16251626162716281629163016311632163316341635163616371638163916401641" +
"16421643164416451646164716481649165016511652165316541655165616571658" +
"16591660166116621663166416651666166716681669167016711672167316741675" +
"16761677167816791680168116821683168416851686168716881689169016911692" +
"16931694169516961697169816991700170117021703170417051706170717081709" +
"17101711171217131714171517161717171817191720172117221723172417251726" +
"17271728172917301731173217331734173517361737173817391740174117421743" +
"17441745174617471748174917501751175217531754175517561757175817591760" +
"17611762176317641765176617671768176917701771177217731774177517761777" +
"17781779178017811782178317841785178617871788178917901791179217931794" +
"17951796179717981799180018011802180318041805180618071808180918101811" +
"18121813181418151816181718181819182018211822182318241825182618271828" +
"18291830183118321833183418351836183718381839184018411842184318441845" +
"18461847184818491850185118521853185418551856185718581859186018611862" +
"18631864186518661867186818691870187118721873187418751876187718781879" +
"18801881188218831884188518861887188818891890189118921893189418951896" +
"18971898189919001901190219031904190519061907190819091910191119121913" +
"19141915191619171918191919201921192219231924192519261927192819291930" +
"19311932193319341935193619371938193919401941194219431944194519461947" +
"19481949195019511952195319541955195619571958195919601961196219631964" +
"19651966196719681969197019711972197319741975197619771978197919801981" +
"19821983198419851986198719881989199019911992199319941995199619971998" +
"19992000200120022003200420052006200720082009201020112012201320142015" +
"20162017201820192020202120222023202420252026202720282029203020312032" +
"20332034203520362037203820392040204120422043204420452046204720482049" +
"20502051205220532054205520562057205820592060206120622063206420652066" +
"20672068206920702071207220732074207520762077207820792080208120822083" +
"20842085208620872088208920902091209220932094209520962097209820992100" +
"21012102210321042105210621072108210921102111211221132114211521162117" +
"21182119212021212122212321242125212621272128212921302131213221332134" +
"21352136213721382139214021412142214321442145214621472148214921502151" +
"21522153215421552156215721582159216021612162216321642165216621672168" +
"21692170217121722173217421752176217721782179218021812182218321842185" +
"21862187218821892190219121922193219421952196219721982199220022012202" +
"22032204220522062207220822092210221122122213221422152216221722182219" +
"22202221222222232224222522262227222822292230223122322233223422352236" +
"22372238223922402241224222432244224522462247224822492250225122522253" +
"22542255225622572258225922602261226222632264226522662267226822692270" +
"22712272227322742275227622772278227922802281228222832284228522862287" +
"22882289229022912292229322942295229622972298229923002301230223032304" +
"23052306230723082309231023112312231323142315231623172318231923202321" +
"23222323232423252326232723282329233023312332233323342335233623372338" +
"23392340234123422343234423452346234723482349235023512352235323542355" +
"23562357235823592360236123622363236423652366236723682369237023712372" +
"23732374237523762377237823792380238123822383238423852386238723882389" +
"23902391239223932394239523962397239823992400240124022403240424052406" +
"24072408240924102411241224132414241524162417241824192420242124222423" +
"24242425242624272428242924302431243224332434243524362437243824392440" +
"24412442244324442445244624472448244924502451245224532454245524562457" +
"24582459246024612462246324642465246624672468246924702471247224732474" +
"24752476247724782479248024812482248324842485248624872488248924902491" +
"24922493249424952496249724982499250025012502250325042505250625072508" +
"25092510251125122513251425152516251725182519252025212522252325242525" +
"25262527252825292530253125322533253425352536253725382539254025412542" +
"25432544254525462547254825492550255125522553255425552556255725582559" +
"25602561256225632564256525662567256825692570257125722573257425752576" +
"25772578257925802581258225832584258525862587258825892590259125922593" +
"25942595259625972598259926002601260226032604260526062607260826092610" +
"26112612261326142615261626172618261926202621262226232624262526262627" +
"26282629263026312632263326342635263626372638263926402641264226432644" +
"26452646264726482649265026512652265326542655265626572658265926602661" +
"26622663266426652666266726682669267026712672267326742675267626772678" +
"26792680268126822683268426852686268726882689269026912692269326942695" +
"26962697269826992700270127022703270427052706270727082709271027112712" +
"27132714271527162717271827192720272127222723272427252726272727282729" +
"27302731273227332734273527362737273827392740274127422743274427452746" +
"27472748274927502751275227532754275527562757275827592760276127622763" +
"27642765276627672768276927702771277227732774277527762777277827792780" +
"27812782278327842785278627872788278927902791279227932794279527962797" +
"27982799280028012802280328042805280628072808280928102811281228132814" +
"28152816281728182819282028212822282328242825282628272828282928302831" +
"28322833283428352836283728382839284028412842284328442845284628472848" +
"28492850285128522853285428552856285728582859286028612862286328642865" +
"28662867286828692870287128722873287428752876287728782879288028812882" +
"28832884288528862887288828892890289128922893289428952896289728982899" +
"29002901290229032904290529062907290829092910291129122913291429152916" +
"29172918291929202921292229232924292529262927292829292930293129322933" +
"29342935293629372938293929402941294229432944294529462947294829492950" +
"29512952295329542955295629572958295929602961296229632964296529662967" +
"29682969297029712972297329742975297629772978297929802981298229832984" +
"29852986298729882989299029912992299329942995299629972998299930003001" +
"30023003300430053006300730083009301030113012301330143015301630173018" +
"30193020302130223023302430253026302730283029303030313032303330343035" +
"30363037303830393040304130423043304430453046304730483049305030513052" +
"30533054305530563057305830593060306130623063306430653066306730683069" +
"30703071307230733074307530763077307830793080308130823083308430853086" +
"30873088308930903091309230933094309530963097309830993100310131023103" +
"31043105310631073108310931103111311231133114311531163117311831193120" +
"31213122312331243125312631273128312931303131313231333134313531363137" +
"31383139314031413142314331443145314631473148314931503151315231533154" +
"31553156315731583159316031613162316331643165316631673168316931703171" +
"31723173317431753176317731783179318031813182318331843185318631873188" +
"31893190319131923193319431953196319731983199320032013202320332043205" +
"32063207320832093210321132123213321432153216321732183219322032213222" +
"32233224322532263227322832293230323132323233323432353236323732383239" +
"32403241324232433244324532463247324832493250325132523253325432553256" +
"32573258325932603261326232633264326532663267326832693270327132723273" +
"32743275327632773278327932803281328232833284328532863287328832893290" +
"32913292329332943295329632973298329933003301330233033304330533063307" +
"33083309331033113312331333143315331633173318331933203321332233233324" +
"33253326332733283329333033313332333333343335333633373338333933403341" +
"33423343334433453346334733483349335033513352335333543355335633573358" +
"33593360336133623363336433653366336733683369337033713372337333743375" +
"33763377337833793380338133823383338433853386338733883389339033913392" +
"33933394339533963397339833993400340134023403340434053406340734083409" +
"34103411341234133414341534163417341834193420342134223423342434253426" +
"34273428342934303431343234333434343534363437343834393440344134423443" +
"34443445344634473448344934503451345234533454345534563457345834593460" +
"34613462346334643465346634673468346934703471347234733474347534763477" +
"34783479348034813482348334843485348634873488348934903491349234933494" +
"34953496349734983499350035013502350335043505350635073508350935103511" +
"35123513351435153516351735183519352035213522352335243525352635273528" +
"35293530353135323533353435353536353735383539354035413542354335443545" +
"35463547354835493550355135523553355435553556355735583559356035613562" +
"35633564356535663567356835693570357135723573357435753576357735783579" +
"35803581358235833584358535863587358835893590359135923593359435953596" +
"35973598359936003601360236033604360536063607360836093610361136123613" +
"36143615361636173618361936203621362236233624362536263627362836293630" +
"36313632363336343635363636373638363936403641364236433644364536463647" +
"36483649365036513652365336543655365636573658365936603661366236633664" +
"36653666366736683669367036713672367336743675367636773678367936803681" +
"36823683368436853686368736883689369036913692369336943695369636973698" +
"36993700370137023703370437053706370737083709371037113712371337143715" +
"37163717371837193720372137223723372437253726372737283729373037313732" +
"37333734373537363737373837393740374137423743374437453746374737483749" +
"37503751375237533754375537563757375837593760376137623763376437653766" +
"37673768376937703771377237733774377537763777377837793780378137823783" +
"37843785378637873788378937903791379237933794379537963797379837993800" +
"38013802380338043805380638073808380938103811381238133814381538163817" +
"38183819382038213822382338243825382638273828382938303831383238333834" +
"38353836383738383839384038413842384338443845384638473848384938503851" +
"38523853385438553856385738583859386038613862386338643865386638673868" +
"38693870387138723873387438753876387738783879388038813882388338843885" +
"38863887388838893890389138923893389438953896389738983899390039013902" +
"39033904390539063907390839093910391139123913391439153916391739183919" +
"39203921392239233924392539263927392839293930393139323933393439353936" +
"39373938393939403941394239433944394539463947394839493950395139523953" +
"39543955395639573958395939603961396239633964396539663967396839693970" +
"39713972397339743975397639773978397939803981398239833984398539863987" +
"39883989399039913992399339943995399639973998399940004001400240034004" +
"40054006400740084009401040114012401340144015401640174018401940204021" +
"40224023402440254026402740284029403040314032403340344035403640374038" +
"40394040404140424043404440454046404740484049405040514052405340544055" +
"40564057405840594060406140624063406440654066406740684069407040714072" +
"40734074407540764077407840794080408140824083408440854086408740884089" +
"40904091409240934094409540964097409840994100410141024103410441054106" +
"41074108410941104111411241134114411541164117411841194120412141224123" +
"41244125412641274128412941304131413241334134413541364137413841394140" +
"41414142414341444145414641474148414941504151415241534154415541564157" +
"41584159416041614162416341644165416641674168416941704171417241734174" +
"41754176417741784179418041814182418341844185418641874188418941904191" +
"41924193419441954196419741984199420042014202420342044205420642074208" +
"42094210421142124213421442154216421742184219422042214222422342244225" +
"42264227422842294230423142324233423442354236423742384239424042414242" +
"42434244424542464247424842494250425142524253425442554256425742584259" +
"42604261426242634264426542664267426842694270427142724273427442754276" +
"42774278427942804281428242834284428542864287428842894290429142924293" +
"42944295429642974298429943004301430243034304430543064307430843094310" +
"43114312431343144315431643174318431943204321432243234324432543264327" +
"43284329433043314332433343344335433643374338433943404341434243434344" +
"43454346434743484349435043514352435343544355435643574358435943604361" +
"43624363436443654366436743684369437043714372437343744375437643774378" +
"43794380438143824383438443854386438743884389439043914392439343944395" +
"43964397439843994400440144024403440444054406440744084409441044114412" +
"44134414441544164417441844194420442144224423442444254426442744284429" +
"44304431443244334434443544364437443844394440444144424443444444454446" +
"44474448444944504451445244534454445544564457445844594460446144624463" +
"44644465446644674468446944704471447244734474447544764477447844794480" +
"44814482448344844485448644874488448944904491449244934494449544964497" +
"44984499450045014502450345044505450645074508450945104511451245134514" +
"45154516451745184519452045214522452345244525452645274528452945304531" +
"45324533453445354536453745384539454045414542454345444545454645474548" +
"45494550455145524553455445554556455745584559456045614562456345644565" +
"45664567456845694570457145724573457445754576457745784579458045814582" +
"45834584458545864587458845894590459145924593459445954596459745984599" +
"46004601460246034604460546064607460846094610461146124613461446154616" +
"46174618461946204621462246234624462546264627462846294630463146324633" +
"46344635463646374638463946404641464246434644464546464647464846494650" +
"46514652465346544655465646574658465946604661466246634664466546664667" +
"46684669467046714672467346744675467646774678467946804681468246834684" +
"46854686468746884689469046914692469346944695469646974698469947004701" +
"47024703470447054706470747084709471047114712471347144715471647174718" +
"47194720472147224723472447254726472747284729473047314732473347344735" +
"47364737473847394740474147424743474447454746474747484749475047514752" +
"47534754475547564757475847594760476147624763476447654766476747684769" +
"47704771477247734774477547764777477847794780478147824783478447854786" +
"47874788478947904791479247934794479547964797479847994800480148024803" +
"48044805480648074808480948104811481248134814481548164817481848194820" +
"48214822482348244825482648274828482948304831483248334834483548364837" +
"48384839484048414842484348444845484648474848484948504851485248534854" +
"48554856485748584859486048614862486348644865486648674868486948704871" +
"48724873487448754876487748784879488048814882488348844885488648874888" +
"48894890489148924893489448954896489748984899490049014902490349044905" +
"49064907490849094910491149124913491449154916491749184919492049214922" +
"49234924492549264927492849294930493149324933493449354936493749384939" +
"49404941494249434944494549464947494849494950495149524953495449554956" +
"49574958495949604961496249634964496549664967496849694970497149724973" +
"49744975497649774978497949804981498249834984498549864987498849894990" +
"49914992499349944995499649974998499950005001500250035004500550065007" +
"50085009501050115012501350145015501650175018501950205021502250235024" +
"50255026502750285029503050315032503350345035503650375038503950405041" +
"50425043504450455046504750485049505050515052505350545055505650575058" +
"50595060506150625063506450655066506750685069507050715072507350745075" +
"50765077507850795080508150825083508450855086508750885089509050915092" +
"50935094509550965097509850995100510151025103510451055106510751085109" +
"51105111511251135114511551165117511851195120512151225123512451255126" +
"51275128512951305131513251335134513551365137513851395140514151425143" +
"51445145514651475148514951505151515251535154515551565157515851595160" +
"51615162516351645165516651675168516951705171517251735174517551765177" +
"51785179518051815182518351845185518651875188518951905191519251935194" +
"51955196519751985199520052015202520352045205520652075208520952105211" +
"52125213521452155216521752185219522052215222522352245225522652275228" +
"52295230523152325233523452355236523752385239524052415242524352445245" +
"52465247524852495250525152525253525452555256525752585259526052615262" +
"52635264526552665267526852695270527152725273527452755276527752785279" +
"52805281528252835284528552865287528852895290529152925293529452955296" +
"52975298529953005301530253035304530553065307530853095310531153125313" +
"53145315531653175318531953205321532253235324532553265327532853295330" +
"53315332533353345335533653375338533953405341534253435344534553465347" +
"53485349535053515352535353545355535653575358535953605361536253635364" +
"53655366536753685369537053715372537353745375537653775378537953805381" +
"53825383538453855386538753885389539053915392539353945395539653975398" +
"53995400540154025403540454055406540754085409541054115412541354145415" +
"54165417541854195420542154225423542454255426542754285429543054315432" +
"54335434543554365437543854395440544154425443544454455446544754485449" +
"54505451545254535454545554565457545854595460546154625463546454655466" +
"54675468546954705471547254735474547554765477547854795480548154825483" +
"54845485548654875488548954905491549254935494549554965497549854995500" +
"55015502550355045505550655075508550955105511551255135514551555165517" +
"55185519552055215522552355245525552655275528552955305531553255335534" +
"55355536553755385539554055415542554355445545554655475548554955505551" +
"55525553555455555556555755585559556055615562556355645565556655675568" +
"55695570557155725573557455755576557755785579558055815582558355845585" +
"55865587558855895590559155925593559455955596559755985599560056015602" +
"56035604560556065607560856095610561156125613561456155616561756185619" +
"56205621562256235624562556265627562856295630563156325633563456355636" +
"56375638563956405641564256435644564556465647564856495650565156525653" +
"56545655565656575658565956605661566256635664566556665667566856695670" +
"56715672567356745675567656775678567956805681568256835684568556865687" +
"56885689569056915692569356945695569656975698569957005701570257035704" +
"57055706570757085709571057115712571357145715571657175718571957205721" +
"57225723572457255726572757285729573057315732573357345735573657375738" +
"57395740574157425743574457455746574757485749575057515752575357545755" +
"57565757575857595760576157625763576457655766576757685769577057715772" +
"57735774577557765777577857795780578157825783578457855786578757885789" +
"57905791579257935794579557965797579857995800580158025803580458055806" +
"58075808580958105811581258135814581558165817581858195820582158225823" +
"58245825582658275828582958305831583258335834583558365837583858395840" +
"58415842584358445845584658475848584958505851585258535854585558565857" +
"58585859586058615862586358645865586658675868586958705871587258735874" +
"58755876587758785879588058815882588358845885588658875888588958905891" +
"58925893589458955896589758985899590059015902590359045905590659075908" +
"59095910591159125913591459155916591759185919592059215922592359245925" +
"59265927592859295930593159325933593459355936593759385939594059415942" +
"59435944594559465947594859495950595159525953595459555956595759585959" +
"59605961596259635964596559665967596859695970597159725973597459755976" +
"59775978597959805981598259835984598559865987598859895990599159925993" +
"59945995599659975998599960006001600260036004600560066007600860096010" +
"60116012601360146015601660176018601960206021602260236024602560266027" +
"60286029603060316032603360346035603660376038603960406041604260436044" +
"60456046604760486049605060516052605360546055605660576058605960606061" +
"60626063606460656066606760686069607060716072607360746075607660776078" +
"60796080608160826083608460856086608760886089609060916092609360946095" +
"60966097609860996100610161026103610461056106610761086109611061116112" +
"61136114611561166117611861196120612161226123612461256126612761286129" +
"61306131613261336134613561366137613861396140614161426143614461456146" +
"61476148614961506151615261536154615561566157615861596160616161626163" +
"61646165616661676168616961706171617261736174617561766177617861796180" +
"61816182618361846185618661876188618961906191619261936194619561966197" +
"61986199620062016202620362046205620662076208620962106211621262136214" +
"62156216621762186219622062216222622362246225622662276228622962306231" +
"62326233623462356236623762386239624062416242624362446245624662476248" +
"62496250625162526253625462556256625762586259626062616262626362646265" +
"62666267626862696270627162726273627462756276627762786279628062816282" +
"62836284628562866287628862896290629162926293629462956296629762986299" +
"63006301630263036304630563066307630863096310631163126313631463156316" +
"63176318631963206321632263236324632563266327632863296330633163326333" +
"63346335633663376338633963406341634263436344634563466347634863496350" +
"63516352635363546355635663576358635963606361636263636364636563666367" +
"63686369637063716372637363746375637663776378637963806381638263836384" +
"63856386638763886389639063916392639363946395639663976398639964006401" +
"64026403640464056406640764086409641064116412641364146415641664176418" +
"64196420642164226423642464256426642764286429643064316432643364346435" +
"64366437643864396440644164426443644464456446644764486449645064516452" +
"64536454645564566457645864596460646164626463646464656466646764686469" +
"64706471647264736474647564766477647864796480648164826483648464856486" +
"64876488648964906491649264936494649564966497649864996500650165026503" +
"65046505650665076508650965106511651265136514651565166517651865196520" +
"65216522652365246525652665276528652965306531653265336534653565366537" +
"65386539654065416542654365446545654665476548654965506551655265536554" +
"65556556655765586559656065616562656365646565656665676568656965706571" +
"65726573657465756576657765786579658065816582658365846585658665876588" +
"65896590659165926593659465956596659765986599660066016602660366046605" +
"66066607660866096610661166126613661466156616661766186619662066216622" +
"66236624662566266627662866296630663166326633663466356636663766386639" +
"66406641664266436644664566466647664866496650665166526653665466556656" +
"66576658665966606661666266636664666566666667666866696670667166726673" +
"66746675667666776678667966806681668266836684668566866687668866896690" +
"66916692669366946695669666976698669967006701670267036704670567066707" +
"67086709671067116712671367146715671667176718671967206721672267236724" +
"67256726672767286729673067316732673367346735673667376738673967406741" +
"67426743674467456746674767486749675067516752675367546755675667576758" +
"67596760676167626763676467656766676767686769677067716772677367746775" +
"67766777677867796780678167826783678467856786678767886789679067916792" +
"67936794679567966797679867996800680168026803680468056806680768086809" +
"68106811681268136814681568166817681868196820682168226823682468256826" +
"68276828682968306831683268336834683568366837683868396840684168426843" +
"68446845684668476848684968506851685268536854685568566857685868596860" +
"68616862686368646865686668676868686968706871687268736874687568766877" +
"68786879688068816882688368846885688668876888688968906891689268936894" +
"68956896689768986899690069016902690369046905690669076908690969106911" +
"69126913691469156916691769186919692069216922692369246925692669276928" +
"69296930693169326933693469356936693769386939694069416942694369446945" +
"69466947694869496950695169526953695469556956695769586959696069616962" +
"69636964696569666967696869696970697169726973697469756976697769786979" +
"69806981698269836984698569866987698869896990699169926993699469956996" +
"69976998699970007001700270037004700570067007700870097010701170127013" +
"70147015701670177018701970207021702270237024702570267027702870297030" +
"70317032703370347035703670377038703970407041704270437044704570467047" +
"70487049705070517052705370547055705670577058705970607061706270637064" +
"70657066706770687069707070717072707370747075707670777078707970807081" +
"70827083708470857086708770887089709070917092709370947095709670977098" +
"70997100710171027103710471057106710771087109711071117112711371147115" +
"71167117711871197120712171227123712471257126712771287129713071317132" +
"71337134713571367137713871397140714171427143714471457146714771487149" +
"71507151715271537154715571567157715871597160716171627163716471657166" +
"71677168716971707171717271737174717571767177717871797180718171827183" +
"71847185718671877188718971907191719271937194719571967197719871997200" +
"72017202720372047205720672077208720972107211721272137214721572167217" +
"72187219722072217222722372247225722672277228722972307231723272337234" +
"72357236723772387239724072417242724372447245724672477248724972507251" +
"72527253725472557256725772587259726072617262726372647265726672677268" +
"72697270727172727273727472757276727772787279728072817282728372847285" +
"72867287728872897290729172927293729472957296729772987299730073017302" +
"73037304730573067307730873097310731173127313731473157316731773187319" +
"73207321732273237324732573267327732873297330733173327333733473357336" +
"73377338733973407341734273437344734573467347734873497350735173527353" +
"73547355735673577358735973607361736273637364736573667367736873697370" +
"73717372737373747375737673777378737973807381738273837384738573867387" +
"73887389739073917392739373947395739673977398739974007401740274037404" +
"74057406740774087409741074117412741374147415741674177418741974207421" +
"74227423742474257426742774287429743074317432743374347435743674377438" +
"74397440744174427443744474457446744774487449745074517452745374547455" +
"74567457745874597460746174627463746474657466746774687469747074717472" +
"74737474747574767477747874797480748174827483748474857486748774887489" +
"74907491749274937494749574967497749874997500750175027503750475057506" +
"75077508750975107511751275137514751575167517751875197520752175227523" +
"75247525752675277528752975307531753275337534753575367537753875397540" +
"75417542754375447545754675477548754975507551755275537554755575567557" +
"75587559756075617562756375647565756675677568756975707571757275737574" +
"75757576757775787579758075817582758375847585758675877588758975907591" +
"75927593759475957596759775987599760076017602760376047605760676077608" +
"76097610761176127613761476157616761776187619762076217622762376247625" +
"76267627762876297630763176327633763476357636763776387639764076417642" +
"76437644764576467647764876497650765176527653765476557656765776587659" +
"76607661766276637664766576667667766876697670767176727673767476757676" +
"76777678767976807681768276837684768576867687768876897690769176927693" +
"76947695769676977698769977007701770277037704770577067707770877097710" +
"77117712771377147715771677177718771977207721772277237724772577267727" +
"77287729773077317732773377347735773677377738773977407741774277437744" +
"77457746774777487749775077517752775377547755775677577758775977607761" +
"77627763776477657766776777687769777077717772777377747775777677777778" +
"77797780778177827783778477857786778777887789779077917792779377947795" +
"77967797779877997800780178027803780478057806780778087809781078117812" +
"78137814781578167817781878197820782178227823782478257826782778287829" +
"78307831783278337834783578367837783878397840784178427843784478457846" +
"78477848784978507851785278537854785578567857785878597860786178627863" +
"78647865786678677868786978707871787278737874787578767877787878797880" +
"78817882788378847885788678877888788978907891789278937894789578967897" +
"78987899790079017902790379047905790679077908790979107911791279137914" +
"79157916791779187919792079217922792379247925792679277928792979307931" +
"79327933793479357936793779387939794079417942794379447945794679477948" +
"79497950795179527953795479557956795779587959796079617962796379647965" +
"79667967796879697970797179727973797479757976797779787979798079817982" +
"79837984798579867987798879897990799179927993799479957996799779987999" +
"80008001800280038004800580068007800880098010801180128013801480158016" +
"80178018801980208021802280238024802580268027802880298030803180328033" +
"80348035803680378038803980408041804280438044804580468047804880498050" +
"80518052805380548055805680578058805980608061806280638064806580668067" +
"80688069807080718072807380748075807680778078807980808081808280838084" +
"80858086808780888089809080918092809380948095809680978098809981008101" +
"81028103810481058106810781088109811081118112811381148115811681178118" +
"81198120812181228123812481258126812781288129813081318132813381348135" +
"81368137813881398140814181428143814481458146814781488149815081518152" +
"81538154815581568157815881598160816181628163816481658166816781688169" +
"81708171817281738174817581768177817881798180818181828183818481858186" +
"81878188818981908191819281938194819581968197819881998200820182028203" +
"82048205820682078208820982108211821282138214821582168217821882198220" +
"82218222822382248225822682278228822982308231823282338234823582368237" +
"82388239824082418242824382448245824682478248824982508251825282538254" +
"82558256825782588259826082618262826382648265826682678268826982708271" +
"82728273827482758276827782788279828082818282828382848285828682878288" +
"82898290829182928293829482958296829782988299830083018302830383048305" +
"83068307830883098310831183128313831483158316831783188319832083218322" +
"83238324832583268327832883298330833183328333833483358336833783388339" +
"83408341834283438344834583468347834883498350835183528353835483558356" +
"83578358835983608361836283638364836583668367836883698370837183728373" +
"83748375837683778378837983808381838283838384838583868387838883898390" +
"83918392839383948395839683978398839984008401840284038404840584068407" +
"84088409841084118412841384148415841684178418841984208421842284238424" +
"84258426842784288429843084318432843384348435843684378438843984408441" +
"84428443844484458446844784488449845084518452845384548455845684578458" +
"84598460846184628463846484658466846784688469847084718472847384748475" +
"84768477847884798480848184828483848484858486848784888489849084918492" +
"84938494849584968497849884998500850185028503850485058506850785088509" +
"85108511851285138514851585168517851885198520852185228523852485258526" +
"85278528852985308531853285338534853585368537853885398540854185428543" +
"85448545854685478548854985508551855285538554855585568557855885598560" +
"85618562856385648565856685678568856985708571857285738574857585768577" +
"85788579858085818582858385848585858685878588858985908591859285938594" +
"85958596859785988599860086018602860386048605860686078608860986108611" +
"86128613861486158616861786188619862086218622862386248625862686278628" +
"86298630863186328633863486358636863786388639864086418642864386448645" +
"86468647864886498650865186528653865486558656865786588659866086618662" +
"86638664866586668667866886698670867186728673867486758676867786788679" +
"86808681868286838684868586868687868886898690869186928693869486958696" +
"86978698869987008701870287038704870587068707870887098710871187128713" +
"87148715871687178718871987208721872287238724872587268727872887298730" +
"87318732873387348735873687378738873987408741874287438744874587468747" +
"87488749875087518752875387548755875687578758875987608761876287638764" +
"87658766876787688769877087718772877387748775877687778778877987808781" +
"87828783878487858786878787888789879087918792879387948795879687978798" +
"87998800880188028803880488058806880788088809881088118812881388148815" +
"88168817881888198820882188228823882488258826882788288829883088318832" +
"88338834883588368837883888398840884188428843884488458846884788488849" +
"88508851885288538854885588568857885888598860886188628863886488658866" +
"88678868886988708871887288738874887588768877887888798880888188828883" +
"88848885888688878888888988908891889288938894889588968897889888998900" +
"89018902890389048905890689078908890989108911891289138914891589168917" +
"89188919892089218922892389248925892689278928892989308931893289338934" +
"89358936893789388939894089418942894389448945894689478948894989508951" +
"89528953895489558956895789588959896089618962896389648965896689678968" +
"89698970897189728973897489758976897789788979898089818982898389848985" +
"89868987898889898990899189928993899489958996899789988999900090019002" +
"90039004900590069007900890099010901190129013901490159016901790189019" +
"90209021902290239024902590269027902890299030903190329033903490359036" +
"90379038903990409041904290439044904590469047904890499050905190529053" +
"90549055905690579058905990609061906290639064906590669067906890699070" +
"90719072907390749075907690779078907990809081908290839084908590869087" +
"90889089909090919092909390949095909690979098909991009101910291039104" +
"91059106910791089109911091119112911391149115911691179118911991209121" +
"91229123912491259126912791289129913091319132913391349135913691379138" +
"91399140914191429143914491459146914791489149915091519152915391549155" +
"91569157915891599160916191629163916491659166916791689169917091719172" +
"91739174917591769177917891799180918191829183918491859186918791889189" +
"91909191919291939194919591969197919891999200920192029203920492059206" +
"92079208920992109211921292139214921592169217921892199220922192229223" +
"92249225922692279228922992309231923292339234923592369237923892399240" +
"92419242924392449245924692479248924992509251925292539254925592569257" +
"92589259926092619262926392649265926692679268926992709271927292739274" +
"92759276927792789279928092819282928392849285928692879288928992909291" +
"92929293929492959296929792989299930093019302930393049305930693079308" +
"93099310931193129313931493159316931793189319932093219322932393249325" +
"93269327932893299330933193329333933493359336933793389339934093419342" +
"93439344934593469347934893499350935193529353935493559356935793589359" +
"93609361936293639364936593669367936893699370937193729373937493759376" +
"93779378937993809381938293839384938593869387938893899390939193929393" +
"93949395939693979398939994009401940294039404940594069407940894099410" +
"94119412941394149415941694179418941994209421942294239424942594269427" +
"94289429943094319432943394349435943694379438943994409441944294439444" +
"94459446944794489449945094519452945394549455945694579458945994609461" +
"94629463946494659466946794689469947094719472947394749475947694779478" +
"94799480948194829483948494859486948794889489949094919492949394949495" +
"94969497949894999500950195029503950495059506950795089509951095119512" +
"95139514951595169517951895199520952195229523952495259526952795289529" +
"95309531953295339534953595369537953895399540954195429543954495459546" +
"95479548954995509551955295539554955595569557955895599560956195629563" +
"95649565956695679568956995709571957295739574957595769577957895799580" +
"95819582958395849585958695879588958995909591959295939594959595969597" +
"95989599960096019602960396049605960696079608960996109611961296139614" +
"96159616961796189619962096219622962396249625962696279628962996309631" +
"96329633963496359636963796389639964096419642964396449645964696479648" +
"96499650965196529653965496559656965796589659966096619662966396649665" +
"96669667966896699670967196729673967496759676967796789679968096819682" +
"96839684968596869687968896899690969196929693969496959696969796989699" +
"97009701970297039704970597069707970897099710971197129713971497159716" +
"97179718971997209721972297239724972597269727972897299730973197329733" +
"97349735973697379738973997409741974297439744974597469747974897499750" +
"97519752975397549755975697579758975997609761976297639764976597669767" +
"97689769977097719772977397749775977697779778977997809781978297839784" +
"97859786978797889789979097919792979397949795979697979798979998009801" +
"98029803980498059806980798089809981098119812981398149815981698179818" +
"98199820982198229823982498259826982798289829983098319832983398349835" +
"98369837983898399840984198429843984498459846984798489849985098519852" +
"98539854985598569857985898599860986198629863986498659866986798689869" +
"98709871987298739874987598769877987898799880988198829883988498859886" +
"98879888988998909891989298939894989598969897989898999900990199029903" +
"99049905990699079908990999109911991299139914991599169917991899199920" +
"99219922992399249925992699279928992999309931993299339934993599369937" +
"99389939994099419942994399449945994699479948994999509951995299539954" +
"99559956995799589959996099619962996399649965996699679968996999709971" +
"99729973997499759976997799789979998099819982998399849985998699879988" +
"99899990999199929993999499959996999799989999"
)
package strutil
import (
"bytes"
"fmt"
"strconv"
"testing"
)
func TestItoa(t *testing.T) {
tests := []struct {
from int
exp string
}{
{0, `0`}, {1, `1`}, {8, `8`}, {9, `9`},
{10, `10`}, {11, `11`}, {98, `98`}, {99, `99`},
{100, `100`}, {101, `101`}, {998, `998`}, {999, `999`},
{1000, `1000`}, {1001, `1001`}, {9998, `9998`}, {9999, `9999`},
}
for idx, test := range tests {
t.Logf(`test #%v from %v exp %v`, idx, test.from, test.exp)
if exp, got := test.exp, Itoa(test.from); exp != got {
t.Fatalf(`exp %v; got %v`, exp, got)
}
}
for i := 0; i < 10010; i++ {
if exp, got := strconv.Itoa(i), Itoa(i); exp != got {
t.Fatalf(`from %v exp %v; got %v`, i, exp, got)
}
}
}
func BenchmarkItoa(b *testing.B) {
for _, y := range []int{100, 500, 1000, 9999} {
str := fmt.Sprint(y)
b.Run(`From`+str, func(b *testing.B) {
b.Run(`Std`, func(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
if s := strconv.Itoa(y); s != str {
b.Fatalf(`exp %v; got %v`, str, s)
}
}
})
b.Run(`Itoa`, func(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
if s := Itoa(y); s != str {
b.Fatalf(`exp %v; got %v`, str, s)
}
}
})
})
}
}
func makeLut3() {
var (
buf bytes.Buffer
y = 4
)
fmt.Fprintf(&buf, `var intStrLut3 = "`)
for i := 100; i <= 999; i++ {
fmt.Fprintf(&buf, "%03d", i)
if y++; y > 20 {
fmt.Fprintf(&buf, "\" +\n\"")
y = 0
}
}
fmt.Fprintf(&buf, `"`)
fmt.Println(buf.String())
}
func makeLut4() {
var (
buf bytes.Buffer
y = 4
)
fmt.Fprintf(&buf, `var intStrLut4 = "`)
for i := 1000; i <= 9999; i++ {
fmt.Fprintf(&buf, "%04d", i)
if y++; y > 16 {
fmt.Fprintf(&buf, "\" +\n\"")
y = 0
}
}
fmt.Fprintf(&buf, `"`)
fmt.Println(buf.String())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment