// license that can be found in the LICENSE file.
// Uniformly distributed pseudo-random numbers.
-package rand
+package rand
/*
* algorithm by
*/
const (
- _LEN = 607;
- _TAP = 273;
- _MAX = 1<<63;
- _MASK = _MAX-1;
- _A = 48271;
- _M = (1<<31)-1;
- _Q = 44488;
- _R = 3399;
+ _LEN = 607;
+ _TAP = 273;
+ _MAX = 1<<63;
+ _MASK = _MAX-1;
+ _A = 48271;
+ _M = (1<<31)-1;
+ _Q = 44488;
+ _R = 3399;
)
var (
rng_feed int; // index into vector
rng_vec [_LEN]int64; // current feedback register
- // cooked random numbers
- // the state of the rng
- // after 780e10 iterations
- rng_cooked [_LEN]int64 = [...]int64 {
- 5041579894721019882, 4646389086726545243, 1395769623340756751, 5333664234075297259,
- 2875692520355975054, 9033628115061424579, 7143218595135194537, 4812947590706362721,
- 7937252194349799378, 5307299880338848416, 8209348851763925077, 2115741599318814044,
- 4593015457530856296, 8140875735541888011, 3319429241265089026, 8619815648190321034,
- 1727074043483619500, 113108499721038619, 4569519971459345583, 5062833859075314731,
- 2387618771259064424, 2716131344356686112, 6559392774825876886, 7650093201692370310,
- 7684323884043752161, 257867835996031390, 6593456519409015164, 271327514973697897,
- 2789386447340118284, 1065192797246149621, 3344507881999356393, 4459797941780066633,
- 7465081662728599889, 1014950805555097187, 4449440729345990775, 3481109366438502643,
- 2418672789110888383, 5796562887576294778, 4484266064449540171, 3738982361971787048,
- 4523597184512354423, 10530508058128498, 8633833783282346118, 2625309929628791628,
- 8660405965245884302, 10162832508971942, 6540714680961817391, 7031802312784620857,
- 6240911277345944669, 831864355460801054, 8004434137542152891, 2116287251661052151,
- 2202309800992166967, 9161020366945053561, 4069299552407763864, 4936383537992622449,
- 457351505131524928, 342195045928179354, 2847771682816600509, 2068020115986376518,
- 4368649989588021065, 887231587095185257, 5563591506886576496, 6816225200251950296,
- 5616972787034086048, 8471809303394836566, 1686575021641186857, 4045484338074262002,
- 4244156215201778923, 7848217333783577387, 5632136521049761902, 833283142057835272,
- 9029726508369077193, 3243583134664087292, 4316371101804477087, 8937849979965997980,
- 6446940406810434101, 1679342092332374735, 6050638460742422078, 6993520719509581582,
- 7640877852514293609, 5881353426285907985, 812786550756860885, 4541845584483343330,
- 2725470216277009086, 4980675660146853729, 5210769080603236061, 8894283318990530821,
- 6326442804750084282, 1495812843684243920, 7069751578799128019, 7370257291860230865,
- 6756929275356942261, 4706794511633873654, 7824520467827898663, 8549875090542453214,
- 33650829478596156, 1328918435751322643, 7297902601803624459, 1011190183918857495,
- 2238025036817854944, 5147159997473910359, 896512091560522982, 2659470849286379941,
- 6097729358393448602, 1731725986304753684, 4106255841983812711, 8327155210721535508,
- 8477511620686074402, 5803876044675762232, 8435417780860221662, 5988852856651071244,
- 4715837297103951910, 7566171971264485114, 505808562678895611, 5070098180695063370,
- 842110666775871513, 572156825025677802, 1791881013492340891, 3393267094866038768,
- 3778721850472236509, 2352769483186201278, 1292459583847367458, 8897907043675088419,
- 5781809037144163536, 2733958794029492513, 5092019688680754699, 8996124554772526841,
- 4234737173186232084, 5027558287275472836, 4635198586344772304, 8687338893267139351,
- 5907508150730407386, 784756255473944452, 972392927514829904, 5422057694808175112,
- 5158420642969283891, 9048531678558643225, 2407211146698877100, 7583282216521099569,
- 3940796514530962282, 3341174631045206375, 3095313889586102949, 7405321895688238710,
- 5832080132947175283, 7890064875145919662, 8184139210799583195, 1149859861409226130,
- 1464597243840211302, 4641648007187991873, 3516491885471466898, 956288521791657692,
- 6657089965014657519, 5220884358887979358, 1796677326474620641, 5340761970648932916,
- 1147977171614181568, 5066037465548252321, 2574765911837859848, 1085848279845204775,
- 3350107529868390359, 6116438694366558490, 2107701075971293812, 1803294065921269267,
- 2469478054175558874, 7368243281019965984, 3791908367843677526, 185046971116456637,
- 2257095756513439648, 7217693971077460129, 909049953079504259, 7196649268545224266,
- 5637660345400869599, 3955544945427965183, 8057528650917418961, 4139268440301127643,
- 6621926588513568059, 1373361136802681441, 6527366231383600011, 3507654575162700890,
- 9202058512774729859, 1954818376891585542, 6640380907130175705, 8299563319178235687,
- 3901867355218954373, 7046310742295574065, 6847195391333990232, 1572638100518868053,
- 8850422670118399721, 3631909142291992901, 5158881091950831288, 2882958317343121593,
- 4763258931815816403, 6280052734341785344, 4243789408204964850, 2043464728020827976,
- 6545300466022085465, 4562580375758598164, 5495451168795427352, 1738312861590151095,
- 553004618757816492, 6895160632757959823, 8233623922264685171, 7139506338801360852,
- 8550891222387991669, 5535668688139305547, 2430933853350256242, 5401941257863201076,
- 8159640039107728799, 6157493831600770366, 7632066283658143750, 6308328381617103346,
- 3681878764086140361, 3289686137190109749, 6587997200611086848, 244714774258135476,
- 4079788377417136100, 8090302575944624335, 2945117363431356361, 864324395848741045,
- 3009039260312620700, 8430027460082534031, 401084700045993341, 7254622446438694921,
- 4707864159563588614, 5640248530963493951, 5982507712689997893, 3315098242282210105,
- 5503847578771918426, 3941971367175193882, 8118566580304798074, 3839261274019871296,
- 7062410411742090847, 741381002980207668, 6027994129690250817, 2497829994150063930,
- 6251390334426228834, 1368930247903518833, 8809096399316380241, 6492004350391900708,
- 2462145737463489636, 404828418920299174, 4153026434231690595, 261785715255475940,
- 5464715384600071357, 592710404378763017, 6764129236657751224, 8513655718539357449,
- 5820343663801914208, 385298524683789911, 5224135003438199467, 6303131641338802145,
- 7150122561309371392, 368107899140673753, 3115186834558311558, 2915636353584281051,
- 4782583894627718279, 6718292300699989587, 8387085186914375220, 3387513132024756289,
- 4654329375432538231, 8930667561363381602, 5374373436876319273, 7623042350483453954,
- 7725442901813263321, 9186225467561587250, 4091027289597503355, 2357631606492579800,
- 2530936820058611833, 1636551876240043639, 5564664674334965799, 1452244145334316253,
- 2061642381019690829, 1279580266495294036, 9108481583171221009, 6023278686734049809,
- 5007630032676973346, 2153168792952589781, 6720334534964750538, 6041546491134794105,
- 3433922409283786309, 2285479922797300912, 3110614940896576130, 6366559590722842893,
- 5418791419666136509, 7163298419643543757, 4891138053923696990, 580618510277907015,
- 1684034065251686769, 4429514767357295841, 330346578555450005, 1119637995812174675,
- 7177515271653460134, 4589042248470800257, 7693288629059004563, 143607045258444228,
- 246994305896273627, 866417324803099287, 6473547110565816071, 3092379936208876896,
- 2058427839513754051, 5133784708526867938, 8785882556301281247, 6149332666841167611,
- 8585842181454472135, 6137678347805511274, 2070447184436970006, 5708223427705576541,
- 5999657892458244504, 4358391411789012426, 325123008708389849, 6837621693887290924,
- 4843721905315627004, 6010651222149276415, 5398352198963874652, 4602025990114250980,
- 1044646352569048800, 9106614159853161675, 829256115228593269, 4919284369102997000,
- 2681532557646850893, 3681559472488511871, 5307999518958214035, 6334130388442829274,
- 2658708232916537604, 1163313865052186287, 581945337509520675, 3648778920718647903,
- 4423673246306544414, 1620799783996955743, 220828013409515943, 8150384699999389761,
- 4287360518296753003, 4590000184845883843, 5513660857261085186, 6964829100392774275,
- 478991688350776035, 8746140185685648781, 228500091334420247, 1356187007457302238,
- 3019253992034194581, 3152601605678500003, 430152752706002213, 5559581553696971176,
- 4916432985369275664, 663574931734554391, 3420773838927732076, 2868348622579915573,
- 1999319134044418520, 3328689518636282723, 2587672709781371173, 1517255313529399333,
- 3092343956317362483, 3662252519007064108, 972445599196498113, 7664865435875959367,
- 1708913533482282562, 6917817162668868494, 3217629022545312900, 2570043027221707107,
- 8739788839543624613, 2488075924621352812, 4694002395387436668, 4559628481798514356,
- 2997203966153298104, 1282559373026354493, 240113143146674385, 8665713329246516443,
- 628141331766346752, 4571950817186770476, 1472811188152235408, 7596648026010355826,
- 6091219417754424743, 7834161864828164065, 7103445518877254909, 4390861237357459201,
- 4442653864240571734, 8903482404847331368, 622261699494173647, 6037261250297213248,
- 504404948065709118, 7275215526217113061, 1011176780856001400, 2194750105623461063,
- 2623071828615234808, 5157313728073836108, 3738405111966602044, 2539767524076729570,
- 2467284396349269342, 5256026990536851868, 7841086888628396109, 6640857538655893162,
- 1202087339038317498, 2113514992440715978, 7534350895342931403, 4925284734898484745,
- 5145623771477493805, 8225140880134972332, 2719520354384050532, 9132346697815513771,
- 4332154495710163773, 7137789594094346916, 6994721091344268833, 6667228574869048934,
- 655440045726677499, 59934747298466858, 6124974028078036405, 8957774780655365418,
- 2332206071942466437, 1701056712286369627, 3154897383618636503, 1637766181387607527,
- 2460521277767576533, 197309393502684135, 643677854385267315, 2543179307861934850,
- 4350769010207485119, 4754652089410667672, 2015595502641514512, 7999059458976458608,
- 4287946071480840813, 8362686366770308971, 6486469209321732151, 3617727845841796026,
- 7554353525834302244, 4450022655153542367, 1605195740213535749, 5327014565305508387,
- 4626575813550328320, 2692222020597705149, 241045573717249868, 5098046974627094010,
- 7916882295460730264, 884817090297530579, 5329160409530630596, 7790979528857726136,
- 4955070238059373407, 4918537275422674302, 3008076183950404629, 3007769226071157901,
- 2470346235617803020, 8928702772696731736, 7856187920214445904, 4474874585391974885,
- 7900176660600710914, 2140571127916226672, 2425445057265199971, 2486055153341847830,
- 4186670094382025798, 1883939007446035042, 8808666044074867985, 3734134241178479257,
- 4065968871360089196, 6953124200385847784, 1305686814738899057, 1637739099014457647,
- 3656125660947993209, 3966759634633167020, 3106378204088556331, 6328899822778449810,
- 4565385105440252958, 1979884289539493806, 2331793186920865425, 3783206694208922581,
- 8464961209802336085, 2843963751609577687, 3030678195484896323, 4793717574095772604,
- 4459239494808162889, 402587895800087237, 8057891408711167515, 4541888170938985079,
- 1042662272908816815, 5557303057122568958, 2647678726283249984, 2144477441549833761,
- 5806352215355387087, 7117771003473903623, 5916597177708541638, 462597715452321361,
- 8833658097025758785, 5970273481425315300, 563813119381731307, 2768349550652697015,
- 1598828206250873866, 5206393647403558110, 6235043485709261823, 3152217402014639496,
- 8469693267274066490, 125672920241807416, 5311079624024060938, 6663754932310491587,
- 8736848295048751716, 4488039774992061878, 5923302823487327109, 140891791083103236,
- 7414942793393574290, 7990420780896957397, 4317817392807076702, 3625184369705367340,
- 2740722765288122703, 5743100009702758344, 5997898640509039159, 8854493341352484163,
- 5242208035432907801, 701338899890987198, 7609280429197514109, 3020985755112334161,
- 6651322707055512866, 2635195723621160615, 5144520864246028816, 1035086515727829828,
- 1567242097116389047, 8172389260191636581, 6337820351429292273, 2163012566996458925,
- 2743190902890262681, 1906367633221323427, 6011544915663598137, 5932255307352610768,
- 2241128460406315459, 895504896216695588, 3094483003111372717, 4583857460292963101,
- 9079887171656594975, 8839289181930711403, 5762740387243057873, 4225072055348026230,
- 1838220598389033063, 3801620336801580414, 8823526620080073856, 1776617605585100335,
- 7899055018877642622, 5421679761463003041, 5521102963086275121, 4248279443559365898,
- 8735487530905098534, 1760527091573692978, 7142485049657745894, 8222656872927218123,
- 4969531564923704323, 3394475942196872480, 6424174453260338141, 359248545074932887,
- 3273651282831730598, 6797106199797138596, 3030918217665093212, 145600834617314036,
- 6036575856065626233, 740416251634527158, 7080427635449935582, 6951781370868335478,
- 399922722363687927, 294902314447253185, 7844950936339178523, 880320858634709042,
- 6192655680808675579, 411604686384710388, 9026808440365124461, 6440783557497587732,
- 4615674634722404292, 539897290441580544, 2096238225866883852, 8751955639408182687,
- 1907224908052289603, 7381039757301768559, 6157238513393239656, 7749994231914157575,
- 8629571604380892756, 5280433031239081479, 7101611890139813254, 2479018537985767835,
- 7169176924412769570, 7942066497793203302, 1357759729055557688, 2278447439451174845,
- 3625338785743880657, 6477479539006708521, 8976185375579272206, 5511371554711836120,
- 1326024180520890843, 7537449876596048829, 5464680203499696154, 3189671183162196045,
- 6346751753565857109, 241159987320630307, 3095793449658682053, 8978332846736310159,
- 2902794662273147216, 7208698530190629697, 7276901792339343736, 1732385229314443140,
- 4133292154170828382, 2918308698224194548, 1519461397937144458, 5293934712616591764,
- 4922828954023452664, 2879211533496425641, 5896236396443472108, 8465043815351752425,
- 7329020396871624740, 8915471717014488588, 2944902635677463047, 7052079073493465134,
- 8382142935188824023, 9103922860780351547, 4152330101494654406 };
+ // cooked random numbers
+ // the state of the rng
+ // after 780e10 iterations
+ rng_cooked [_LEN]int64 = [...]int64{
+ 5041579894721019882, 4646389086726545243, 1395769623340756751, 5333664234075297259,
+ 2875692520355975054, 9033628115061424579, 7143218595135194537, 4812947590706362721,
+ 7937252194349799378, 5307299880338848416, 8209348851763925077, 2115741599318814044,
+ 4593015457530856296, 8140875735541888011, 3319429241265089026, 8619815648190321034,
+ 1727074043483619500, 113108499721038619, 4569519971459345583, 5062833859075314731,
+ 2387618771259064424, 2716131344356686112, 6559392774825876886, 7650093201692370310,
+ 7684323884043752161, 257867835996031390, 6593456519409015164, 271327514973697897,
+ 2789386447340118284, 1065192797246149621, 3344507881999356393, 4459797941780066633,
+ 7465081662728599889, 1014950805555097187, 4449440729345990775, 3481109366438502643,
+ 2418672789110888383, 5796562887576294778, 4484266064449540171, 3738982361971787048,
+ 4523597184512354423, 10530508058128498, 8633833783282346118, 2625309929628791628,
+ 8660405965245884302, 10162832508971942, 6540714680961817391, 7031802312784620857,
+ 6240911277345944669, 831864355460801054, 8004434137542152891, 2116287251661052151,
+ 2202309800992166967, 9161020366945053561, 4069299552407763864, 4936383537992622449,
+ 457351505131524928, 342195045928179354, 2847771682816600509, 2068020115986376518,
+ 4368649989588021065, 887231587095185257, 5563591506886576496, 6816225200251950296,
+ 5616972787034086048, 8471809303394836566, 1686575021641186857, 4045484338074262002,
+ 4244156215201778923, 7848217333783577387, 5632136521049761902, 833283142057835272,
+ 9029726508369077193, 3243583134664087292, 4316371101804477087, 8937849979965997980,
+ 6446940406810434101, 1679342092332374735, 6050638460742422078, 6993520719509581582,
+ 7640877852514293609, 5881353426285907985, 812786550756860885, 4541845584483343330,
+ 2725470216277009086, 4980675660146853729, 5210769080603236061, 8894283318990530821,
+ 6326442804750084282, 1495812843684243920, 7069751578799128019, 7370257291860230865,
+ 6756929275356942261, 4706794511633873654, 7824520467827898663, 8549875090542453214,
+ 33650829478596156, 1328918435751322643, 7297902601803624459, 1011190183918857495,
+ 2238025036817854944, 5147159997473910359, 896512091560522982, 2659470849286379941,
+ 6097729358393448602, 1731725986304753684, 4106255841983812711, 8327155210721535508,
+ 8477511620686074402, 5803876044675762232, 8435417780860221662, 5988852856651071244,
+ 4715837297103951910, 7566171971264485114, 505808562678895611, 5070098180695063370,
+ 842110666775871513, 572156825025677802, 1791881013492340891, 3393267094866038768,
+ 3778721850472236509, 2352769483186201278, 1292459583847367458, 8897907043675088419,
+ 5781809037144163536, 2733958794029492513, 5092019688680754699, 8996124554772526841,
+ 4234737173186232084, 5027558287275472836, 4635198586344772304, 8687338893267139351,
+ 5907508150730407386, 784756255473944452, 972392927514829904, 5422057694808175112,
+ 5158420642969283891, 9048531678558643225, 2407211146698877100, 7583282216521099569,
+ 3940796514530962282, 3341174631045206375, 3095313889586102949, 7405321895688238710,
+ 5832080132947175283, 7890064875145919662, 8184139210799583195, 1149859861409226130,
+ 1464597243840211302, 4641648007187991873, 3516491885471466898, 956288521791657692,
+ 6657089965014657519, 5220884358887979358, 1796677326474620641, 5340761970648932916,
+ 1147977171614181568, 5066037465548252321, 2574765911837859848, 1085848279845204775,
+ 3350107529868390359, 6116438694366558490, 2107701075971293812, 1803294065921269267,
+ 2469478054175558874, 7368243281019965984, 3791908367843677526, 185046971116456637,
+ 2257095756513439648, 7217693971077460129, 909049953079504259, 7196649268545224266,
+ 5637660345400869599, 3955544945427965183, 8057528650917418961, 4139268440301127643,
+ 6621926588513568059, 1373361136802681441, 6527366231383600011, 3507654575162700890,
+ 9202058512774729859, 1954818376891585542, 6640380907130175705, 8299563319178235687,
+ 3901867355218954373, 7046310742295574065, 6847195391333990232, 1572638100518868053,
+ 8850422670118399721, 3631909142291992901, 5158881091950831288, 2882958317343121593,
+ 4763258931815816403, 6280052734341785344, 4243789408204964850, 2043464728020827976,
+ 6545300466022085465, 4562580375758598164, 5495451168795427352, 1738312861590151095,
+ 553004618757816492, 6895160632757959823, 8233623922264685171, 7139506338801360852,
+ 8550891222387991669, 5535668688139305547, 2430933853350256242, 5401941257863201076,
+ 8159640039107728799, 6157493831600770366, 7632066283658143750, 6308328381617103346,
+ 3681878764086140361, 3289686137190109749, 6587997200611086848, 244714774258135476,
+ 4079788377417136100, 8090302575944624335, 2945117363431356361, 864324395848741045,
+ 3009039260312620700, 8430027460082534031, 401084700045993341, 7254622446438694921,
+ 4707864159563588614, 5640248530963493951, 5982507712689997893, 3315098242282210105,
+ 5503847578771918426, 3941971367175193882, 8118566580304798074, 3839261274019871296,
+ 7062410411742090847, 741381002980207668, 6027994129690250817, 2497829994150063930,
+ 6251390334426228834, 1368930247903518833, 8809096399316380241, 6492004350391900708,
+ 2462145737463489636, 404828418920299174, 4153026434231690595, 261785715255475940,
+ 5464715384600071357, 592710404378763017, 6764129236657751224, 8513655718539357449,
+ 5820343663801914208, 385298524683789911, 5224135003438199467, 6303131641338802145,
+ 7150122561309371392, 368107899140673753, 3115186834558311558, 2915636353584281051,
+ 4782583894627718279, 6718292300699989587, 8387085186914375220, 3387513132024756289,
+ 4654329375432538231, 8930667561363381602, 5374373436876319273, 7623042350483453954,
+ 7725442901813263321, 9186225467561587250, 4091027289597503355, 2357631606492579800,
+ 2530936820058611833, 1636551876240043639, 5564664674334965799, 1452244145334316253,
+ 2061642381019690829, 1279580266495294036, 9108481583171221009, 6023278686734049809,
+ 5007630032676973346, 2153168792952589781, 6720334534964750538, 6041546491134794105,
+ 3433922409283786309, 2285479922797300912, 3110614940896576130, 6366559590722842893,
+ 5418791419666136509, 7163298419643543757, 4891138053923696990, 580618510277907015,
+ 1684034065251686769, 4429514767357295841, 330346578555450005, 1119637995812174675,
+ 7177515271653460134, 4589042248470800257, 7693288629059004563, 143607045258444228,
+ 246994305896273627, 866417324803099287, 6473547110565816071, 3092379936208876896,
+ 2058427839513754051, 5133784708526867938, 8785882556301281247, 6149332666841167611,
+ 8585842181454472135, 6137678347805511274, 2070447184436970006, 5708223427705576541,
+ 5999657892458244504, 4358391411789012426, 325123008708389849, 6837621693887290924,
+ 4843721905315627004, 6010651222149276415, 5398352198963874652, 4602025990114250980,
+ 1044646352569048800, 9106614159853161675, 829256115228593269, 4919284369102997000,
+ 2681532557646850893, 3681559472488511871, 5307999518958214035, 6334130388442829274,
+ 2658708232916537604, 1163313865052186287, 581945337509520675, 3648778920718647903,
+ 4423673246306544414, 1620799783996955743, 220828013409515943, 8150384699999389761,
+ 4287360518296753003, 4590000184845883843, 5513660857261085186, 6964829100392774275,
+ 478991688350776035, 8746140185685648781, 228500091334420247, 1356187007457302238,
+ 3019253992034194581, 3152601605678500003, 430152752706002213, 5559581553696971176,
+ 4916432985369275664, 663574931734554391, 3420773838927732076, 2868348622579915573,
+ 1999319134044418520, 3328689518636282723, 2587672709781371173, 1517255313529399333,
+ 3092343956317362483, 3662252519007064108, 972445599196498113, 7664865435875959367,
+ 1708913533482282562, 6917817162668868494, 3217629022545312900, 2570043027221707107,
+ 8739788839543624613, 2488075924621352812, 4694002395387436668, 4559628481798514356,
+ 2997203966153298104, 1282559373026354493, 240113143146674385, 8665713329246516443,
+ 628141331766346752, 4571950817186770476, 1472811188152235408, 7596648026010355826,
+ 6091219417754424743, 7834161864828164065, 7103445518877254909, 4390861237357459201,
+ 4442653864240571734, 8903482404847331368, 622261699494173647, 6037261250297213248,
+ 504404948065709118, 7275215526217113061, 1011176780856001400, 2194750105623461063,
+ 2623071828615234808, 5157313728073836108, 3738405111966602044, 2539767524076729570,
+ 2467284396349269342, 5256026990536851868, 7841086888628396109, 6640857538655893162,
+ 1202087339038317498, 2113514992440715978, 7534350895342931403, 4925284734898484745,
+ 5145623771477493805, 8225140880134972332, 2719520354384050532, 9132346697815513771,
+ 4332154495710163773, 7137789594094346916, 6994721091344268833, 6667228574869048934,
+ 655440045726677499, 59934747298466858, 6124974028078036405, 8957774780655365418,
+ 2332206071942466437, 1701056712286369627, 3154897383618636503, 1637766181387607527,
+ 2460521277767576533, 197309393502684135, 643677854385267315, 2543179307861934850,
+ 4350769010207485119, 4754652089410667672, 2015595502641514512, 7999059458976458608,
+ 4287946071480840813, 8362686366770308971, 6486469209321732151, 3617727845841796026,
+ 7554353525834302244, 4450022655153542367, 1605195740213535749, 5327014565305508387,
+ 4626575813550328320, 2692222020597705149, 241045573717249868, 5098046974627094010,
+ 7916882295460730264, 884817090297530579, 5329160409530630596, 7790979528857726136,
+ 4955070238059373407, 4918537275422674302, 3008076183950404629, 3007769226071157901,
+ 2470346235617803020, 8928702772696731736, 7856187920214445904, 4474874585391974885,
+ 7900176660600710914, 2140571127916226672, 2425445057265199971, 2486055153341847830,
+ 4186670094382025798, 1883939007446035042, 8808666044074867985, 3734134241178479257,
+ 4065968871360089196, 6953124200385847784, 1305686814738899057, 1637739099014457647,
+ 3656125660947993209, 3966759634633167020, 3106378204088556331, 6328899822778449810,
+ 4565385105440252958, 1979884289539493806, 2331793186920865425, 3783206694208922581,
+ 8464961209802336085, 2843963751609577687, 3030678195484896323, 4793717574095772604,
+ 4459239494808162889, 402587895800087237, 8057891408711167515, 4541888170938985079,
+ 1042662272908816815, 5557303057122568958, 2647678726283249984, 2144477441549833761,
+ 5806352215355387087, 7117771003473903623, 5916597177708541638, 462597715452321361,
+ 8833658097025758785, 5970273481425315300, 563813119381731307, 2768349550652697015,
+ 1598828206250873866, 5206393647403558110, 6235043485709261823, 3152217402014639496,
+ 8469693267274066490, 125672920241807416, 5311079624024060938, 6663754932310491587,
+ 8736848295048751716, 4488039774992061878, 5923302823487327109, 140891791083103236,
+ 7414942793393574290, 7990420780896957397, 4317817392807076702, 3625184369705367340,
+ 2740722765288122703, 5743100009702758344, 5997898640509039159, 8854493341352484163,
+ 5242208035432907801, 701338899890987198, 7609280429197514109, 3020985755112334161,
+ 6651322707055512866, 2635195723621160615, 5144520864246028816, 1035086515727829828,
+ 1567242097116389047, 8172389260191636581, 6337820351429292273, 2163012566996458925,
+ 2743190902890262681, 1906367633221323427, 6011544915663598137, 5932255307352610768,
+ 2241128460406315459, 895504896216695588, 3094483003111372717, 4583857460292963101,
+ 9079887171656594975, 8839289181930711403, 5762740387243057873, 4225072055348026230,
+ 1838220598389033063, 3801620336801580414, 8823526620080073856, 1776617605585100335,
+ 7899055018877642622, 5421679761463003041, 5521102963086275121, 4248279443559365898,
+ 8735487530905098534, 1760527091573692978, 7142485049657745894, 8222656872927218123,
+ 4969531564923704323, 3394475942196872480, 6424174453260338141, 359248545074932887,
+ 3273651282831730598, 6797106199797138596, 3030918217665093212, 145600834617314036,
+ 6036575856065626233, 740416251634527158, 7080427635449935582, 6951781370868335478,
+ 399922722363687927, 294902314447253185, 7844950936339178523, 880320858634709042,
+ 6192655680808675579, 411604686384710388, 9026808440365124461, 6440783557497587732,
+ 4615674634722404292, 539897290441580544, 2096238225866883852, 8751955639408182687,
+ 1907224908052289603, 7381039757301768559, 6157238513393239656, 7749994231914157575,
+ 8629571604380892756, 5280433031239081479, 7101611890139813254, 2479018537985767835,
+ 7169176924412769570, 7942066497793203302, 1357759729055557688, 2278447439451174845,
+ 3625338785743880657, 6477479539006708521, 8976185375579272206, 5511371554711836120,
+ 1326024180520890843, 7537449876596048829, 5464680203499696154, 3189671183162196045,
+ 6346751753565857109, 241159987320630307, 3095793449658682053, 8978332846736310159,
+ 2902794662273147216, 7208698530190629697, 7276901792339343736, 1732385229314443140,
+ 4133292154170828382, 2918308698224194548, 1519461397937144458, 5293934712616591764,
+ 4922828954023452664, 2879211533496425641, 5896236396443472108, 8465043815351752425,
+ 7329020396871624740, 8915471717014488588, 2944902635677463047, 7052079073493465134,
+ 8382142935188824023, 9103922860780351547, 4152330101494654406,
+ };
)
// seed rng x[n+1] = 48271 * x[n] mod (2**31 - 1)
func seedrand(x int32) int32 {
- hi := x / _Q;
- lo := x % _Q;
+ hi := x/_Q;
+ lo := x%_Q;
x = _A*lo - _R*hi;
if x < 0 {
x += _M;
x = seedrand(x);
if i >= 0 {
var u int64;
- u = int64(x) << 40;
+ u = int64(x)<<40;
x = seedrand(x);
- u ^= int64(x) << 20;
+ u ^= int64(x)<<20;
x = seedrand(x);
u ^= int64(x);
u ^= rng_cooked[i];
- rng_vec[i] = u & _MASK;
+ rng_vec[i] = u&_MASK;
}
}
}
rng_feed += _LEN;
}
- x := (rng_vec[rng_feed] + rng_vec[rng_tap]) & _MASK;
+ x := (rng_vec[rng_feed]+rng_vec[rng_tap])&_MASK;
rng_vec[rng_feed] = x;
return x;
}
// Uint32 returns a pseudo-random 32-bit value as a uint32.
func Uint32() uint32 {
- return uint32(Int63() >> 31);
+ return uint32(Int63()>>31);
}
// Int31 returns a non-negative pseudo-random 31-bit integer as an int32.
func Int31() int32 {
- return int32(Int63() >> 32);
+ return int32(Int63()>>32);
}
// Int returns a non-negative pseudo-random int. All bits but the top bit are random.
func Int() int {
u := uint(Int63());
- return int(u << 1 >> 1); // clear sign bit if int == int32
+ return int(u<<1>>1); // clear sign bit if int == int32
}
// Int63n returns, as an int64, a non-negative pseudo-random number in [0,n).
func Int63n(n int64) int64 {
if n <= 0 {
- return 0
+ return 0;
}
- max := int64((1<<63)-1 - (1<<63) % uint64(n));
+ max := int64((1<<63) - 1 - (1<<63)%uint64(n));
v := Int63();
for v > max {
- v = Int63()
+ v = Int63();
}
- return v % n
+ return v%n;
}
// Int31n returns, as an int32, a non-negative pseudo-random number in [0,n).
func Int31n(n int32) int32 {
- return int32(Int63n(int64(n)))
+ return int32(Int63n(int64(n)));
}
// Intn returns, as an int, a non-negative pseudo-random number in [0,n).
func Intn(n int) int {
- return int(Int63n(int64(n)))
+ return int(Int63n(int64(n)));
}
// Float64 returns, as a float64, a pseudo-random number in [0.0,1.0).
func Float64() float64 {
- x := float64(Int63()) / float64(_MAX);
+ x := float64(Int63())/float64(_MAX);
for x >= 1 {
- x = float64(Int63()) / float64(_MAX);
+ x = float64(Int63())/float64(_MAX);
}
return x;
}
// Float32 returns, as a float32, a pseudo-random number in [0.0,1.0).
func Float32() float32 {
- return float32(Float64())
+ return float32(Float64());
}
// Float returns, as a float, a pseudo-random number in [0.0,1.0).
-func Float() float
-{
- return float(Float64())
+func Float() float {
+ return float(Float64());
}
// Perm returns, as a slice of n ints, a pseudo-random permutation of the integers [0,n).
func Perm(n int) []int {
m := make([]int, n);
- for i:=0; i<n; i++ {
+ for i := 0; i < n; i++ {
m[i] = i;
}
- for i:=0; i<n; i++ {
+ for i := 0; i < n; i++ {
j := Intn(n);
- m[i],m[j] = m[j],m[i];
+ m[i], m[j] = m[j], m[i];
}
return m;
}
// checks in progress are true when it reencounters them.
// Visited are stored in a map indexed by 17 * a1 + a2;
type visit struct {
- a1 uintptr;
- a2 uintptr;
- typ Type;
- next *visit;
+ a1 uintptr;
+ a2 uintptr;
+ typ Type;
+ next *visit;
}
// Tests for deep equality using reflected types. The map argument tracks
// recursive types.
func deepValueEqual(v1, v2 Value, visited map[uintptr]*visit, depth int) bool {
if v1 == nil || v2 == nil {
- return v1 == v2
+ return v1 == v2;
}
if v1.Type() != v2.Type() {
return false;
}
// ... or already seen
- h := 17 * addr1 + addr2;
+ h := 17*addr1 + addr2;
seen, _ := visited[h];
typ := v1.Type();
for p := seen; p != nil; p = p.next {
case *StructValue:
struct1 := v;
struct2 := v2.(*StructValue);
- for i, n:= 0, v.NumField(); i < n; i++ {
+ for i, n := 0, v.NumField(); i < n; i++ {
if !deepValueEqual(struct1.Field(i), struct2.Field(i), visited, depth+1) {
return false;
}
package reflect_test
import (
- . "reflect";
- "strconv";
+ . "reflect";
+ "strconv";
)
// valueToString returns a textual representation of the reflection value val.
return val.Get();
case *BoolValue:
if val.Get() {
- return "true"
+ return "true";
} else {
- return "false"
+ return "false";
}
case *PtrValue:
v := val;
str += "{";
for i := 0; i < v.Len(); i++ {
if i > 0 {
- str += ", "
+ str += ", ";
}
str += valueToString(v.Elem(i));
}
str += "{";
for i, n := 0, v.NumField(); i < n; i++ {
if i > 0 {
- str += ", "
+ str += ", ";
}
str += valueToString(v.Field(i));
}
*/
type commonType struct {
- size uintptr;
- hash uint32;
- alg uint8;
- align uint8;
- fieldAlign uint8;
- string *string;
+ size uintptr;
+ hash uint32;
+ alg uint8;
+ align uint8;
+ fieldAlign uint8;
+ string *string;
*uncommonType;
}
type method struct {
- hash uint32;
- name *string;
- pkgPath *string;
- typ *runtime.Type;
- ifn unsafe.Pointer;
- tfn unsafe.Pointer;
+ hash uint32;
+ name *string;
+ pkgPath *string;
+ typ *runtime.Type;
+ ifn unsafe.Pointer;
+ tfn unsafe.Pointer;
}
type uncommonType struct {
- name *string;
- pkgPath *string;
- methods []method;
+ name *string;
+ pkgPath *string;
+ methods []method;
}
// BoolType represents a boolean type.
type BoolType struct {
- commonType
+ commonType;
}
// Float32Type represents a float32 type.
type Float32Type struct {
- commonType
+ commonType;
}
// Float64Type represents a float64 type.
type Float64Type struct {
- commonType
+ commonType;
}
// FloatType represents a float type.
type FloatType struct {
- commonType
+ commonType;
}
// Int16Type represents an int16 type.
type Int16Type struct {
- commonType
+ commonType;
}
// Int32Type represents an int32 type.
type Int32Type struct {
- commonType
+ commonType;
}
// Int64Type represents an int64 type.
type Int64Type struct {
- commonType
+ commonType;
}
// Int8Type represents an int8 type.
type Int8Type struct {
- commonType
+ commonType;
}
// IntType represents an int type.
type IntType struct {
- commonType
+ commonType;
}
// Uint16Type represents a uint16 type.
type Uint16Type struct {
- commonType
+ commonType;
}
// Uint32Type represents a uint32 type.
type Uint32Type struct {
- commonType
+ commonType;
}
// Uint64Type represents a uint64 type.
type Uint64Type struct {
- commonType
+ commonType;
}
// Uint8Type represents a uint8 type.
type Uint8Type struct {
- commonType
+ commonType;
}
// UintType represents a uint type.
type UintType struct {
- commonType
+ commonType;
}
// StringType represents a string type.
type StringType struct {
- commonType
+ commonType;
}
// UintptrType represents a uintptr type.
type UintptrType struct {
- commonType
+ commonType;
}
// DotDotDotType represents the ... that can
// be used as the type of the final function parameter.
type DotDotDotType struct {
- commonType
+ commonType;
}
// UnsafePointerType represents an unsafe.Pointer type.
type UnsafePointerType struct {
- commonType
+ commonType;
}
// ArrayType represents a fixed array type.
type ArrayType struct {
commonType;
- elem *runtime.Type;
- len uintptr;
+ elem *runtime.Type;
+ len uintptr;
}
// ChanDir represents a channel type's direction.
type ChanDir int
+
const (
- RecvDir ChanDir = 1<<iota;
+ RecvDir ChanDir = 1<<iota;
SendDir;
- BothDir = RecvDir | SendDir;
+ BothDir = RecvDir|SendDir;
)
// ChanType represents a channel type.
type ChanType struct {
commonType;
- elem *runtime.Type;
- dir uintptr;
+ elem *runtime.Type;
+ dir uintptr;
}
// FuncType represents a function type.
type FuncType struct {
commonType;
- in []*runtime.Type;
- out []*runtime.Type;
+ in []*runtime.Type;
+ out []*runtime.Type;
}
// Method on interface type
type imethod struct {
- hash uint32;
- perm uint32;
- name *string;
- pkgPath *string;
- typ *runtime.Type;
+ hash uint32;
+ perm uint32;
+ name *string;
+ pkgPath *string;
+ typ *runtime.Type;
}
// InterfaceType represents an interface type.
type InterfaceType struct {
commonType;
- methods []imethod;
+ methods []imethod;
}
// MapType represents a map type.
type MapType struct {
commonType;
- key *runtime.Type;
- elem *runtime.Type;
+ key *runtime.Type;
+ elem *runtime.Type;
}
// PtrType represents a pointer type.
type PtrType struct {
commonType;
- elem *runtime.Type;
+ elem *runtime.Type;
}
// SliceType represents a slice type.
type SliceType struct {
commonType;
- elem *runtime.Type;
+ elem *runtime.Type;
}
// Struct field
type structField struct {
- name *string;
- pkgPath *string;
- typ *runtime.Type;
- tag *string;
- offset uintptr;
+ name *string;
+ pkgPath *string;
+ typ *runtime.Type;
+ tag *string;
+ offset uintptr;
}
// StructType represents a struct type.
type StructType struct {
commonType;
- fields []structField;
+ fields []structField;
}
// Method represents a single method.
type Method struct {
- PkgPath string; // empty for uppercase Name
- Name string;
- Type *FuncType;
- Func *FuncValue;
+ PkgPath string; // empty for uppercase Name
+ Name string;
+ Type *FuncType;
+ Func *FuncValue;
}
// Type is the runtime representation of a Go type.
type Type interface {
// PkgPath returns the type's package path.
// The package path is a full package import path like "container/vector".
- PkgPath() string;
+ PkgPath() string;
// Name returns the type's name within its package.
- Name() string;
+ Name() string;
// String returns a string representation of the type.
// The string representation may use shortened package names
// (e.g., vector instead of "container/vector") and is not
// guaranteed to be unique among types. To test for equality,
// compare the Types directly.
- String() string;
+ String() string;
// Size returns the number of bytes needed to store
// a value of the given type; it is analogous to unsafe.Sizeof.
- Size() uintptr;
+ Size() uintptr;
// Align returns the alignment of a value of this type
// when allocated in memory.
- Align() int;
+ Align() int;
// FieldAlign returns the alignment of a value of this type
// when used as a field in a struct.
- FieldAlign() int;
+ FieldAlign() int;
// For non-interface types, Method returns the i'th method with receiver T.
// For interface types, Method returns the i'th method in the interface.
// NumMethod returns the number of such methods.
- Method(int) Method;
- NumMethod() int;
-
+ Method(int) Method;
+ NumMethod() int;
uncommon() *uncommonType;
}
func (t *uncommonType) PkgPath() string {
if t == nil || t.pkgPath == nil {
- return ""
+ return "";
}
return *t.pkgPath;
}
}
type StructField struct {
- PkgPath string; // empty for uppercase Name
- Name string;
- Type Type;
- Tag string;
- Offset uintptr;
- Index []int;
- Anonymous bool;
+ PkgPath string; // empty for uppercase Name
+ Name string;
+ Type Type;
+ Tag string;
+ Offset uintptr;
+ Index []int;
+ Anonymous bool;
}
// Field returns the i'th struct field.
return;
}
-const inf = 1 << 30; // infinity - no struct has that many nesting levels
+const inf = 1<<30 // infinity - no struct has that many nesting levels
func (t *StructType) fieldByName(name string, mark map[*StructType]bool, depth int) (ff StructField, fd int) {
fd = inf; // field depth
mark[t] = true;
var fi int; // field index
- n := 0; // number of matching fields at depth fd
+ n := 0; // number of matching fields at depth fd
L: for i, _ := range t.fields {
f := t.Field(i);
d := inf;
ff.Index = ff.Index[0 : fd+1];
f, present = ff, true;
}
- return
+ return;
}
// NumField returns the number of struct fields.
func Typeof(i interface{}) Type {
return toType(unsafe.Typeof(i));
}
-
-
re string;
err os.Error;
}
+
var bad_re = []stringError{
- stringError{ `*`, ErrBareClosure },
- stringError{ `(abc`, ErrUnmatchedLpar },
- stringError{ `abc)`, ErrUnmatchedRpar },
- stringError{ `x[a-z`, ErrUnmatchedLbkt },
- stringError{ `abc]`, ErrUnmatchedRbkt },
- stringError{ `[z-a]`, ErrBadRange },
- stringError{ `abc\`, ErrExtraneousBackslash },
- stringError{ `a**`, ErrBadClosure },
- stringError{ `a*+`, ErrBadClosure },
- stringError{ `a??`, ErrBadClosure },
- stringError{ `*`, ErrBareClosure },
- stringError{ `\x`, ErrBadBackslash },
-}
-
-type vec []int;
+ stringError{`*`, ErrBareClosure},
+ stringError{`(abc`, ErrUnmatchedLpar},
+ stringError{`abc)`, ErrUnmatchedRpar},
+ stringError{`x[a-z`, ErrUnmatchedLbkt},
+ stringError{`abc]`, ErrUnmatchedRbkt},
+ stringError{`[z-a]`, ErrBadRange},
+ stringError{`abc\`, ErrExtraneousBackslash},
+ stringError{`a**`, ErrBadClosure},
+ stringError{`a*+`, ErrBadClosure},
+ stringError{`a??`, ErrBadClosure},
+ stringError{`*`, ErrBareClosure},
+ stringError{`\x`, ErrBadBackslash},
+}
+
+type vec []int
type tester struct {
re string;
match vec;
}
-var matches = []tester {
- tester{ ``, "", vec{0,0} },
- tester{ `a`, "a", vec{0,1} },
- tester{ `x`, "y", vec{} },
- tester{ `b`, "abc", vec{1,2} },
- tester{ `.`, "a", vec{0,1} },
- tester{ `.*`, "abcdef", vec{0,6} },
- tester{ `^abcd$`, "abcd", vec{0,4} },
- tester{ `^bcd'`, "abcdef", vec{} },
- tester{ `^abcd$`, "abcde", vec{} },
- tester{ `a+`, "baaab", vec{1,4} },
- tester{ `a*`, "baaab", vec{0,0} },
- tester{ `[a-z]+`, "abcd", vec{0,4} },
- tester{ `[^a-z]+`, "ab1234cd", vec{2,6} },
- tester{ `[a\-\]z]+`, "az]-bcz", vec{0,4} },
- tester{ `[^\n]+`, "abcd\n", vec{0,4} },
- tester{ `[日本語]+`, "日本語日本語", vec{0,18} },
- tester{ `()`, "", vec{0,0, 0,0} },
- tester{ `(a)`, "a", vec{0,1, 0,1} },
- tester{ `(.)(.)`, "日a", vec{0,4, 0,3, 3,4} },
- tester{ `(.*)`, "", vec{0,0, 0,0} },
- tester{ `(.*)`, "abcd", vec{0,4, 0,4} },
- tester{ `(..)(..)`, "abcd", vec{0,4, 0,2, 2,4} },
- tester{ `(([^xyz]*)(d))`, "abcd", vec{0,4, 0,4, 0,3, 3,4} },
- tester{ `((a|b|c)*(d))`, "abcd", vec{0,4, 0,4, 2,3, 3,4} },
- tester{ `(((a|b|c)*)(d))`, "abcd", vec{0,4, 0,4, 0,3, 2,3, 3,4} },
- tester{ `a*(|(b))c*`, "aacc", vec{0,4, 2,2, -1,-1} },
+var matches = []tester{
+ tester{``, "", vec{0, 0}},
+ tester{`a`, "a", vec{0, 1}},
+ tester{`x`, "y", vec{}},
+ tester{`b`, "abc", vec{1, 2}},
+ tester{`.`, "a", vec{0, 1}},
+ tester{`.*`, "abcdef", vec{0, 6}},
+ tester{`^abcd$`, "abcd", vec{0, 4}},
+ tester{`^bcd'`, "abcdef", vec{}},
+ tester{`^abcd$`, "abcde", vec{}},
+ tester{`a+`, "baaab", vec{1, 4}},
+ tester{`a*`, "baaab", vec{0, 0}},
+ tester{`[a-z]+`, "abcd", vec{0, 4}},
+ tester{`[^a-z]+`, "ab1234cd", vec{2, 6}},
+ tester{`[a\-\]z]+`, "az]-bcz", vec{0, 4}},
+ tester{`[^\n]+`, "abcd\n", vec{0, 4}},
+ tester{`[日本語]+`, "日本語日本語", vec{0, 18}},
+ tester{`()`, "", vec{0, 0, 0, 0}},
+ tester{`(a)`, "a", vec{0, 1, 0, 1}},
+ tester{`(.)(.)`, "日a", vec{0, 4, 0, 3, 3, 4}},
+ tester{`(.*)`, "", vec{0, 0, 0, 0}},
+ tester{`(.*)`, "abcd", vec{0, 4, 0, 4}},
+ tester{`(..)(..)`, "abcd", vec{0, 4, 0, 2, 2, 4}},
+ tester{`(([^xyz]*)(d))`, "abcd", vec{0, 4, 0, 4, 0, 3, 3, 4}},
+ tester{`((a|b|c)*(d))`, "abcd", vec{0, 4, 0, 4, 2, 3, 3, 4}},
+ tester{`(((a|b|c)*)(d))`, "abcd", vec{0, 4, 0, 4, 0, 3, 2, 3, 3, 4}},
+ tester{`a*(|(b))c*`, "aacc", vec{0, 4, 2, 2, -1, -1}},
}
func compileTest(t *testing.T, expr string, error os.Error) *Regexp {
if err != error {
t.Error("compiling `", expr, "`; unexpected error: ", err.String());
}
- return re
+ return re;
}
func printVec(t *testing.T, m []int) {
t.Log("\t<no match>");
} else {
for i := 0; i < l; i = i+2 {
- t.Log("\t", m[i], ",", m[i+1])
+ t.Log("\t", m[i], ",", m[i+1]);
}
}
}
t.Log("\t<no match>");
} else {
for i := 0; i < l; i = i+2 {
- t.Logf("\t%q", m[i])
+ t.Logf("\t%q", m[i]);
}
}
}
t.Log("\t<no match>");
} else {
for i := 0; i < l; i = i+2 {
- t.Logf("\t%q", b[i])
+ t.Logf("\t%q", b[i]);
}
}
}
func equal(m1, m2 []int) bool {
l := len(m1);
if l != len(m2) {
- return false
+ return false;
}
for i := 0; i < l; i++ {
if m1[i] != m2[i] {
- return false
+ return false;
}
}
- return true
+ return true;
}
func equalStrings(m1, m2 []string) bool {
l := len(m1);
if l != len(m2) {
- return false
+ return false;
}
for i := 0; i < l; i++ {
if m1[i] != m2[i] {
- return false
+ return false;
}
}
- return true
+ return true;
}
func equalBytes(m1 [][]byte, m2 []string) bool {
l := len(m1);
if l != len(m2) {
- return false
+ return false;
}
for i := 0; i < l; i++ {
if string(m1[i]) != m2[i] {
- return false
+ return false;
}
}
- return true
+ return true;
}
func executeTest(t *testing.T, expr string, str string, match []int) {
re := compileTest(t, expr, nil);
if re == nil {
- return
+ return;
}
m := re.ExecuteString(str);
if !equal(m, match) {
func TestBadCompile(t *testing.T) {
for i := 0; i < len(bad_re); i++ {
- compileTest(t, bad_re[i].re, bad_re[i].err)
+ compileTest(t, bad_re[i].re, bad_re[i].err);
}
}
func TestExecute(t *testing.T) {
for i := 0; i < len(matches); i++ {
test := &matches[i];
- executeTest(t, test.re, test.text, test.match)
+ executeTest(t, test.re, test.text, test.match);
}
}
func matchTest(t *testing.T, expr string, str string, match []int) {
re := compileTest(t, expr, nil);
if re == nil {
- return
+ return;
}
m := re.MatchString(str);
if m != (len(match) > 0) {
func TestMatch(t *testing.T) {
for i := 0; i < len(matches); i++ {
test := &matches[i];
- matchTest(t, test.re, test.text, test.match)
+ matchTest(t, test.re, test.text, test.match);
}
}
func matchStringsTest(t *testing.T, expr string, str string, match []int) {
re := compileTest(t, expr, nil);
if re == nil {
- return
+ return;
}
strs := make([]string, len(match)/2);
for i := 0; i < len(match); i++ {
- strs[i/2] = str[match[i] : match[i+1]]
+ strs[i/2] = str[match[i]:match[i+1]];
}
m := re.MatchStrings(str);
if !equalStrings(m, strs) {
func TestMatchStrings(t *testing.T) {
for i := 0; i < len(matches); i++ {
test := &matches[i];
- matchTest(t, test.re, test.text, test.match)
+ matchTest(t, test.re, test.text, test.match);
}
}
func matchFunctionTest(t *testing.T, expr string, str string, match []int) {
m, err := MatchString(expr, str);
if err == nil {
- return
+ return;
}
if m != (len(match) > 0) {
t.Error("function Match failure on `", expr, "` matching `", str, "`:", m, "should be", len(match) > 0);
func TestMatchFunction(t *testing.T) {
for i := 0; i < len(matches); i++ {
test := &matches[i];
- matchFunctionTest(t, test.re, test.text, test.match)
+ matchFunctionTest(t, test.re, test.text, test.match);
}
}
pattern, replacement, input, output string;
}
-var replaceTests = []ReplaceTest {
+var replaceTests = []ReplaceTest{
// Test empty input and/or replacement, with pattern that matches the empty string.
ReplaceTest{"", "", "", ""},
ReplaceTest{"", "x", "", "x"},
pattern, output string;
}
-var quoteMetaTests = []QuoteMetaTest {
+var quoteMetaTests = []QuoteMetaTest{
QuoteMetaTest{``, ``},
QuoteMetaTest{`foo`, `foo`},
QuoteMetaTest{`!@#$%^&*()_+-=[{]}\|,<.>/?~`, `!@#\$%\^&\*\(\)_\+-=\[{\]}\\\|,<\.>/\?~`},
}
type matchCase struct {
- matchfunc string;
- input string;
- n int;
- regexp string;
- expected []string;
-}
-
-var matchCases = []matchCase {
- matchCase{"match", " aa b", 0, "[^ ]+", []string { "aa", "b" }},
- matchCase{"match", " aa b", 0, "[^ ]*", []string { "", "aa", "b" }},
- matchCase{"match", "a b c", 0, "[^ ]*", []string { "a", "b", "c" }},
- matchCase{"match", "a:a: a:", 0, "^.:", []string { "a:" }},
- matchCase{"match", "", 0, "[^ ]*", []string { "" }},
- matchCase{"match", "", 0, "", []string { "" }},
- matchCase{"match", "a", 0, "", []string { "", "" }},
- matchCase{"match", "ab", 0, "^", []string { "", }},
- matchCase{"match", "ab", 0, "$", []string { "", }},
- matchCase{"match", "ab", 0, "X*", []string { "", "", "" }},
- matchCase{"match", "aX", 0, "X*", []string { "", "X" }},
- matchCase{"match", "XabX", 0, "X*", []string { "X", "", "X" }},
-
- matchCase{"matchit", "", 0, ".", []string {}},
- matchCase{"matchit", "abc", 2, ".", []string { "a", "b" }},
- matchCase{"matchit", "abc", 0, ".", []string { "a", "b", "c" }},
+ matchfunc string;
+ input string;
+ n int;
+ regexp string;
+ expected []string;
+}
+
+var matchCases = []matchCase{
+ matchCase{"match", " aa b", 0, "[^ ]+", []string{"aa", "b"}},
+ matchCase{"match", " aa b", 0, "[^ ]*", []string{"", "aa", "b"}},
+ matchCase{"match", "a b c", 0, "[^ ]*", []string{"a", "b", "c"}},
+ matchCase{"match", "a:a: a:", 0, "^.:", []string{"a:"}},
+ matchCase{"match", "", 0, "[^ ]*", []string{""}},
+ matchCase{"match", "", 0, "", []string{""}},
+ matchCase{"match", "a", 0, "", []string{"", ""}},
+ matchCase{"match", "ab", 0, "^", []string{""}},
+ matchCase{"match", "ab", 0, "$", []string{""}},
+ matchCase{"match", "ab", 0, "X*", []string{"", "", ""}},
+ matchCase{"match", "aX", 0, "X*", []string{"", "X"}},
+ matchCase{"match", "XabX", 0, "X*", []string{"X", "", "X"}},
+
+ matchCase{"matchit", "", 0, ".", []string{}},
+ matchCase{"matchit", "abc", 2, ".", []string{"a", "b"}},
+ matchCase{"matchit", "abc", 0, ".", []string{"a", "b", "c"}},
}
func printStringSlice(t *testing.T, s []string) {
t.Log("\t<empty>");
} else {
for i := 0; i < l; i++ {
- t.Logf("\t%q", s[i])
+ t.Logf("\t%q", s[i]);
}
}
}
c.input,
c.n,
c.regexp,
- c.expected };
+ c.expected,
+ };
ch <- stringCase;
}
close(ch);
"template";
)
-const debugText =
- `<html>
+const debugText = `<html>
<body>
<title>Services</title>
{.repeated section @}
type serviceArray []debugService
-func (s serviceArray) Len() int { return len(s) }
-func (s serviceArray) Less(i, j int) bool { return s[i].name < s[j].name }
-func (s serviceArray) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
+func (s serviceArray) Len() int {
+ return len(s);
+}
+func (s serviceArray) Less(i, j int) bool {
+ return s[i].name < s[j].name;
+}
+func (s serviceArray) Swap(i, j int) {
+ s[i], s[j] = s[j], s[i];
+}
-func (m methodArray) Len() int { return len(m) }
-func (m methodArray) Less(i, j int) bool { return m[i].name < m[j].name }
-func (m methodArray) Swap(i, j int) { m[i], m[j] = m[j], m[i] }
+func (m methodArray) Len() int {
+ return len(m);
+}
+func (m methodArray) Less(i, j int) bool {
+ return m[i].name < m[j].name;
+}
+func (m methodArray) Swap(i, j int) {
+ m[i], m[j] = m[j], m[i];
+}
// Runs at /debug/rpc
func debugHTTP(c *http.Conn, req *http.Request) {
// Precompute the reflect type for os.Error. Can't use os.Error directly
// because Typeof takes an empty interface value. This is annoying.
-var unusedError *os.Error;
+var unusedError *os.Error
var typeOfOsError = reflect.Typeof(unusedError).(*reflect.PtrType).Elem()
type methodType struct {
sync.Mutex; // protects counters
- method reflect.Method;
- argType *reflect.PtrType;
+ method reflect.Method;
+ argType *reflect.PtrType;
replyType *reflect.PtrType;
numCalls uint;
}
type service struct {
- name string; // name of service
- rcvr reflect.Value; // receiver of methods for the service
- typ reflect.Type; // type of the receiver
- method map[string] *methodType; // registered methods
+ name string; // name of service
+ rcvr reflect.Value; // receiver of methods for the service
+ typ reflect.Type; // type of the receiver
+ method map[string]*methodType; // registered methods
}
// Request is a header written before every RPC call. It is used internally
// network traffic.
type Request struct {
ServiceMethod string; // format: "Service.Method"
- Seq uint64; // sequence number chosen by client
+ Seq uint64; // sequence number chosen by client
}
// Response is a header written before every RPC return. It is used internally
// network traffic.
type Response struct {
ServiceMethod string; // echoes that of the Request
- Seq uint64; // echoes that of the request
- Error string; // error, if any.
+ Seq uint64; // echoes that of the request
+ Error string; // error, if any.
}
type serverType struct {
sync.Mutex; // protects the serviceMap
- serviceMap map[string] *service;
+ serviceMap map[string]*service;
}
// This variable is a global whose "public" methods are really private methods
// called from the global functions of this package: rpc.Register, rpc.ServeConn, etc.
// For example, rpc.Register() calls server.add().
-var server = &serverType{ serviceMap: make(map[string] *service) }
+var server = &serverType{serviceMap: make(map[string]*service)}
// Is this a publicly vislble - upper case - name?
func isPublic(name string) bool {
rune, _ := utf8.DecodeRuneInString(name);
- return unicode.IsUpper(rune)
+ return unicode.IsUpper(rune);
}
func (server *serverType) register(rcvr interface{}) os.Error {
server.Lock();
defer server.Unlock();
if server.serviceMap == nil {
- server.serviceMap = make(map[string] *service);
+ server.serviceMap = make(map[string]*service);
}
s := new(service);
s.typ = reflect.Typeof(rcvr);
s.rcvr = reflect.NewValue(rcvr);
sname := reflect.Indirect(s.rcvr).Type().Name();
if sname == "" {
- log.Exit("rpc: no service name for type", s.typ.String())
+ log.Exit("rpc: no service name for type", s.typ.String());
}
if !isPublic(sname) {
s := "rpc Register: type " + sname + " is not public";
return os.ErrorString("rpc: service already defined: " + sname);
}
s.name = sname;
- s.method = make(map[string] *methodType);
+ s.method = make(map[string]*methodType);
// Install the methods
for m := 0; m < s.typ.NumMethod(); m++ {
mtype := method.Type;
mname := method.Name;
if !isPublic(mname) {
- continue
+ continue;
}
// Method needs three ins: receiver, *args, *reply.
// The args and reply must be structs until gobs are more general.
// A value sent as a placeholder for the response when the server receives an invalid request.
type InvalidRequest struct {
- marker int
+ marker int;
}
+
var invalidRequest = InvalidRequest{1}
func _new(t *reflect.PtrType) *reflect.PtrValue {
// It returns an error if the receiver is not public or has no
// suitable methods.
func Register(rcvr interface{}) os.Error {
- return server.register(rcvr)
+ return server.register(rcvr);
}
// ServeConn runs the server on a single connection. When the connection
// completes, service terminates. ServeConn blocks; the caller typically
// invokes it in a go statement.
func ServeConn(conn io.ReadWriteCloser) {
- go server.input(conn)
+ go server.input(conn);
}
// Accept accepts connections on the listener and serves requests
// for each incoming connection. Accept blocks; the caller typically
// invokes it in a go statement.
func Accept(lis net.Listener) {
- server.accept(lis)
+ server.accept(lis);
}
// Can connect to RPC service using HTTP CONNECT to rpcPath.
type Args struct {
- A, B int
+ A, B int;
}
type Reply struct {
- C int
+ C int;
}
type Arith int
func (t *Arith) Add(args *Args, reply *Reply) os.Error {
reply.C = args.A + args.B;
- return nil
+ return nil;
}
func (t *Arith) Mul(args *Args, reply *Reply) os.Error {
reply.C = args.A * args.B;
- return nil
+ return nil;
}
func (t *Arith) Div(args *Args, reply *Reply) os.Error {
return os.ErrorString("divide by zero");
}
reply.C = args.A / args.B;
- return nil
+ return nil;
}
func (t *Arith) Error(args *Args, reply *Reply) os.Error {
}
// Synchronous calls
- args := &Args{7,8};
+ args := &Args{7, 8};
reply := new(Reply);
err = client.Call("Arith.Add", args, reply);
if reply.C != args.A + args.B {
t.Errorf("Add: expected %d got %d", reply.C, args.A + args.B);
}
- args = &Args{7,8};
+ args = &Args{7, 8};
reply = new(Reply);
err = client.Call("Arith.Mul", args, reply);
if reply.C != args.A * args.B {
}
// Out of order.
- args = &Args{7,8};
+ args = &Args{7, 8};
mulReply := new(Reply);
mulCall := client.Go("Arith.Mul", args, mulReply, nil);
addReply := new(Reply);
}
// Error test
- args = &Args{7,0};
+ args = &Args{7, 0};
reply = new(Reply);
err = client.Call("Arith.Div", args, reply);
// expect an error: zero divide
}
// Synchronous calls
- args := &Args{7,8};
+ args := &Args{7, 8};
reply := new(Reply);
err = client.Call("Arith.Add", args, reply);
if reply.C != args.A + args.B {
conn, err := net.Dial("tcp", "", serverAddr);
if err != nil {
- t.Fatal("dialing:", err)
+ t.Fatal("dialing:", err);
}
client := NewClient(conn);
- args := &Args{7,8};
+ args := &Args{7, 8};
reply := new(Reply);
err = client.Call("Unknown.Add", args, reply);
if err == nil {
conn, err := net.Dial("tcp", "", serverAddr);
if err != nil {
- t.Fatal("dialing:", err)
+ t.Fatal("dialing:", err);
}
client := NewClient(conn);
- args := &Args{7,8};
+ args := &Args{7, 8};
reply := new(Reply);
err = client.Call("Arith.Unknown", args, reply);
if err == nil {
conn, err := net.Dial("tcp", "", serverAddr);
if err != nil {
- t.Fatal("dialing:", err)
+ t.Fatal("dialing:", err);
}
client := NewClient(conn);
// Gosched yields the processor, allowing other goroutines to run. It does not
// suspend the current goroutine, so execution resumes automatically.
-func Gosched()
+func Gosched()
// Goexit terminates the goroutine that calls it. No other goroutine is affected.
-func Goexit()
+func Goexit()
// Breakpoint() executes a breakpoint trap.
-func Breakpoint()
+func Breakpoint()
// Caller reports file and line number information about function invocations on
// the calling goroutine's stack. The argument is the number of stack frames to
// ascend, with 1 identifying the the caller of Caller. The return values report the
// program counter, file name, and line number within the file of the corresponding
// call. The boolean ok is false if it was not possible to recover the information.
-func Caller(n int) (pc uintptr, file string, line int, ok bool)
+func Caller(n int) (pc uintptr, file string, line int, ok bool)
// mid returns the current os thread (m) id.
func mid() uint32
m1 := a;
m2 := c;
// bubble sort on 3 elements
- if data.Less(m1, m0) { data.Swap(m1, m0); }
- if data.Less(m2, m1) { data.Swap(m2, m1); }
- if data.Less(m1, m0) { data.Swap(m1, m0); }
- // now data[m0] <= data[m1] <= data[m2]
+ if data.Less(m1, m0) {
+ data.Swap(m1, m0);
+ }
+ if data.Less(m2, m1) {
+ data.Swap(m2, m1);
+ }
+ if data.Less(m1, m0) {
+ data.Swap(m1, m0);
+ }
+// now data[m0] <= data[m1] <= data[m2]
}
func swapRange(data SortInterface, a, b, n int) {
func doPivot(data SortInterface, lo, hi int) (midlo, midhi int) {
m := (lo+hi)/2;
- if hi - lo > 40 {
+ if hi-lo > 40 {
// Tukey's ``Ninther,'' median of three medians of three.
- s := (hi - lo) / 8;
- medianOfThree(data, lo, lo+s, lo+2*s);
+ s := (hi-lo)/8;
+ medianOfThree(data, lo, lo+s, lo + 2*s);
medianOfThree(data, m, m-s, m+s);
- medianOfThree(data, hi-1, hi-1-s, hi-1-2*s);
+ medianOfThree(data, hi-1, hi-1-s, hi - 1 - 2*s);
}
medianOfThree(data, lo, m, hi-1);
}
func quickSort(data SortInterface, a, b int) {
- if b - a > 7 {
+ if b-a > 7 {
mlo, mhi := doPivot(data, a, b);
quickSort(data, a, mlo);
quickSort(data, mhi, b);
- } else if b - a > 1 {
+ } else if b-a > 1 {
insertionSort(data, a, b);
}
}
func IsSorted(data SortInterface) bool {
n := data.Len();
- for i := n - 1; i > 0; i-- {
- if data.Less(i, i - 1) {
+ for i := n-1; i > 0; i-- {
+ if data.Less(i, i-1) {
return false;
}
}
// IntArray attaches the methods of SortInterface to []int, sorting in increasing order.
type IntArray []int
-func (p IntArray) Len() int { return len(p); }
-func (p IntArray) Less(i, j int) bool { return p[i] < p[j]; }
-func (p IntArray) Swap(i, j int) { p[i], p[j] = p[j], p[i]; }
+func (p IntArray) Len() int {
+ return len(p);
+}
+func (p IntArray) Less(i, j int) bool {
+ return p[i] < p[j];
+}
+func (p IntArray) Swap(i, j int) {
+ p[i], p[j] = p[j], p[i];
+}
// Sort is a convenience method.
-func (p IntArray) Sort() { Sort(p); }
+func (p IntArray) Sort() {
+ Sort(p);
+}
// FloatArray attaches the methods of SortInterface to []float, sorting in increasing order.
-type FloatArray []float
+type FloatArray []float
-func (p FloatArray) Len() int { return len(p); }
-func (p FloatArray) Less(i, j int) bool { return p[i] < p[j]; }
-func (p FloatArray) Swap(i, j int) { p[i], p[j] = p[j], p[i]; }
+func (p FloatArray) Len() int {
+ return len(p);
+}
+func (p FloatArray) Less(i, j int) bool {
+ return p[i] < p[j];
+}
+func (p FloatArray) Swap(i, j int) {
+ p[i], p[j] = p[j], p[i];
+}
// Sort is a convenience method.
-func (p FloatArray) Sort() { Sort(p); }
+func (p FloatArray) Sort() {
+ Sort(p);
+}
// StringArray attaches the methods of SortInterface to []string, sorting in increasing order.
type StringArray []string
-func (p StringArray) Len() int { return len(p); }
-func (p StringArray) Less(i, j int) bool { return p[i] < p[j]; }
-func (p StringArray) Swap(i, j int) { p[i], p[j] = p[j], p[i]; }
+func (p StringArray) Len() int {
+ return len(p);
+}
+func (p StringArray) Less(i, j int) bool {
+ return p[i] < p[j];
+}
+func (p StringArray) Swap(i, j int) {
+ p[i], p[j] = p[j], p[i];
+}
// Sort is a convenience method.
-func (p StringArray) Sort() { Sort(p); }
+func (p StringArray) Sort() {
+ Sort(p);
+}
// Convenience wrappers for common cases
// SortInts sorts an array of ints in increasing order.
-func SortInts(a []int) { Sort(IntArray(a)); }
+func SortInts(a []int) {
+ Sort(IntArray(a));
+}
// SortFloats sorts an array of floats in increasing order.
-func SortFloats(a []float) { Sort(FloatArray(a)); }
+func SortFloats(a []float) {
+ Sort(FloatArray(a));
+}
// SortStrings sorts an array of strings in increasing order.
-func SortStrings(a []string) { Sort(StringArray(a)); }
+func SortStrings(a []string) {
+ Sort(StringArray(a));
+}
// IntsAreSorted tests whether an array of ints is sorted in increasing order.
-func IntsAreSorted(a []int) bool { return IsSorted(IntArray(a)); }
+func IntsAreSorted(a []int) bool {
+ return IsSorted(IntArray(a));
+}
// FloatsAreSorted tests whether an array of floats is sorted in increasing order.
-func FloatsAreSorted(a []float) bool { return IsSorted(FloatArray(a)); }
+func FloatsAreSorted(a []float) bool {
+ return IsSorted(FloatArray(a));
+}
// StringsAreSorted tests whether an array of strings is sorted in increasing order.
-func StringsAreSorted(a []string) bool { return IsSorted(StringArray(a)); }
+func StringsAreSorted(a []string) bool {
+ return IsSorted(StringArray(a));
+}
}
const (
- _Sawtooth = iota;
+ _Sawtooth = iota;
_Rand;
_Stagger;
_Plateau;
)
const (
- _Copy = iota;
+ _Copy = iota;
_Reverse;
_ReverseFirstHalf;
_ReverseSecondHalf;
)
type testingData struct {
- desc string;
- t *testing.T;
- data []int;
- maxswap int; // number of swaps allowed
- nswap int;
+ desc string;
+ t *testing.T;
+ data []int;
+ maxswap int; // number of swaps allowed
+ nswap int;
}
-func (d *testingData) Len() int { return len(d.data); }
-func (d *testingData) Less(i, j int) bool { return d.data[i] < d.data[j]; }
+func (d *testingData) Len() int {
+ return len(d.data);
+}
+func (d *testingData) Less(i, j int) bool {
+ return d.data[i] < d.data[j];
+}
func (d *testingData) Swap(i, j int) {
if d.nswap >= d.maxswap {
d.t.Errorf("%s: used %d swaps sorting array of %d", d.desc, d.nswap, len(d.data));
for i := 0; i < n; i++ {
switch dist {
case _Sawtooth:
- data[i] = i % m;
+ data[i] = i%m;
case _Rand:
data[i] = rand.Intn(m);
case _Stagger:
- data[i] = (i*m + i) % n;
+ data[i] = (i*m + i)%n;
case _Plateau:
data[i] = min(i, m);
case _Shuffle:
}
case _ReverseFirstHalf:
for i := 0; i < n/2; i++ {
- mdata[i] = data[n/2-i-1];
+ mdata[i] = data[n/2 - i - 1];
}
for i := n/2; i < n; i++ {
mdata[i] = data[i];
mdata[i] = data[i];
}
for i := n/2; i < n; i++ {
- mdata[i] = data[n-(i-n/2)-1];
+ mdata[i] = data[n-(i - n/2)-1];
}
case _Sorted:
for i := 0; i < n; i++ {
}
}
}
-
}
// decimal power of ten to binary power of two.
-var powtab = []int{
- 1, 3, 6, 9, 13, 16, 19, 23, 26
-}
+var powtab = []int{1, 3, 6, 9, 13, 16, 19, 23, 26}
func decimalToFloatBits(neg bool, d *decimal, trunc bool, flt *floatInfo) (b uint64, overflow bool) {
var exp int;
// Minimum representable exponent is flt.bias+1.
// If the exponent is smaller, move it up and
// adjust d accordingly.
- if exp < flt.bias+1 {
- n := flt.bias+1 - exp;
+ if exp < flt.bias + 1 {
+ n := flt.bias + 1 - exp;
d.Shift(-n);
exp += n;
}
- if exp-flt.bias >= 1<<flt.expbits - 1 {
+ if exp - flt.bias >= 1 << flt.expbits - 1 {
goto overflow;
}
// Extract 1+flt.mantbits bits.
- mant = d.Shift(int(1+flt.mantbits)).RoundedInteger();
+ mant = d.Shift(int(1 + flt.mantbits)).RoundedInteger();
// Rounding might have added a bit; shift down.
- if mant == 2<<flt.mantbits {
+ if mant == 2 << flt.mantbits {
mant >>= 1;
exp++;
- if exp-flt.bias >= 1<<flt.expbits - 1 {
+ if exp - flt.bias >= 1 << flt.expbits - 1 {
goto overflow;
}
}
// Denormalized?
- if mant&(1<<flt.mantbits) == 0 {
+ if mant&(1 << flt.mantbits) == 0 {
exp = flt.bias;
}
goto out;
overflow:
// ±Inf
mant = 0;
- exp = 1<<flt.expbits - 1 + flt.bias;
+ exp = 1 << flt.expbits - 1 + flt.bias;
overflow = true;
out:
// Assemble bits.
- bits := mant & (uint64(1)<<flt.mantbits - 1);
- bits |= uint64((exp-flt.bias)&(1<<flt.expbits - 1)) << flt.mantbits;
+ bits := mant&(uint64(1) << flt.mantbits - 1);
+ bits |= uint64((exp - flt.bias)&(1 << flt.expbits - 1)) << flt.mantbits;
if neg {
- bits |= 1<<flt.mantbits<<flt.expbits;
+ bits |= 1 << flt.mantbits << flt.expbits;
}
return bits, overflow;
}
}
// Exact powers of 10.
-var float64pow10 = []float64 {
+var float64pow10 = []float64{
1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9,
1e10, 1e11, 1e12, 1e13, 1e14, 1e15, 1e16, 1e17, 1e18, 1e19,
- 1e20, 1e21, 1e22
-}
-var float32pow10 = []float32 {
- 1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9, 1e10
+ 1e20, 1e21, 1e22,
}
+var float32pow10 = []float32{1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9, 1e10}
// If possible to convert decimal d to 64-bit float f exactly,
// entirely in floating-point math, do so, avoiding the expense of decimalToFloatBits.
if ovf {
err = &NumError{s, os.ERANGE};
}
- return f, err
+ return f, err;
}
// Atof64 converts the string s to a 64-bit floating-point number.
if ovf {
err = &NumError{s, os.ERANGE};
}
- return f, err
+ return f, err;
}
// Atof is like Atof32 or Atof64, depending on the size of float.
f1, err1 := Atof64(s);
return float(f1), err1;
}
-
package strconv_test
import (
- "os";
- "reflect";
- . "strconv";
- "testing";
+ "os";
+ "reflect";
+ . "strconv";
+ "testing";
)
type atofTest struct {
- in string;
- out string;
- err os.Error;
+ in string;
+ out string;
+ err os.Error;
}
-var atoftests = []atofTest {
- atofTest{ "", "0", os.EINVAL },
- atofTest{ "1", "1", nil },
- atofTest{ "+1", "1", nil },
- atofTest{ "1x", "0", os.EINVAL },
- atofTest{ "1.1.", "0", os.EINVAL },
- atofTest{ "1e23", "1e+23", nil },
- atofTest{ "100000000000000000000000", "1e+23", nil },
- atofTest{ "1e-100", "1e-100", nil },
- atofTest{ "123456700", "1.234567e+08", nil },
- atofTest{ "99999999999999974834176", "9.999999999999997e+22", nil },
- atofTest{ "100000000000000000000001", "1.0000000000000001e+23", nil },
- atofTest{ "100000000000000008388608", "1.0000000000000001e+23", nil },
- atofTest{ "100000000000000016777215", "1.0000000000000001e+23", nil },
- atofTest{ "100000000000000016777216", "1.0000000000000003e+23", nil },
- atofTest{ "-1", "-1", nil },
- atofTest{ "-0", "-0", nil },
- atofTest{ "1e-20", "1e-20", nil },
- atofTest{ "625e-3", "0.625", nil },
+var atoftests = []atofTest{
+ atofTest{"", "0", os.EINVAL},
+ atofTest{"1", "1", nil},
+ atofTest{"+1", "1", nil},
+ atofTest{"1x", "0", os.EINVAL},
+ atofTest{"1.1.", "0", os.EINVAL},
+ atofTest{"1e23", "1e+23", nil},
+ atofTest{"100000000000000000000000", "1e+23", nil},
+ atofTest{"1e-100", "1e-100", nil},
+ atofTest{"123456700", "1.234567e+08", nil},
+ atofTest{"99999999999999974834176", "9.999999999999997e+22", nil},
+ atofTest{"100000000000000000000001", "1.0000000000000001e+23", nil},
+ atofTest{"100000000000000008388608", "1.0000000000000001e+23", nil},
+ atofTest{"100000000000000016777215", "1.0000000000000001e+23", nil},
+ atofTest{"100000000000000016777216", "1.0000000000000003e+23", nil},
+ atofTest{"-1", "-1", nil},
+ atofTest{"-0", "-0", nil},
+ atofTest{"1e-20", "1e-20", nil},
+ atofTest{"625e-3", "0.625", nil},
// largest float64
- atofTest{ "1.7976931348623157e308", "1.7976931348623157e+308", nil },
- atofTest{ "-1.7976931348623157e308", "-1.7976931348623157e+308", nil },
+ atofTest{"1.7976931348623157e308", "1.7976931348623157e+308", nil},
+ atofTest{"-1.7976931348623157e308", "-1.7976931348623157e+308", nil},
// next float64 - too large
- atofTest{ "1.7976931348623159e308", "+Inf", os.ERANGE },
- atofTest{ "-1.7976931348623159e308", "-Inf", os.ERANGE },
+ atofTest{"1.7976931348623159e308", "+Inf", os.ERANGE},
+ atofTest{"-1.7976931348623159e308", "-Inf", os.ERANGE},
// the border is ...158079
// borderline - okay
- atofTest{ "1.7976931348623158e308", "1.7976931348623157e+308", nil },
- atofTest{ "-1.7976931348623158e308", "-1.7976931348623157e+308", nil },
+ atofTest{"1.7976931348623158e308", "1.7976931348623157e+308", nil},
+ atofTest{"-1.7976931348623158e308", "-1.7976931348623157e+308", nil},
// borderline - too large
- atofTest{ "1.797693134862315808e308", "+Inf", os.ERANGE },
- atofTest{ "-1.797693134862315808e308", "-Inf", os.ERANGE },
+ atofTest{"1.797693134862315808e308", "+Inf", os.ERANGE},
+ atofTest{"-1.797693134862315808e308", "-Inf", os.ERANGE},
// a little too large
- atofTest{ "1e308", "1e+308", nil },
- atofTest{ "2e308", "+Inf", os.ERANGE },
- atofTest{ "1e309", "+Inf", os.ERANGE },
+ atofTest{"1e308", "1e+308", nil},
+ atofTest{"2e308", "+Inf", os.ERANGE},
+ atofTest{"1e309", "+Inf", os.ERANGE},
// way too large
- atofTest{ "1e310", "+Inf", os.ERANGE },
- atofTest{ "-1e310", "-Inf", os.ERANGE },
- atofTest{ "1e400", "+Inf", os.ERANGE },
- atofTest{ "-1e400", "-Inf", os.ERANGE },
- atofTest{ "1e400000", "+Inf", os.ERANGE },
- atofTest{ "-1e400000", "-Inf", os.ERANGE },
+ atofTest{"1e310", "+Inf", os.ERANGE},
+ atofTest{"-1e310", "-Inf", os.ERANGE},
+ atofTest{"1e400", "+Inf", os.ERANGE},
+ atofTest{"-1e400", "-Inf", os.ERANGE},
+ atofTest{"1e400000", "+Inf", os.ERANGE},
+ atofTest{"-1e400000", "-Inf", os.ERANGE},
// denormalized
- atofTest{ "1e-305", "1e-305", nil },
- atofTest{ "1e-306", "1e-306", nil },
- atofTest{ "1e-307", "1e-307", nil },
- atofTest{ "1e-308", "1e-308", nil },
- atofTest{ "1e-309", "1e-309", nil },
- atofTest{ "1e-310", "1e-310", nil },
- atofTest{ "1e-322", "1e-322", nil },
+ atofTest{"1e-305", "1e-305", nil},
+ atofTest{"1e-306", "1e-306", nil},
+ atofTest{"1e-307", "1e-307", nil},
+ atofTest{"1e-308", "1e-308", nil},
+ atofTest{"1e-309", "1e-309", nil},
+ atofTest{"1e-310", "1e-310", nil},
+ atofTest{"1e-322", "1e-322", nil},
// smallest denormal
- atofTest{ "5e-324", "5e-324", nil },
+ atofTest{"5e-324", "5e-324", nil},
// too small
- atofTest{ "4e-324", "0", nil },
+ atofTest{"4e-324", "0", nil},
// way too small
- atofTest{ "1e-350", "0", nil },
- atofTest{ "1e-400000", "0", nil },
+ atofTest{"1e-350", "0", nil},
+ atofTest{"1e-400000", "0", nil},
// try to overflow exponent
- atofTest{ "1e-4294967296", "0", nil },
- atofTest{ "1e+4294967296", "+Inf", os.ERANGE },
- atofTest{ "1e-18446744073709551616", "0", nil },
- atofTest{ "1e+18446744073709551616", "+Inf", os.ERANGE },
+ atofTest{"1e-4294967296", "0", nil},
+ atofTest{"1e+4294967296", "+Inf", os.ERANGE},
+ atofTest{"1e-18446744073709551616", "0", nil},
+ atofTest{"1e+18446744073709551616", "+Inf", os.ERANGE},
// Parse errors
- atofTest{ "1e", "0", os.EINVAL },
- atofTest{ "1e-", "0", os.EINVAL },
- atofTest{ ".e-1", "0", os.EINVAL },
+ atofTest{"1e", "0", os.EINVAL},
+ atofTest{"1e-", "0", os.EINVAL},
+ atofTest{".e-1", "0", os.EINVAL},
}
func init() {
for i := range atoftests {
test := &atoftests[i];
if test.err != nil {
- test.err = &NumError{test.in, test.err}
+ test.err = &NumError{test.in, test.err};
}
}
}
import "os"
type NumError struct {
- Num string;
- Error os.Error;
+ Num string;
+ Error os.Error;
}
func (e *NumError) String() string {
func computeIntsize() uint {
siz := uint(8);
for 1<<siz != 0 {
- siz *= 2
+ siz *= 2;
}
- return siz
+ return siz;
}
-var IntSize = computeIntsize();
+
+var IntSize = computeIntsize()
// Return the first number n such that n*base >= 1<<64.
func cutoff64(base int) uint64 {
if base < 2 {
return 0;
}
- return (1<<64 - 1) / uint64(base) + 1;
+ return (1<<64 - 1)/uint64(base) + 1;
}
// Btoui64 interprets a string s in an arbitrary base b (2 to 36)
var v byte;
switch {
case '0' <= s[i] && s[i] <= '9':
- v = s[i] - '0';
+ v = s[i]-'0';
case 'a' <= s[i] && s[i] <= 'z':
- v = s[i] - 'a' + 10;
+ v = s[i]-'a'+10;
case 'A' <= s[i] && s[i] <= 'Z':
- v = s[i] - 'A' + 10;
+ v = s[i]-'A'+10;
default:
n = 0;
err = os.EINVAL;
if n >= cutoff {
// n*b overflows
- n = 1<<64-1;
+ n = 1<<64 - 1;
err = os.ERANGE;
goto Error;
}
n1 := n+uint64(v);
if n1 < n {
// n+v overflows
- n = 1<<64-1;
+ n = 1<<64 - 1;
err = os.ERANGE;
goto Error;
}
func Atoui64(s string) (n uint64, err os.Error) {
// Empty string bad.
if len(s) == 0 {
- return 0, &NumError{s, os.EINVAL}
+ return 0, &NumError{s, os.EINVAL};
}
// Look for octal, hex prefix.
func Atoi64(s string) (i int64, err os.Error) {
// Empty string bad.
if len(s) == 0 {
- return 0, &NumError{s, os.EINVAL}
+ return 0, &NumError{s, os.EINVAL};
}
// Pick off leading sign.
s0 := s;
neg := false;
if s[0] == '+' {
- s = s[1:len(s)]
+ s = s[1:len(s)];
} else if s[0] == '-' {
neg = true;
- s = s[1:len(s)]
+ s = s[1:len(s)];
}
// Convert unsigned and check range.
un, err = Atoui64(s);
if err != nil && err.(*NumError).Error != os.ERANGE {
err.(*NumError).Num = s0;
- return 0, err
+ return 0, err;
}
if !neg && un >= 1<<63 {
- return 1<<63-1, &NumError{s0, os.ERANGE}
+ return 1<<63 - 1, &NumError{s0, os.ERANGE};
}
if neg && un > 1<<63 {
- return -1<<63, &NumError{s0, os.ERANGE}
+ return -1 << 63, &NumError{s0, os.ERANGE};
}
n := int64(un);
if neg {
- n = -n
+ n = -n;
}
- return n, nil
+ return n, nil;
}
// Atoui is like Atoui64 but returns its result as a uint.
func Atoui(s string) (i uint, err os.Error) {
i1, e1 := Atoui64(s);
if e1 != nil && e1.(*NumError).Error != os.ERANGE {
- return 0, e1
+ return 0, e1;
}
i = uint(i1);
if uint64(i) != i1 {
- return ^uint(0), &NumError{s, os.ERANGE}
+ return ^uint(0), &NumError{s, os.ERANGE};
}
- return i, nil
+ return i, nil;
}
// Atoi is like Atoi64 but returns its result as an int.
func Atoi(s string) (i int, err os.Error) {
i1, e1 := Atoi64(s);
if e1 != nil && e1.(*NumError).Error != os.ERANGE {
- return 0, e1
+ return 0, e1;
}
i = int(i1);
if int64(i) != i1 {
if i1 < 0 {
- return -1<<(IntSize-1), &NumError{s, os.ERANGE}
+ return -1 << (IntSize-1), &NumError{s, os.ERANGE};
}
- return 1<<(IntSize-1) - 1, &NumError{s, os.ERANGE}
+ return 1<<(IntSize-1) - 1, &NumError{s, os.ERANGE};
}
- return i, nil
+ return i, nil;
}
-
package strconv_test
import (
- "os";
- "reflect";
- . "strconv";
- "testing";
+ "os";
+ "reflect";
+ . "strconv";
+ "testing";
)
type atoui64Test struct {
- in string;
- out uint64;
- err os.Error;
+ in string;
+ out uint64;
+ err os.Error;
}
-var atoui64tests = []atoui64Test {
+var atoui64tests = []atoui64Test{
atoui64Test{"", 0, os.EINVAL},
atoui64Test{"0", 0, nil},
atoui64Test{"1", 1, nil},
atoui64Test{"0X12345", 0x12345, nil},
atoui64Test{"12345x", 0, os.EINVAL},
atoui64Test{"98765432100", 98765432100, nil},
- atoui64Test{"18446744073709551615", 1<<64-1, nil},
- atoui64Test{"18446744073709551616", 1<<64-1, os.ERANGE},
- atoui64Test{"18446744073709551620", 1<<64-1, os.ERANGE},
- atoui64Test{"0xFFFFFFFFFFFFFFFF", 1<<64-1, nil},
- atoui64Test{"0x10000000000000000", 1<<64-1, os.ERANGE},
- atoui64Test{"01777777777777777777777", 1<<64-1, nil},
+ atoui64Test{"18446744073709551615", 1<<64 - 1, nil},
+ atoui64Test{"18446744073709551616", 1<<64 - 1, os.ERANGE},
+ atoui64Test{"18446744073709551620", 1<<64 - 1, os.ERANGE},
+ atoui64Test{"0xFFFFFFFFFFFFFFFF", 1<<64 - 1, nil},
+ atoui64Test{"0x10000000000000000", 1<<64 - 1, os.ERANGE},
+ atoui64Test{"01777777777777777777777", 1<<64 - 1, nil},
atoui64Test{"01777777777777777777778", 0, os.EINVAL},
- atoui64Test{"02000000000000000000000", 1<<64-1, os.ERANGE},
+ atoui64Test{"02000000000000000000000", 1<<64 - 1, os.ERANGE},
atoui64Test{"0200000000000000000000", 1<<61, nil},
}
type atoi64Test struct {
- in string;
- out int64;
- err os.Error;
+ in string;
+ out int64;
+ err os.Error;
}
-var atoi64tests = []atoi64Test {
+var atoi64tests = []atoi64Test{
atoi64Test{"", 0, os.EINVAL},
atoi64Test{"0", 0, nil},
atoi64Test{"-0", 0, nil},
atoi64Test{"-12345x", 0, os.EINVAL},
atoi64Test{"98765432100", 98765432100, nil},
atoi64Test{"-98765432100", -98765432100, nil},
- atoi64Test{"9223372036854775807", 1<<63-1, nil},
- atoi64Test{"-9223372036854775807", -(1<<63-1), nil},
- atoi64Test{"9223372036854775808", 1<<63-1, os.ERANGE},
- atoi64Test{"-9223372036854775808", -1<<63, nil},
- atoi64Test{"9223372036854775809", 1<<63-1, os.ERANGE},
- atoi64Test{"-9223372036854775809", -1<<63, os.ERANGE},
+ atoi64Test{"9223372036854775807", 1<<63 - 1, nil},
+ atoi64Test{"-9223372036854775807", -(1<<63 - 1), nil},
+ atoi64Test{"9223372036854775808", 1<<63 - 1, os.ERANGE},
+ atoi64Test{"-9223372036854775808", -1 << 63, nil},
+ atoi64Test{"9223372036854775809", 1<<63 - 1, os.ERANGE},
+ atoi64Test{"-9223372036854775809", -1 << 63, os.ERANGE},
}
type atoui32Test struct {
- in string;
- out uint32;
- err os.Error;
+ in string;
+ out uint32;
+ err os.Error;
}
-var atoui32tests = []atoui32Test {
+var atoui32tests = []atoui32Test{
atoui32Test{"", 0, os.EINVAL},
atoui32Test{"0", 0, nil},
atoui32Test{"1", 1, nil},
atoui32Test{"0X12345", 0x12345, nil},
atoui32Test{"12345x", 0, os.EINVAL},
atoui32Test{"987654321", 987654321, nil},
- atoui32Test{"4294967295", 1<<32-1, nil},
- atoui32Test{"4294967296", 1<<32-1, os.ERANGE},
+ atoui32Test{"4294967295", 1<<32 - 1, nil},
+ atoui32Test{"4294967296", 1<<32 - 1, os.ERANGE},
}
type atoi32Test struct {
- in string;
- out int32;
- err os.Error;
+ in string;
+ out int32;
+ err os.Error;
}
-var atoi32tests = []atoi32Test {
+var atoi32tests = []atoi32Test{
atoi32Test{"", 0, os.EINVAL},
atoi32Test{"0", 0, nil},
atoi32Test{"-0", 0, nil},
atoi32Test{"-12345x", 0, os.EINVAL},
atoi32Test{"987654321", 987654321, nil},
atoi32Test{"-987654321", -987654321, nil},
- atoi32Test{"2147483647", 1<<31-1, nil},
- atoi32Test{"-2147483647", -(1<<31-1), nil},
- atoi32Test{"2147483648", 1<<31-1, os.ERANGE},
- atoi32Test{"-2147483648", -1<<31, nil},
- atoi32Test{"2147483649", 1<<31-1, os.ERANGE},
- atoi32Test{"-2147483649", -1<<31, os.ERANGE},
+ atoi32Test{"2147483647", 1<<31 - 1, nil},
+ atoi32Test{"-2147483647", -(1<<31 - 1), nil},
+ atoi32Test{"2147483648", 1<<31 - 1, os.ERANGE},
+ atoi32Test{"-2147483648", -1 << 31, nil},
+ atoi32Test{"2147483649", 1<<31 - 1, os.ERANGE},
+ atoi32Test{"-2147483649", -1 << 31, os.ERANGE},
}
func init() {
for i := range atoui64tests {
test := &atoui64tests[i];
if test.err != nil {
- test.err = &NumError{test.in, test.err}
+ test.err = &NumError{test.in, test.err};
}
}
for i := range atoi64tests {
test := &atoi64tests[i];
if test.err != nil {
- test.err = &NumError{test.in, test.err}
+ test.err = &NumError{test.in, test.err};
}
}
for i := range atoui32tests {
test := &atoui32tests[i];
if test.err != nil {
- test.err = &NumError{test.in, test.err}
+ test.err = &NumError{test.in, test.err};
}
}
for i := range atoi32tests {
test := &atoi32tests[i];
if test.err != nil {
- test.err = &NumError{test.in, test.err}
+ test.err = &NumError{test.in, test.err};
}
}
}
}
}
}
-
package strconv_test
import (
- . "strconv";
- "testing";
+ . "strconv";
+ "testing";
)
type shiftTest struct {
- i uint64;
- shift int;
- out string;
+ i uint64;
+ shift int;
+ out string;
}
-var shifttests = []shiftTest {
- shiftTest{ 0, -100, "0" },
- shiftTest{ 0, 100, "0" },
- shiftTest{ 1, 100, "1267650600228229401496703205376" },
- shiftTest{ 1, -100,
+var shifttests = []shiftTest{
+ shiftTest{0, -100, "0"},
+ shiftTest{0, 100, "0"},
+ shiftTest{1, 100, "1267650600228229401496703205376"},
+ shiftTest{1, -100,
"0.00000000000000000000000000000078886090522101180541"
- "17285652827862296732064351090230047702789306640625" },
- shiftTest{ 12345678, 8, "3160493568" },
- shiftTest{ 12345678, -8, "48225.3046875" },
- shiftTest{ 195312, 9, "99999744" },
- shiftTest{ 1953125, 9, "1000000000" },
+ "17285652827862296732064351090230047702789306640625",
+ },
+ shiftTest{12345678, 8, "3160493568"},
+ shiftTest{12345678, -8, "48225.3046875"},
+ shiftTest{195312, 9, "99999744"},
+ shiftTest{1953125, 9, "1000000000"},
}
func TestDecimalShift(t *testing.T) {
}
type roundTest struct {
- i uint64;
- nd int;
- down, round, up string;
- int uint64;
+ i uint64;
+ nd int;
+ down, round, up string;
+ int uint64;
}
-var roundtests = []roundTest {
- roundTest{ 0, 4, "0", "0", "0", 0 },
- roundTest{ 12344999, 4, "12340000", "12340000", "12350000", 12340000 },
- roundTest{ 12345000, 4, "12340000", "12340000", "12350000", 12340000 },
- roundTest{ 12345001, 4, "12340000", "12350000", "12350000", 12350000 },
- roundTest{ 23454999, 4, "23450000", "23450000", "23460000", 23450000 },
- roundTest{ 23455000, 4, "23450000", "23460000", "23460000", 23460000 },
- roundTest{ 23455001, 4, "23450000", "23460000", "23460000", 23460000 },
+var roundtests = []roundTest{
+ roundTest{0, 4, "0", "0", "0", 0},
+ roundTest{12344999, 4, "12340000", "12340000", "12350000", 12340000},
+ roundTest{12345000, 4, "12340000", "12340000", "12350000", 12340000},
+ roundTest{12345001, 4, "12340000", "12350000", "12350000", 12350000},
+ roundTest{23454999, 4, "23450000", "23450000", "23460000", 23450000},
+ roundTest{23455000, 4, "23450000", "23460000", "23460000", 23460000},
+ roundTest{23455001, 4, "23450000", "23460000", "23460000", 23460000},
- roundTest{ 99994999, 4, "99990000", "99990000", "100000000", 99990000 },
- roundTest{ 99995000, 4, "99990000", "100000000", "100000000", 100000000 },
- roundTest{ 99999999, 4, "99990000", "100000000", "100000000", 100000000 },
+ roundTest{99994999, 4, "99990000", "99990000", "100000000", 99990000},
+ roundTest{99995000, 4, "99990000", "100000000", "100000000", 100000000},
+ roundTest{99999999, 4, "99990000", "100000000", "100000000", 100000000},
- roundTest{ 12994999, 4, "12990000", "12990000", "13000000", 12990000 },
- roundTest{ 12995000, 4, "12990000", "13000000", "13000000", 13000000 },
- roundTest{ 12999999, 4, "12990000", "13000000", "13000000", 13000000 },
+ roundTest{12994999, 4, "12990000", "12990000", "13000000", 12990000},
+ roundTest{12995000, 4, "12990000", "13000000", "13000000", 13000000},
+ roundTest{12999999, 4, "12990000", "13000000", "13000000", 13000000},
}
func TestDecimalRound(t *testing.T) {
}
type roundIntTest struct {
- i uint64;
- shift int;
- int uint64;
+ i uint64;
+ shift int;
+ int uint64;
}
-var roundinttests = []roundIntTest {
- roundIntTest{ 0, 100, 0 },
- roundIntTest{ 512, -8, 2 },
- roundIntTest{ 513, -8, 2 },
- roundIntTest{ 640, -8, 2 },
- roundIntTest{ 641, -8, 3 },
- roundIntTest{ 384, -8, 2 },
- roundIntTest{ 385, -8, 2 },
- roundIntTest{ 383, -8, 1 },
- roundIntTest{ 1, 100, 1<<64-1 },
- roundIntTest{ 1000, 0, 1000 },
+var roundinttests = []roundIntTest{
+ roundIntTest{0, 100, 0},
+ roundIntTest{512, -8, 2},
+ roundIntTest{513, -8, 2},
+ roundIntTest{640, -8, 2},
+ roundIntTest{641, -8, 3},
+ roundIntTest{384, -8, 2},
+ roundIntTest{385, -8, 2},
+ roundIntTest{383, -8, 1},
+ roundIntTest{1, 100, 1<<64 - 1},
+ roundIntTest{1000, 0, 1000},
}
func TestDecimalRoundedInteger(t *testing.T) {
// license that can be found in the LICENSE file.
package strconv_test
+
import (
"bufio";
"fmt";
case i == 1:
return 2;
}
- return pow2(i/2) * pow2(i-i/2);
+ return pow2(i/2) * pow2(i - i/2);
}
// Wrapper around strconv.Atof64. Handles dddddp+ddd (binary exponent)
if err2 != nil {
panicln("testfp: read testfp.txt:", err2.String());
}
- line = line[0:len(line)-1];
+ line = line[0 : len(line)-1];
lineno++;
if len(line) == 0 || line[0] == '#' {
- continue
+ continue;
}
a := strings.Split(line, " ", 0);
if len(a) != 4 {
package strconv_test
import (
- "math";
- . "strconv";
- "testing";
+ "math";
+ . "strconv";
+ "testing";
)
type ftoaTest struct {
- f float64;
- fmt byte;
- prec int;
- s string;
+ f float64;
+ fmt byte;
+ prec int;
+ s string;
}
-func fdiv(a, b float64) float64 { return a / b } // keep compiler in the dark
+func fdiv(a, b float64) float64 {
+ return a/b;
+} // keep compiler in the dark
const (
- below1e23 = 99999999999999974834176;
- above1e23 = 100000000000000008388608;
+ below1e23 = 99999999999999974834176;
+ above1e23 = 100000000000000008388608;
)
-var ftoatests = []ftoaTest {
- ftoaTest{ 1, 'e', 5, "1.00000e+00" },
- ftoaTest{ 1, 'f', 5, "1.00000" },
- ftoaTest{ 1, 'g', 5, "1" },
- ftoaTest{ 1, 'g', -1, "1" },
- ftoaTest{ 20, 'g', -1, "20" },
- ftoaTest{ 1234567.8, 'g', -1, "1.2345678e+06" },
- ftoaTest{ 200000, 'g', -1, "200000" },
- ftoaTest{ 2000000, 'g', -1, "2e+06" },
-
- ftoaTest{ 0, 'e', 5, "0.00000e+00" },
- ftoaTest{ 0, 'f', 5, "0.00000" },
- ftoaTest{ 0, 'g', 5, "0" },
- ftoaTest{ 0, 'g', -1, "0" },
-
- ftoaTest{ -1, 'e', 5, "-1.00000e+00" },
- ftoaTest{ -1, 'f', 5, "-1.00000" },
- ftoaTest{ -1, 'g', 5, "-1" },
- ftoaTest{ -1, 'g', -1, "-1" },
-
- ftoaTest{ 12, 'e', 5, "1.20000e+01" },
- ftoaTest{ 12, 'f', 5, "12.00000" },
- ftoaTest{ 12, 'g', 5, "12" },
- ftoaTest{ 12, 'g', -1, "12" },
-
- ftoaTest{ 123456700, 'e', 5, "1.23457e+08" },
- ftoaTest{ 123456700, 'f', 5, "123456700.00000" },
- ftoaTest{ 123456700, 'g', 5, "1.2346e+08" },
- ftoaTest{ 123456700, 'g', -1, "1.234567e+08" },
-
- ftoaTest{ 1.2345e6, 'e', 5, "1.23450e+06" },
- ftoaTest{ 1.2345e6, 'f', 5, "1234500.00000" },
- ftoaTest{ 1.2345e6, 'g', 5, "1.2345e+06" },
-
- ftoaTest{ 1e23, 'e', 17, "9.99999999999999916e+22" },
- ftoaTest{ 1e23, 'f', 17, "99999999999999991611392.00000000000000000" },
- ftoaTest{ 1e23, 'g', 17, "9.9999999999999992e+22" },
-
- ftoaTest{ 1e23, 'e', -1, "1e+23" },
- ftoaTest{ 1e23, 'f', -1, "100000000000000000000000" },
- ftoaTest{ 1e23, 'g', -1, "1e+23" },
-
- ftoaTest{ below1e23, 'e', 17, "9.99999999999999748e+22" },
- ftoaTest{ below1e23, 'f', 17, "99999999999999974834176.00000000000000000" },
- ftoaTest{ below1e23, 'g', 17, "9.9999999999999975e+22" },
-
- ftoaTest{ below1e23, 'e', -1, "9.999999999999997e+22" },
- ftoaTest{ below1e23, 'f', -1, "99999999999999970000000" },
- ftoaTest{ below1e23, 'g', -1, "9.999999999999997e+22" },
-
- ftoaTest{ above1e23, 'e', 17, "1.00000000000000008e+23" },
- ftoaTest{ above1e23, 'f', 17, "100000000000000008388608.00000000000000000" },
- ftoaTest{ above1e23, 'g', 17, "1.0000000000000001e+23" },
-
- ftoaTest{ above1e23, 'e', -1, "1.0000000000000001e+23" },
- ftoaTest{ above1e23, 'f', -1, "100000000000000010000000" },
- ftoaTest{ above1e23, 'g', -1, "1.0000000000000001e+23" },
-
- ftoaTest{ fdiv(5e-304, 1e20), 'g', -1, "5e-324" },
- ftoaTest{ fdiv(-5e-304, 1e20), 'g', -1, "-5e-324" },
-
- ftoaTest{ 32, 'g', -1, "32" },
- ftoaTest{ 32, 'g', 0, "3e+01" },
-
- ftoaTest{ 100, 'x', -1, "%x" },
-
- ftoaTest{ math.NaN(), 'g', -1, "NaN" },
- ftoaTest{ -math.NaN(), 'g', -1, "NaN" },
- ftoaTest{ math.Inf(0), 'g', -1, "+Inf" },
- ftoaTest{ math.Inf(-1), 'g', -1, "-Inf" },
- ftoaTest{ -math.Inf(0), 'g', -1, "-Inf" },
-
- ftoaTest{ -1, 'b', -1, "-4503599627370496p-52" },
+var ftoatests = []ftoaTest{
+ ftoaTest{1, 'e', 5, "1.00000e+00"},
+ ftoaTest{1, 'f', 5, "1.00000"},
+ ftoaTest{1, 'g', 5, "1"},
+ ftoaTest{1, 'g', -1, "1"},
+ ftoaTest{20, 'g', -1, "20"},
+ ftoaTest{1234567.8, 'g', -1, "1.2345678e+06"},
+ ftoaTest{200000, 'g', -1, "200000"},
+ ftoaTest{2000000, 'g', -1, "2e+06"},
+
+ ftoaTest{0, 'e', 5, "0.00000e+00"},
+ ftoaTest{0, 'f', 5, "0.00000"},
+ ftoaTest{0, 'g', 5, "0"},
+ ftoaTest{0, 'g', -1, "0"},
+
+ ftoaTest{-1, 'e', 5, "-1.00000e+00"},
+ ftoaTest{-1, 'f', 5, "-1.00000"},
+ ftoaTest{-1, 'g', 5, "-1"},
+ ftoaTest{-1, 'g', -1, "-1"},
+
+ ftoaTest{12, 'e', 5, "1.20000e+01"},
+ ftoaTest{12, 'f', 5, "12.00000"},
+ ftoaTest{12, 'g', 5, "12"},
+ ftoaTest{12, 'g', -1, "12"},
+
+ ftoaTest{123456700, 'e', 5, "1.23457e+08"},
+ ftoaTest{123456700, 'f', 5, "123456700.00000"},
+ ftoaTest{123456700, 'g', 5, "1.2346e+08"},
+ ftoaTest{123456700, 'g', -1, "1.234567e+08"},
+
+ ftoaTest{1.2345e6, 'e', 5, "1.23450e+06"},
+ ftoaTest{1.2345e6, 'f', 5, "1234500.00000"},
+ ftoaTest{1.2345e6, 'g', 5, "1.2345e+06"},
+
+ ftoaTest{1e23, 'e', 17, "9.99999999999999916e+22"},
+ ftoaTest{1e23, 'f', 17, "99999999999999991611392.00000000000000000"},
+ ftoaTest{1e23, 'g', 17, "9.9999999999999992e+22"},
+
+ ftoaTest{1e23, 'e', -1, "1e+23"},
+ ftoaTest{1e23, 'f', -1, "100000000000000000000000"},
+ ftoaTest{1e23, 'g', -1, "1e+23"},
+
+ ftoaTest{below1e23, 'e', 17, "9.99999999999999748e+22"},
+ ftoaTest{below1e23, 'f', 17, "99999999999999974834176.00000000000000000"},
+ ftoaTest{below1e23, 'g', 17, "9.9999999999999975e+22"},
+
+ ftoaTest{below1e23, 'e', -1, "9.999999999999997e+22"},
+ ftoaTest{below1e23, 'f', -1, "99999999999999970000000"},
+ ftoaTest{below1e23, 'g', -1, "9.999999999999997e+22"},
+
+ ftoaTest{above1e23, 'e', 17, "1.00000000000000008e+23"},
+ ftoaTest{above1e23, 'f', 17, "100000000000000008388608.00000000000000000"},
+ ftoaTest{above1e23, 'g', 17, "1.0000000000000001e+23"},
+
+ ftoaTest{above1e23, 'e', -1, "1.0000000000000001e+23"},
+ ftoaTest{above1e23, 'f', -1, "100000000000000010000000"},
+ ftoaTest{above1e23, 'g', -1, "1.0000000000000001e+23"},
+
+ ftoaTest{fdiv(5e-304, 1e20), 'g', -1, "5e-324"},
+ ftoaTest{fdiv(-5e-304, 1e20), 'g', -1, "-5e-324"},
+
+ ftoaTest{32, 'g', -1, "32"},
+ ftoaTest{32, 'g', 0, "3e+01"},
+
+ ftoaTest{100, 'x', -1, "%x"},
+
+ ftoaTest{math.NaN(), 'g', -1, "NaN"},
+ ftoaTest{-math.NaN(), 'g', -1, "NaN"},
+ ftoaTest{math.Inf(0), 'g', -1, "+Inf"},
+ ftoaTest{math.Inf(-1), 'g', -1, "-Inf"},
+ ftoaTest{-math.Inf(0), 'g', -1, "-Inf"},
+
+ ftoaTest{-1, 'b', -1, "-4503599627370496p-52"},
}
func TestFtoa(t *testing.T) {
optimize = b;
return old;
}
-
// Uitob64 returns the string representation of i in the given base.
func Uitob64(u uint64, base uint) string {
if u == 0 {
- return "0"
+ return "0";
}
// Assemble decimal in reverse order.
u /= b;
}
- return string(buf[j:len(buf)])
+ return string(buf[j:len(buf)]);
}
// Itob64 returns the string representation of i in the given base.
func Itob64(i int64, base uint) string {
if i == 0 {
- return "0"
+ return "0";
}
if i < 0 {
package strconv_test
import (
- . "strconv";
- "testing";
+ . "strconv";
+ "testing";
)
type itob64Test struct {
- in int64;
- base uint;
- out string;
+ in int64;
+ base uint;
+ out string;
}
-var itob64tests = []itob64Test {
- itob64Test{ 0, 10, "0" },
- itob64Test{ 1, 10, "1" },
- itob64Test{ -1, 10, "-1" },
- itob64Test{ 12345678, 10, "12345678" },
- itob64Test{ -987654321, 10, "-987654321" },
- itob64Test{ 1<<31-1, 10, "2147483647" },
- itob64Test{ -1<<31+1, 10, "-2147483647" },
- itob64Test{ 1<<31, 10, "2147483648" },
- itob64Test{ -1<<31, 10, "-2147483648" },
- itob64Test{ 1<<31+1, 10, "2147483649" },
- itob64Test{ -1<<31-1, 10, "-2147483649" },
- itob64Test{ 1<<32-1, 10, "4294967295" },
- itob64Test{ -1<<32+1, 10, "-4294967295" },
- itob64Test{ 1<<32, 10, "4294967296" },
- itob64Test{ -1<<32, 10, "-4294967296" },
- itob64Test{ 1<<32+1, 10, "4294967297" },
- itob64Test{ -1<<32-1, 10, "-4294967297" },
- itob64Test{ 1<<50, 10, "1125899906842624" },
- itob64Test{ 1<<63-1, 10, "9223372036854775807" },
- itob64Test{ -1<<63+1, 10, "-9223372036854775807" },
- itob64Test{ -1<<63, 10, "-9223372036854775808" },
-
- itob64Test{ 0, 2, "0" },
- itob64Test{ 10, 2, "1010" },
- itob64Test{ -1, 2, "-1" },
- itob64Test{ 1<<15, 2, "1000000000000000" },
-
- itob64Test{ -8, 8, "-10" },
- itob64Test{ 057635436545, 8, "57635436545" },
- itob64Test{ 1<<24, 8, "100000000" },
-
- itob64Test{ 16, 16, "10" },
- itob64Test{ -0x123456789abcdef, 16, "-123456789abcdef" },
- itob64Test{ 1<<63-1, 16, "7fffffffffffffff" },
-
- itob64Test{ 16, 17, "g" },
- itob64Test{ 25, 25, "10" },
- itob64Test{ (((((17*35+24)*35+21)*35+34)*35+12)*35+24)*35+32, 35, "holycow" },
- itob64Test{ (((((17*36+24)*36+21)*36+34)*36+12)*36+24)*36+32, 36, "holycow" },
+var itob64tests = []itob64Test{
+ itob64Test{0, 10, "0"},
+ itob64Test{1, 10, "1"},
+ itob64Test{-1, 10, "-1"},
+ itob64Test{12345678, 10, "12345678"},
+ itob64Test{-987654321, 10, "-987654321"},
+ itob64Test{1<<31 - 1, 10, "2147483647"},
+ itob64Test{-1 << 31 + 1, 10, "-2147483647"},
+ itob64Test{1<<31, 10, "2147483648"},
+ itob64Test{-1 << 31, 10, "-2147483648"},
+ itob64Test{1<<31 + 1, 10, "2147483649"},
+ itob64Test{-1 << 31 - 1, 10, "-2147483649"},
+ itob64Test{1<<32 - 1, 10, "4294967295"},
+ itob64Test{-1 << 32 + 1, 10, "-4294967295"},
+ itob64Test{1<<32, 10, "4294967296"},
+ itob64Test{-1 << 32, 10, "-4294967296"},
+ itob64Test{1<<32 + 1, 10, "4294967297"},
+ itob64Test{-1 << 32 - 1, 10, "-4294967297"},
+ itob64Test{1<<50, 10, "1125899906842624"},
+ itob64Test{1<<63 - 1, 10, "9223372036854775807"},
+ itob64Test{-1 << 63 + 1, 10, "-9223372036854775807"},
+ itob64Test{-1 << 63, 10, "-9223372036854775808"},
+
+ itob64Test{0, 2, "0"},
+ itob64Test{10, 2, "1010"},
+ itob64Test{-1, 2, "-1"},
+ itob64Test{1<<15, 2, "1000000000000000"},
+
+ itob64Test{-8, 8, "-10"},
+ itob64Test{057635436545, 8, "57635436545"},
+ itob64Test{1<<24, 8, "100000000"},
+
+ itob64Test{16, 16, "10"},
+ itob64Test{-0x123456789abcdef, 16, "-123456789abcdef"},
+ itob64Test{1<<63 - 1, 16, "7fffffffffffffff"},
+
+ itob64Test{16, 17, "g"},
+ itob64Test{25, 25, "10"},
+ itob64Test{(((((17*35 + 24)*35 + 21)*35 + 34)*35 + 12)*35 + 24)*35 + 32, 35, "holycow"},
+ itob64Test{(((((17*36 + 24)*36 + 21)*36 + 34)*36 + 12)*36 + 24)*36 + 32, 36, "holycow"},
}
func TestItoa(t *testing.T) {
test.in, test.base, s, test.out);
}
- if test.in >= 0 {
+ if test.in >= 0 {
s := Uitob(uint(test.in), test.base);
if s != test.out {
t.Errorf("Uitob(%v, %v) = %v want %v\n",
}
type uitob64Test struct {
- in uint64;
- base uint;
- out string;
+ in uint64;
+ base uint;
+ out string;
}
-var uitob64tests = []uitob64Test {
- uitob64Test{ 1<<63-1, 10, "9223372036854775807" },
- uitob64Test{ 1<<63, 10, "9223372036854775808" },
- uitob64Test{ 1<<63+1, 10, "9223372036854775809" },
- uitob64Test{ 1<<64-2, 10, "18446744073709551614" },
- uitob64Test{ 1<<64-1, 10, "18446744073709551615" },
+var uitob64tests = []uitob64Test{
+ uitob64Test{1<<63 - 1, 10, "9223372036854775807"},
+ uitob64Test{1<<63, 10, "9223372036854775808"},
+ uitob64Test{1<<63 + 1, 10, "9223372036854775809"},
+ uitob64Test{1<<64 - 2, 10, "18446744073709551614"},
+ uitob64Test{1<<64 - 1, 10, "18446744073709551615"},
}
func TestUitoa(t *testing.T) {
t += `\v`;
case c < utf8.RuneSelf:
- t += `\x` + string(lowerhex[c>>4]) + string(lowerhex[c&0xF]);
+ t += `\x`+string(lowerhex[c>>4])+string(lowerhex[c&0xF]);
case utf8.FullRuneInString(s):
r, size := utf8.DecodeRuneInString(s);
if r == utf8.RuneError && size == 1 {
goto EscX;
}
- s = s[size-1:len(s)]; // next iteration will slice off 1 more
+ s = s[size-1 : len(s)]; // next iteration will slice off 1 more
if r < 0x10000 {
t += `\u`;
- for j:=uint(0); j<4; j++ {
- t += string(lowerhex[(r>>(12-4*j))&0xF]);
+ for j := uint(0); j < 4; j++ {
+ t += string(lowerhex[(r>>(12 - 4*j))&0xF]);
}
} else {
t += `\U`;
- for j:=uint(0); j<8; j++ {
- t += string(lowerhex[(r>>(28-4*j))&0xF]);
+ for j := uint(0); j < 8; j++ {
+ t += string(lowerhex[(r>>(28 - 4*j))&0xF]);
}
}
c := int(b);
switch {
case '0' <= c && c <= '9':
- return c - '0', true;
+ return c-'0', true;
case 'a' <= c && c <= 'f':
- return c - 'a' + 10, true;
+ return c-'a'+10, true;
case 'A' <= c && c <= 'F':
- return c - 'A' + 10, true;
+ return c-'A'+10, true;
}
return;
}
// UnquoteChar decodes the first character or byte in the escaped string
// or character literal represented by the string s.
-// It returns four values:
+// It returns four values:
// 1) value, the decoded Unicode code point or byte value;
// 2) multibyte, a boolean indicating whether the decoded character
// requires a multibyte UTF-8 representation;
value = v;
multibyte = true;
case '0', '1', '2', '3', '4', '5', '6', '7':
- v := int(c) - '0';
+ v := int(c)-'0';
if len(s) < 2 {
err = os.EINVAL;
return;
}
for j := 0; j < 2; j++ { // one digit already; two more
- x := int(s[j]) - '0';
+ x := int(s[j])-'0';
if x < 0 || x > 7 {
return;
}
- v = (v<<3) | x;
+ v = (v<<3)|x;
}
s = s[2:len(s)];
if v > 255 {
// character literal; Unquote returns the corresponding
// one-character string.)
func Unquote(s string) (t string, err os.Error) {
- err = os.EINVAL; // assume error for easy return
+ err = os.EINVAL; // assume error for easy return
n := len(s);
if n < 2 {
return;
if quote != s[n-1] {
return;
}
- s = s[1:n-1];
+ s = s[1 : n-1];
if quote == '`' {
return s, nil;
return;
}
}
- return tt, nil
+ return tt, nil;
}
package strconv_test
import (
- "os";
- . "strconv";
- "testing";
+ "os";
+ . "strconv";
+ "testing";
)
type quoteTest struct {
- in string;
- out string;
+ in string;
+ out string;
}
-var quotetests = []quoteTest {
- quoteTest{ "\a\b\f\r\n\t\v", `"\a\b\f\r\n\t\v"` },
- quoteTest{ "\\", `"\\"` },
- quoteTest{ "abc\xffdef", `"abc\xffdef"` },
- quoteTest{ "\u263a", `"\u263a"` },
- quoteTest{ "\U0010ffff", `"\U0010ffff"` },
- quoteTest{ "\x04", `"\x04"` },
+var quotetests = []quoteTest{
+ quoteTest{"\a\b\f\r\n\t\v", `"\a\b\f\r\n\t\v"`},
+ quoteTest{"\\", `"\\"`},
+ quoteTest{"abc\xffdef", `"abc\xffdef"`},
+ quoteTest{"\u263a", `"\u263a"`},
+ quoteTest{"\U0010ffff", `"\U0010ffff"`},
+ quoteTest{"\x04", `"\x04"`},
}
func TestQuote(t *testing.T) {
}
type canBackquoteTest struct {
- in string;
- out bool;
+ in string;
+ out bool;
}
-var canbackquotetests = []canBackquoteTest {
- canBackquoteTest{ "`", false },
- canBackquoteTest{ string(0), false },
- canBackquoteTest{ string(1), false },
- canBackquoteTest{ string(2), false },
- canBackquoteTest{ string(3), false },
- canBackquoteTest{ string(4), false },
- canBackquoteTest{ string(5), false },
- canBackquoteTest{ string(6), false },
- canBackquoteTest{ string(7), false },
- canBackquoteTest{ string(8), false },
- canBackquoteTest{ string(9), true }, // \t
- canBackquoteTest{ string(10), false },
- canBackquoteTest{ string(11), false },
- canBackquoteTest{ string(12), false },
- canBackquoteTest{ string(13), false },
- canBackquoteTest{ string(14), false },
- canBackquoteTest{ string(15), false },
- canBackquoteTest{ string(16), false },
- canBackquoteTest{ string(17), false },
- canBackquoteTest{ string(18), false },
- canBackquoteTest{ string(19), false },
- canBackquoteTest{ string(20), false },
- canBackquoteTest{ string(21), false },
- canBackquoteTest{ string(22), false },
- canBackquoteTest{ string(23), false },
- canBackquoteTest{ string(24), false },
- canBackquoteTest{ string(25), false },
- canBackquoteTest{ string(26), false },
- canBackquoteTest{ string(27), false },
- canBackquoteTest{ string(28), false },
- canBackquoteTest{ string(29), false },
- canBackquoteTest{ string(30), false },
- canBackquoteTest{ string(31), false },
- canBackquoteTest{ `' !"#$%&'()*+,-./:;<=>?@[\]^_{|}~`, true },
- canBackquoteTest{ `0123456789`, true },
- canBackquoteTest{ `ABCDEFGHIJKLMNOPQRSTUVWXYZ`, true },
- canBackquoteTest{ `abcdefghijklmnopqrstuvwxyz`, true },
- canBackquoteTest{ `☺`, true },
+var canbackquotetests = []canBackquoteTest{
+ canBackquoteTest{"`", false},
+ canBackquoteTest{string(0), false},
+ canBackquoteTest{string(1), false},
+ canBackquoteTest{string(2), false},
+ canBackquoteTest{string(3), false},
+ canBackquoteTest{string(4), false},
+ canBackquoteTest{string(5), false},
+ canBackquoteTest{string(6), false},
+ canBackquoteTest{string(7), false},
+ canBackquoteTest{string(8), false},
+ canBackquoteTest{string(9), true}, // \t
+ canBackquoteTest{string(10), false},
+ canBackquoteTest{string(11), false},
+ canBackquoteTest{string(12), false},
+ canBackquoteTest{string(13), false},
+ canBackquoteTest{string(14), false},
+ canBackquoteTest{string(15), false},
+ canBackquoteTest{string(16), false},
+ canBackquoteTest{string(17), false},
+ canBackquoteTest{string(18), false},
+ canBackquoteTest{string(19), false},
+ canBackquoteTest{string(20), false},
+ canBackquoteTest{string(21), false},
+ canBackquoteTest{string(22), false},
+ canBackquoteTest{string(23), false},
+ canBackquoteTest{string(24), false},
+ canBackquoteTest{string(25), false},
+ canBackquoteTest{string(26), false},
+ canBackquoteTest{string(27), false},
+ canBackquoteTest{string(28), false},
+ canBackquoteTest{string(29), false},
+ canBackquoteTest{string(30), false},
+ canBackquoteTest{string(31), false},
+ canBackquoteTest{`' !"#$%&'()*+,-./:;<=>?@[\]^_{|}~`, true},
+ canBackquoteTest{`0123456789`, true},
+ canBackquoteTest{`ABCDEFGHIJKLMNOPQRSTUVWXYZ`, true},
+ canBackquoteTest{`abcdefghijklmnopqrstuvwxyz`, true},
+ canBackquoteTest{`☺`, true},
}
func TestCanBackquote(t *testing.T) {
}
}
-var unquotetests = []quoteTest {
- quoteTest{ `""`, "" },
- quoteTest{ `"a"`, "a" },
- quoteTest{ `"abc"`, "abc" },
- quoteTest{ `"☺"`, "☺" },
- quoteTest{ `"hello world"`, "hello world" },
- quoteTest{ `"\xFF"`, "\xFF" },
- quoteTest{ `"\377"`, "\377" },
- quoteTest{ `"\u1234"`, "\u1234" },
- quoteTest{ `"\U00010111"`, "\U00010111" },
- quoteTest{ `"\U0001011111"`, "\U0001011111" },
- quoteTest{ `"\a\b\f\n\r\t\v\\\""`, "\a\b\f\n\r\t\v\\\"" },
- quoteTest{ `"'"`, "'" },
+var unquotetests = []quoteTest{
+ quoteTest{`""`, ""},
+ quoteTest{`"a"`, "a"},
+ quoteTest{`"abc"`, "abc"},
+ quoteTest{`"☺"`, "☺"},
+ quoteTest{`"hello world"`, "hello world"},
+ quoteTest{`"\xFF"`, "\xFF"},
+ quoteTest{`"\377"`, "\377"},
+ quoteTest{`"\u1234"`, "\u1234"},
+ quoteTest{`"\U00010111"`, "\U00010111"},
+ quoteTest{`"\U0001011111"`, "\U0001011111"},
+ quoteTest{`"\a\b\f\n\r\t\v\\\""`, "\a\b\f\n\r\t\v\\\""},
+ quoteTest{`"'"`, "'"},
- quoteTest{ `'a'`, "a" },
- quoteTest{ `'☹'`, "☹" },
- quoteTest{ `'\a'`, "\a" },
- quoteTest{ `'\x10'`, "\x10" },
- quoteTest{ `'\377'`, "\377" },
- quoteTest{ `'\u1234'`, "\u1234" },
- quoteTest{ `'\U00010111'`, "\U00010111" },
- quoteTest{ `'\t'`, "\t" },
- quoteTest{ `' '`, " " },
- quoteTest{ `'\''`, "'" },
- quoteTest{ `'"'`, "\"" },
+ quoteTest{`'a'`, "a"},
+ quoteTest{`'☹'`, "☹"},
+ quoteTest{`'\a'`, "\a"},
+ quoteTest{`'\x10'`, "\x10"},
+ quoteTest{`'\377'`, "\377"},
+ quoteTest{`'\u1234'`, "\u1234"},
+ quoteTest{`'\U00010111'`, "\U00010111"},
+ quoteTest{`'\t'`, "\t"},
+ quoteTest{`' '`, " "},
+ quoteTest{`'\''`, "'"},
+ quoteTest{`'"'`, "\""},
- quoteTest{ "``", `` },
- quoteTest{ "`a`", `a` },
- quoteTest{ "`abc`", `abc` },
- quoteTest{ "`☺`", `☺` },
- quoteTest{ "`hello world`", `hello world` },
- quoteTest{ "`\\xFF`", `\xFF` },
- quoteTest{ "`\\377`", `\377` },
- quoteTest{ "`\\`", `\` },
- quoteTest{ "` `", ` ` },
- quoteTest{ "` `", ` ` },
+ quoteTest{"``", ``},
+ quoteTest{"`a`", `a`},
+ quoteTest{"`abc`", `abc`},
+ quoteTest{"`☺`", `☺`},
+ quoteTest{"`hello world`", `hello world`},
+ quoteTest{"`\\xFF`", `\xFF`},
+ quoteTest{"`\\377`", `\377`},
+ quoteTest{"`\\`", `\`},
+ quoteTest{"` `", ` `},
+ quoteTest{"` `", ` `},
}
-var misquoted = []string {
+var misquoted = []string{
``,
`"`,
`"a`,
package strings_test
import (
- . "strings";
- "testing";
- "unicode";
- "utf8";
+ . "strings";
+ "testing";
+ "unicode";
+ "utf8";
)
func eq(a, b []string) bool {
return true;
}
-var abcd = "abcd";
-var faces = "☺☻☹";
-var commas = "1,2,3,4";
-var dots = "1....2....3....4";
+var abcd = "abcd"
+var faces = "☺☻☹"
+var commas = "1,2,3,4"
+var dots = "1....2....3....4"
type IndexTest struct {
- s string;
- sep string;
- out int;
+ s string;
+ sep string;
+ out int;
}
-var indexTests = []IndexTest {
+var indexTests = []IndexTest{
IndexTest{"", "", 0},
IndexTest{"", "a", -1},
IndexTest{"", "foo", -1},
IndexTest{"abcABCabc", "A", 3},
}
-var lastIndexTests = []IndexTest {
+var lastIndexTests = []IndexTest{
IndexTest{"", "", 0},
IndexTest{"", "a", -1},
IndexTest{"", "foo", -1},
type ExplodeTest struct {
- s string;
- n int;
- a []string;
+ s string;
+ n int;
+ a []string;
}
-var explodetests = []ExplodeTest {
- ExplodeTest{ abcd, 4, []string{"a", "b", "c", "d"} },
- ExplodeTest{ faces, 3, []string{"☺", "☻", "☹"} },
- ExplodeTest{ abcd, 2, []string{"a", "bcd"} },
+
+var explodetests = []ExplodeTest{
+ ExplodeTest{abcd, 4, []string{"a", "b", "c", "d"}},
+ ExplodeTest{faces, 3, []string{"☺", "☻", "☹"}},
+ ExplodeTest{abcd, 2, []string{"a", "bcd"}},
}
+
func TestExplode(t *testing.T) {
for _, tt := range explodetests {
a := Split(tt.s, "", tt.n);
}
type SplitTest struct {
- s string;
- sep string;
- n int;
- a []string;
+ s string;
+ sep string;
+ n int;
+ a []string;
}
-var splittests = []SplitTest {
- SplitTest{ abcd, "a", 0, []string{"", "bcd"} },
- SplitTest{ abcd, "z", 0, []string{"abcd"} },
- SplitTest{ abcd, "", 0, []string{"a", "b", "c", "d"} },
- SplitTest{ commas, ",", 0, []string{"1", "2", "3", "4"} },
- SplitTest{ dots, "...", 0, []string{"1", ".2", ".3", ".4"} },
- SplitTest{ faces, "☹", 0, []string{"☺☻", ""} },
- SplitTest{ faces, "~", 0, []string{faces} },
- SplitTest{ faces, "", 0, []string{"☺", "☻", "☹"} },
- SplitTest{ "1 2 3 4", " ", 3, []string{"1", "2", "3 4"} },
- SplitTest{ "1 2", " ", 3, []string{"1", "2"} },
- SplitTest{ "123", "", 2, []string{"1", "23"} },
- SplitTest{ "123", "", 17, []string{"1", "2", "3"} },
+
+var splittests = []SplitTest{
+ SplitTest{abcd, "a", 0, []string{"", "bcd"}},
+ SplitTest{abcd, "z", 0, []string{"abcd"}},
+ SplitTest{abcd, "", 0, []string{"a", "b", "c", "d"}},
+ SplitTest{commas, ",", 0, []string{"1", "2", "3", "4"}},
+ SplitTest{dots, "...", 0, []string{"1", ".2", ".3", ".4"}},
+ SplitTest{faces, "☹", 0, []string{"☺☻", ""}},
+ SplitTest{faces, "~", 0, []string{faces}},
+ SplitTest{faces, "", 0, []string{"☺", "☻", "☹"}},
+ SplitTest{"1 2 3 4", " ", 3, []string{"1", "2", "3 4"}},
+ SplitTest{"1 2", " ", 3, []string{"1", "2"}},
+ SplitTest{"123", "", 2, []string{"1", "23"}},
+ SplitTest{"123", "", 17, []string{"1", "2", "3"}},
}
+
func TestSplit(t *testing.T) {
for _, tt := range splittests {
a := Split(tt.s, tt.sep, tt.n);
}
}
-var upperTests = []StringTest {
+var upperTests = []StringTest{
StringTest{"", ""},
StringTest{"abc", "ABC"},
StringTest{"AbC123", "ABC123"},
StringTest{"\u0250\u0250\u0250\u0250\u0250", "\u2C6F\u2C6F\u2C6F\u2C6F\u2C6F"}, // grows one byte per char
}
-var lowerTests = []StringTest {
+var lowerTests = []StringTest{
StringTest{"", ""},
StringTest{"abc", "abc"},
StringTest{"AbC123", "abc123"},
const space = "\t\v\r\f\n\u0085\u00a0\u2000\u3000"
-var trimSpaceTests = []StringTest {
+var trimSpaceTests = []StringTest{
StringTest{"", ""},
StringTest{"abc", "abc"},
- StringTest{space + "abc" + space, "abc"},
+ StringTest{space+"abc"+space, "abc"},
StringTest{" ", ""},
StringTest{" \t\r\n \t\t\r\r\n\n ", ""},
StringTest{" \t\r\n x\t\t\r\r\n\n ", "x"},
func tenRunes(rune int) string {
r := make([]int, 10);
for i := range r {
- r[i] = rune
+ r[i] = rune;
}
- return string(r)
+ return string(r);
}
func TestMap(t *testing.T) {
// Run a couple of awful growth/shrinkage tests
a := tenRunes('a');
// 1. Grow. This triggers two reallocations in Map.
- maxRune := func(rune int) int { return unicode.MaxRune };
+ maxRune := func(rune int) int {
+ return unicode.MaxRune;
+ };
m := Map(maxRune, a);
expect := tenRunes(unicode.MaxRune);
if m != expect {
t.Errorf("growing: expected %q got %q", expect, m);
}
// 2. Shrink
- minRune := func(rune int) int { return 'a' };
+ minRune := func(rune int) int {
+ return 'a';
+ };
m = Map(minRune, tenRunes(unicode.MaxRune));
expect = a;
if m != expect {
func equal(m string, s1, s2 string, t *testing.T) bool {
if s1 == s2 {
- return true
+ return true;
}
e1 := Split(s1, "", 0);
e2 := Split(s2, "", 0);
for i, c1 := range e1 {
if i > len(e2) {
- break
+ break;
}
r1, _ := utf8.DecodeRuneInString(c1);
r2, _ := utf8.DecodeRuneInString(e2[i]);
if r1 != r2 {
- t.Errorf("%s diff at %d: U+%04X U+%04X", m, i, r1, r2)
+ t.Errorf("%s diff at %d: U+%04X U+%04X", m, i, r1, r2);
}
}
return false;
func TestCaseConsistency(t *testing.T) {
// Make a string of all the runes.
- a := make([]int, unicode.MaxRune+1);
+ a := make([]int, unicode.MaxRune + 1);
for i := range a {
- a[i] = i
+ a[i] = i;
}
s := string(a);
// convert the cases.
lower := ToLower(s);
// Consistency checks
- if n := utf8.RuneCountInString(upper); n != unicode.MaxRune+1 {
+ if n := utf8.RuneCountInString(upper); n != unicode.MaxRune + 1 {
t.Error("rune count wrong in upper:", n);
}
- if n := utf8.RuneCountInString(lower); n != unicode.MaxRune+1 {
+ if n := utf8.RuneCountInString(lower); n != unicode.MaxRune + 1 {
t.Error("rune count wrong in lower:", n);
}
if !equal("ToUpper(upper)", ToUpper(upper), upper, t) {
if !equal("ToLower(lower)", ToLower(lower), lower, t) {
t.Error("ToLower(lower) consistency fail");
}
- /*
+/*
These fail because of non-one-to-oneness of the data, such as multiple
upper case 'I' mapping to 'i'. We comment them out but keep them for
interest.
package sync
func Semacquire(s *int32) {
- semacquire(s)
+ semacquire(s);
}
func Semrelease(s *int32) {
- semrelease(s)
+ semrelease(s);
}
// Mutexes can be created as part of other structures;
// the zero value for a Mutex is an unlocked mutex.
type Mutex struct {
- key int32;
- sema int32;
+ key int32;
+ sema int32;
}
func xadd(val *int32, delta int32) (new int32) {
return v+delta;
}
}
- panic("unreached")
+ panic("unreached");
}
// Lock locks m.
func (rw *RWMutex) Unlock() {
rw.m.Unlock();
}
-
package sync_test
import (
- . "sync";
- "testing"
+ . "sync";
+ "testing";
)
func HammerSemaphore(s *int32, cdone chan bool) {
<-c;
}
}
-
)
type writeLogger struct {
- prefix string;
- w io.Writer;
+ prefix string;
+ w io.Writer;
}
func (l *writeLogger) Write(p []byte) (n int, err os.Error) {
// that it logs (using log.Stdout) each write to standard output,
// printing the prefix and the hexadecimal data written.
func NewWriteLogger(prefix string, w io.Writer) io.Writer {
- return &writeLogger{prefix, w}
+ return &writeLogger{prefix, w};
}
type readLogger struct {
- prefix string;
- r io.Reader;
+ prefix string;
+ r io.Reader;
}
func (l *readLogger) Read(p []byte) (n int, err os.Error) {
// that it logs (using log.Stdout) each write to standard output,
// printing the prefix and the hexadecimal data written.
func NewReadLogger(prefix string, r io.Reader) io.Reader {
- return &readLogger{prefix, r}
+ return &readLogger{prefix, r};
}
}
func (r *halfReader) Read(p []byte) (int, os.Error) {
- return r.r.Read(p[0:(len(p)+1)/2]);
+ return r.r.Read(p[0 : (len(p)+1)/2]);
}
}
type dataErrReader struct {
- r io.Reader;
- unread []byte;
- data []byte;
+ r io.Reader;
+ unread []byte;
+ data []byte;
}
func (r *dataErrReader) Read(p []byte) (n int, err os.Error) {
break;
}
n = bytes.Copy(p, r.unread);
- r.unread = r.unread[n:len(r.unread)];
+ r.unread = r.unread[n : len(r.unread)];
}
return;
}
-
}
type truncateWriter struct {
- w io.Writer;
- n int64;
+ w io.Writer;
+ n int64;
}
func (t *truncateWriter) Write(p []byte) (n int, err os.Error) {
if t.n <= 0 {
- return len(p), nil
+ return len(p), nil;
}
// real write
n = len(p);
if err == nil {
n = len(p);
}
- return
+ return;
}
s += "\n";
n++;
}
- for i := 0; i < n - 1; i++ { // -1 to avoid final newline
+ for i := 0; i < n-1; i++ { // -1 to avoid final newline
if s[i] == '\n' {
- return s[0:i+1] + "\t" + tabify(s[i+1:n]);
+ return s[0 : i+1]+"\t"+tabify(s[i+1 : n]);
}
}
- return s
+ return s;
}
// T is a type passed to Test functions to manage test state and support formatted test logs.
// Fail marks the Test function as having failed but continues execution.
func (t *T) Fail() {
- t.failed = true
+ t.failed = true;
}
// Failed returns whether the Test function has failed.
func (t *T) Failed() bool {
- return t.failed
+ return t.failed;
}
// FailNow marks the Test function as having failed and stops its execution.
// An internal type but exported because it is cross-package; part of the implementation
// of gotest.
type Test struct {
- Name string;
- F func(*T);
+ Name string;
+ F func(*T);
}
func tRunner(t *T, test *Test) {
// A Ticker holds a synchronous channel that delivers `ticks' of a clock
// at intervals.
type Ticker struct {
- C <-chan int64; // The channel on which the ticks are delivered.
- ns int64;
+ C <-chan int64; // The channel on which the ticks are delivered.
+ ns int64;
shutdown bool;
}
// Stop turns off a ticker. After Stop, no more ticks will be delivered.
func (t *Ticker) Stop() {
- t.shutdown = true
+ t.shutdown = true;
}
func (t *Ticker) ticker(c chan<- int64) {
// if c <- now took too long, skip ahead
if when < now {
// one big step
- when += (now-when)/t.ns * t.ns;
+ when += (now-when) / t.ns * t.ns;
}
for when <= now {
// little steps until when > now
- when += t.ns
+ when += t.ns;
}
- Sleep(when - now);
+ Sleep(when-now);
now = Nanoseconds();
if t.shutdown {
return;
// channel only. Useful for clients that have no need to shut down the ticker.
func Tick(ns int64) <-chan int64 {
if ns <= 0 {
- return nil
+ return nil;
}
return NewTicker(ns).C;
}
// intervals to make up for pauses in delivery of the ticks.
func NewTicker(ns int64) *Ticker {
if ns <= 0 {
- return nil
+ return nil;
}
c := make(chan int64);
t := &Ticker{c, ns, false};
package time_test
import (
- "testing";
- . "time";
+ "testing";
+ . "time";
)
func TestTicker(t *testing.T) {
const (
- Delta = 100*1e6;
- Count = 10;
- );
+ Delta = 100*1e6;
+ Count = 10;
+ )
ticker := NewTicker(Delta);
t0 := Nanoseconds();
for i := 0; i < Count; i++ {
}
ticker.Stop();
t1 := Nanoseconds();
- ns := t1 - t0;
+ ns := t1-t0;
target := int64(Delta*Count);
slop := target*2/10;
- if ns < target - slop || ns > target + slop {
+ if ns < target-slop || ns > target+slop {
t.Fatalf("%d ticks of %g ns took %g ns, expected %g", Count, float64(Delta), float64(ns), float64(target));
}
// Now test that the ticker stopped
if err != nil {
panic("time: os.Time: ", err.String());
}
- return sec
+ return sec;
}
// Nanoseconds reports the number of nanoseconds since the Unix epoch,
if err != nil {
panic("time: os.Time: ", err.String());
}
- return sec*1e9 + nsec
+ return sec*1e9 + nsec;
}
// Days of the week.
const (
- Sunday = iota;
+ Sunday = iota;
Monday;
Tuesday;
Wednesday;
// Time is the struct representing a parsed time value.
type Time struct {
- Year int64; // 2008 is 2008
- Month, Day int; // Sep-17 is 9, 17
- Hour, Minute, Second int; // 10:43:12 is 10, 43, 12
- Weekday int; // Sunday, Monday, ...
- ZoneOffset int; // seconds east of UTC
- Zone string;
+ Year int64; // 2008 is 2008
+ Month, Day int; // Sep-17 is 9, 17
+ Hour, Minute, Second int; // 10:43:12 is 10, 43, 12
+ Weekday int; // Sunday, Monday, ...
+ ZoneOffset int; // seconds east of UTC
+ Zone string;
}
-var nonleapyear = []int{
- 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
-}
-var leapyear = []int{
- 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
-}
+var nonleapyear = []int{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
+var leapyear = []int{31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
func months(year int64) []int {
if year%4 == 0 && (year%100 != 0 || year%400 == 0) {
- return leapyear
+ return leapyear;
}
- return nonleapyear
+ return nonleapyear;
}
const (
- secondsPerDay = 24*60*60;
-
- daysPer400Years = 365*400+97;
- daysPer100Years = 365*100+24;
- daysPer4Years = 365*4+1;
-
- days1970To2001 = 31*365+8;
+ secondsPerDay = 24*60*60;
+ daysPer400Years = 365*400 + 97;
+ daysPer100Years = 365*100 + 24;
+ daysPer4Years = 365*4 + 1;
+ days1970To2001 = 31*365 + 8;
)
// SecondsToUTC converts sec, in number of seconds since the Unix epoch,
t := new(Time);
// Split into time and day.
- day := sec/secondsPerDay;
- sec -= day*secondsPerDay;
+ day := sec / secondsPerDay;
+ sec -= day * secondsPerDay;
if sec < 0 {
day--;
- sec += secondsPerDay
+ sec += secondsPerDay;
}
// Time
t.Second = int(sec%60);
// Day 0 = January 1, 1970 was a Thursday
- t.Weekday = int((day + Thursday) % 7);
+ t.Weekday = int((day+Thursday)%7);
if t.Weekday < 0 {
- t.Weekday += 7
+ t.Weekday += 7;
}
// Change day from 0 = 1970 to 0 = 2001,
year := int64(2001);
if day < 0 {
// Go back enough 400 year cycles to make day positive.
- n := -day/daysPer400Years + 1;
+ n := -day / daysPer400Years + 1;
year -= 400*n;
- day += daysPer400Years*n;
+ day += daysPer400Years * n;
} else {
// Cut off 400 year cycles.
- n := day/daysPer400Years;
+ n := day / daysPer400Years;
year += 400*n;
- day -= daysPer400Years*n;
+ day -= daysPer400Years * n;
}
// Cut off 100-year cycles
- n := day/daysPer100Years;
+ n := day / daysPer100Years;
year += 100*n;
- day -= daysPer100Years*n;
+ day -= daysPer100Years * n;
// Cut off 4-year cycles
- n = day/daysPer4Years;
+ n = day / daysPer4Years;
year += 4*n;
- day -= daysPer4Years*n;
+ day -= daysPer4Years * n;
// Cut off non-leap years.
n = day/365;
var m int;
yday := int(day);
for m = 0; m < 12 && yday >= months[m]; m++ {
- yday -= months[m]
+ yday -= months[m];
}
t.Month = m+1;
t.Day = yday+1;
// UTC returns the current time as a parsed Time value in the UTC time zone.
func UTC() *Time {
- return SecondsToUTC(Seconds())
+ return SecondsToUTC(Seconds());
}
// SecondsToLocalTime converts sec, in number of seconds since the Unix epoch,
t := SecondsToUTC(sec+int64(offset));
t.Zone = z;
t.ZoneOffset = offset;
- return t
+ return t;
}
// LocalTime returns the current time as a parsed Time value in the local time zone.
func LocalTime() *Time {
- return SecondsToLocalTime(Seconds())
+ return SecondsToLocalTime(Seconds());
}
// Seconds returns the number of seconds since January 1, 1970 represented by the
// Rewrite year to be >= 2001.
year := t.Year;
if year < 2001 {
- n := (2001 - year)/400 + 1;
+ n := (2001-year)/400 + 1;
year += 400*n;
- day -= daysPer400Years*n;
+ day -= daysPer400Years * n;
}
// Add in days from 400-year cycles.
- n := (year - 2001) / 400;
+ n := (year-2001)/400;
year -= 400*n;
- day += daysPer400Years*n;
+ day += daysPer400Years * n;
// Add in 100-year cycles.
- n = (year - 2001) / 100;
+ n = (year-2001)/100;
year -= 100*n;
- day += daysPer100Years*n;
+ day += daysPer100Years * n;
// Add in 4-year cycles.
- n = (year - 2001) / 4;
+ n = (year-2001)/4;
year -= 4*n;
- day += daysPer4Years*n;
+ day += daysPer4Years * n;
// Add in non-leap years.
- n = year - 2001;
+ n = year-2001;
day += 365*n;
// Add in days this year.
months := months(t.Year);
- for m := 0; m < t.Month-1; m++ {
- day += int64(months[m])
+ for m := 0; m < t.Month - 1; m++ {
+ day += int64(months[m]);
}
day += int64(t.Day - 1);
// Account for local time zone.
sec -= int64(t.ZoneOffset);
- return sec
+ return sec;
}
var longDayNames = []string{
"Wednesday",
"Thursday",
"Friday",
- "Saturday"
+ "Saturday",
}
var shortDayNames = []string{
"Wed",
"Thu",
"Fri",
- "Sat"
+ "Sat",
}
var shortMonthNames = []string{
"Sep",
"Oct",
"Nov",
- "Dec"
+ "Dec",
}
func copy(dst []byte, s string) {
for i := 0; i < len(s); i++ {
- dst[i] = s[i]
+ dst[i] = s[i];
}
}
func decimal(dst []byte, n int) {
if n < 0 {
- n = 0
+ n = 0;
}
for i := len(dst)-1; i >= 0; i-- {
dst[i] = byte(n%10 + '0');
- n /= 10
+ n /= 10;
}
}
func addString(buf []byte, bp int, s string) int {
n := len(s);
- copy(buf[bp:bp+n], s);
- return bp+n
+ copy(buf[bp : bp+n], s);
+ return bp+n;
}
// Just enough of strftime to implement the date formats below.
case 'b': // %b abbreviated month name
bp = addString(buf, bp, shortMonthNames[t.Month]);
case 'd': // %d day of month (01-31)
- decimal(buf[bp:bp+2], t.Day);
+ decimal(buf[bp : bp+2], t.Day);
bp += 2;
case 'e': // %e day of month ( 1-31)
if t.Day >= 10 {
- decimal(buf[bp:bp+2], t.Day)
+ decimal(buf[bp : bp+2], t.Day);
} else {
buf[bp] = ' ';
- buf[bp+1] = byte(t.Day + '0')
+ buf[bp+1] = byte(t.Day + '0');
}
bp += 2;
case 'H': // %H hour 00-23
- decimal(buf[bp:bp+2], t.Hour);
+ decimal(buf[bp : bp+2], t.Hour);
bp += 2;
case 'M': // %M minute 00-59
- decimal(buf[bp:bp+2], t.Minute);
+ decimal(buf[bp : bp+2], t.Minute);
bp += 2;
case 'S': // %S second 00-59
- decimal(buf[bp:bp+2], t.Second);
+ decimal(buf[bp : bp+2], t.Second);
bp += 2;
case 'Y': // %Y year 2008
- decimal(buf[bp:bp+4], int(t.Year));
+ decimal(buf[bp : bp+4], int(t.Year));
bp += 4;
case 'y': // %y year 08
- decimal(buf[bp:bp+2], int(t.Year%100));
+ decimal(buf[bp : bp+2], int(t.Year % 100));
bp += 2;
case 'Z':
bp = addString(buf, bp, t.Zone);
default:
buf[bp] = '%';
buf[bp+1] = fmt[i];
- bp += 2
+ bp += 2;
}
} else {
buf[bp] = fmt[i];
bp++;
}
}
- return string(buf[0:bp])
+ return string(buf[0:bp]);
}
// Asctime formats the parsed time value in the style of
// ANSI C asctime: Sun Nov 6 08:49:37 1994
func (t *Time) Asctime() string {
- return format(t, "%a %b %e %H:%M:%S %Y")
+ return format(t, "%a %b %e %H:%M:%S %Y");
}
// RFC850 formats the parsed time value in the style of
// RFC 850: Sunday, 06-Nov-94 08:49:37 UTC
func (t *Time) RFC850() string {
- return format(t, "%A, %d-%b-%y %H:%M:%S %Z")
+ return format(t, "%A, %d-%b-%y %H:%M:%S %Z");
}
// RFC1123 formats the parsed time value in the style of
// RFC 1123: Sun, 06 Nov 1994 08:49:37 UTC
func (t *Time) RFC1123() string {
- return format(t, "%a, %d %b %Y %H:%M:%S %Z")
+ return format(t, "%a, %d %b %Y %H:%M:%S %Z");
}
// String formats the parsed time value in the style of
// date(1) - Sun Nov 6 08:49:37 UTC 1994
func (t *Time) String() string {
- return format(t, "%a %b %e %H:%M:%S %Z %Y")
+ return format(t, "%a %b %e %H:%M:%S %Z %Y");
}
import (
"io";
"once";
- "os"
+ "os";
)
const (
- headerSize = 4+16+4*7;
-
- zoneDir = "/usr/share/zoneinfo/";
+ headerSize = 4 + 16 + 4*7;
+ zoneDir = "/usr/share/zoneinfo/";
)
// Simple I/O interface to binary blob of data.
type data struct {
- p []byte;
- error bool;
+ p []byte;
+ error bool;
}
return nil;
}
p := d.p[0:n];
- d.p = d.p[n:len(d.p)];
- return p
+ d.p = d.p[n : len(d.p)];
+ return p;
}
func (d *data) big4() (n uint32, ok bool) {
p := d.read(4);
if len(p) < 4 {
d.error = true;
- return 0, false
+ return 0, false;
}
- return uint32(p[0]) << 24 | uint32(p[1]) << 16 | uint32(p[2]) << 8 | uint32(p[3]), true
+ return uint32(p[0])<<24 | uint32(p[1])<<16 | uint32(p[2])<<8 | uint32(p[3]), true;
}
func (d *data) byte() (n byte, ok bool) {
p := d.read(1);
if len(p) < 1 {
d.error = true;
- return 0, false
+ return 0, false;
}
- return p[0], true
+ return p[0], true;
}
func byteString(p []byte) string {
for i := 0; i < len(p); i++ {
if p[i] == 0 {
- return string(p[0:i])
+ return string(p[0:i]);
}
}
- return string(p)
+ return string(p);
}
// Parsed representation
type zone struct {
- utcoff int;
- isdst bool;
- name string;
+ utcoff int;
+ isdst bool;
+ name string;
}
type zonetime struct {
- time int32; // transition time, in seconds since 1970 GMT
- zone *zone; // the zone that goes into effect at that time
- isstd, isutc bool; // ignored - no idea what these mean
+ time int32; // transition time, in seconds since 1970 GMT
+ zone *zone; // the zone that goes into effect at that time
+ isstd, isutc bool; // ignored - no idea what these mean
}
func parseinfo(bytes []byte) (zt []zonetime, ok bool) {
// 4-byte magic "TZif"
if magic := d.read(4); string(magic) != "TZif" {
- return nil, false
+ return nil, false;
}
// 1-byte version, then 15 bytes of padding
var p []byte;
if p = d.read(16); len(p) != 16 || p[0] != 0 && p[0] != '2' {
- return nil, false
+ return nil, false;
}
// six big-endian 32-bit integers:
// number of local time zones
// number of characters of time zone abbrev strings
const (
- NUTCLocal = iota;
+ NUTCLocal = iota;
NStdWall;
NLeap;
NTime;
NZone;
- NChar
+ NChar;
)
var n [6]int;
for i := 0; i < 6; i++ {
nn, ok := d.big4();
if !ok {
- return nil, false
+ return nil, false;
}
n[i] = int(nn);
}
isutc := d.read(n[NUTCLocal]);
if d.error { // ran out of data
- return nil, false
+ return nil, false;
}
// If version == 2, the entire file repeats, this time using
var ok bool;
var n uint32;
if n, ok = zonedata.big4(); !ok {
- return nil, false
+ return nil, false;
}
z[i].utcoff = int(n);
var b byte;
if b, ok = zonedata.byte(); !ok {
- return nil, false
+ return nil, false;
}
z[i].isdst = b != 0;
if b, ok = zonedata.byte(); !ok || int(b) >= len(abbrev) {
- return nil, false
+ return nil, false;
}
- z[i].name = byteString(abbrev[b:len(abbrev)])
+ z[i].name = byteString(abbrev[b:len(abbrev)]);
}
// Now the transition time info.
var ok bool;
var n uint32;
if n, ok = txtimes.big4(); !ok {
- return nil, false
+ return nil, false;
}
zt[i].time = int32(n);
if int(txzones[i]) >= len(z) {
- return nil, false
+ return nil, false;
}
zt[i].zone = &z[txzones[i]];
if i < len(isstd) {
- zt[i].isstd = isstd[i] != 0
+ zt[i].isstd = isstd[i] != 0;
}
if i < len(isutc) {
- zt[i].isutc = isutc[i] != 0
+ zt[i].isutc = isutc[i] != 0;
}
}
- return zt, true
+ return zt, true;
}
func readinfofile(name string) ([]zonetime, bool) {
buf, err := io.ReadFile(name);
if err != nil {
- return nil, false
+ return nil, false;
}
return parseinfo(buf);
}
case err == os.ENOENV:
zones, _ = readinfofile("/etc/localtime");
case len(tz) > 0:
- zones, _ = readinfofile(zoneDir + tz);
+ zones, _ = readinfofile(zoneDir+tz);
case len(tz) == 0:
- // do nothing: use UTC
+ // do nothing: use UTC
}
}
func lookupTimezone(sec int64) (zone string, offset int) {
once.Do(setupZone);
if len(zones) == 0 {
- return "UTC", 0
+ return "UTC", 0;
}
// Binary search for entry with largest time <= sec
for len(tz) > 1 {
m := len(tz)/2;
if sec < int64(tz[m].time) {
- tz = tz[0:m]
+ tz = tz[0:m];
} else {
- tz = tz[m:len(tz)]
+ tz = tz[m:len(tz)];
}
}
z := tz[0].zone;
- return z.name, z.utcoff
+ return z.name, z.utcoff;
}
// IsDigit reports whether the rune is a decimal digit.
func IsDigit(rune int) bool {
if rune < 0x100 { // quick ASCII (Latin-1, really) check
- return '0' <= rune && rune <= '9'
+ return '0' <= rune && rune <= '9';
}
return Is(Digit, rune);
}
package unicode_test
import (
- "testing";
- . "unicode";
+ "testing";
+ . "unicode";
)
-var testDigit = []int {
+var testDigit = []int{
0x0030,
0x0039,
0x0661,
0x1D7CE,
}
-var testLetter = []int {
+var testLetter = []int{
0x0041,
0x0061,
0x00AA,
func TestDigitOptimization(t *testing.T) {
for i := 0; i < 0x100; i++ {
if Is(Digit, i) != IsDigit(i) {
- t.Errorf("IsDigit(U+%04X) disagrees with Is(Digit)", i)
+ t.Errorf("IsDigit(U+%04X) disagrees with Is(Digit)", i);
}
}
}
package unicode
const (
- MaxRune = 0x10FFFF; // Maximum valid Unicode code point.
+ MaxRune = 0x10FFFF; // Maximum valid Unicode code point.
ReplacementChar = 0xFFFD; // Represents invalid code points.
)
// Indices into the Delta arrays inside CaseRanges for case mapping.
const (
- UpperCase = iota;
+ UpperCase = iota;
LowerCase;
TitleCase;
MaxCase;
)
+
type d [MaxCase]int32 // to make the CaseRanges text shorter
// If the Delta field of a CaseRange is UpperLower or LowerUpper, it means
// this CaseRange represents a sequence of the form (say)
// Upper Lower Upper Lower.
const (
- UpperLower = MaxRune + 1; // (Cannot be a valid delta.)
+ UpperLower = MaxRune+1; // (Cannot be a valid delta.)
)
// Is tests whether rune is in the specified table of ranges.
lo := 0;
hi := len(ranges);
for lo < hi {
- m := lo + (hi - lo)/2;
+ m := lo + (hi-lo)/2;
r := ranges[m];
if r.Lo <= rune && rune <= r.Hi {
return (rune - r.Lo) % r.Stride == 0;
// To maps the rune to the specified case: UpperCase, LowerCase, or TitleCase
func To(_case int, rune int) int {
if _case < 0 || MaxCase <= _case {
- return ReplacementChar // as reasonable an error as any
+ return ReplacementChar; // as reasonable an error as any
}
// binary search over ranges
lo := 0;
hi := len(CaseRanges);
for lo < hi {
- m := lo + (hi - lo)/2;
+ m := lo + (hi-lo)/2;
r := CaseRanges[m];
if r.Lo <= rune && rune <= r.Hi {
delta := int(r.Delta[_case]);
// is odd so we take the low bit from _case.
return r.Lo + ((rune - r.Lo)&^1 | _case&1);
}
- return rune + delta;
+ return rune+delta;
}
if rune < r.Lo {
hi = m;
func ToUpper(rune int) int {
if rune < 0x80 { // quick ASCII check
if 'a' <= rune && rune <= 'z' {
- rune -= 'a'-'A'
+ rune -= 'a'-'A';
}
- return rune
+ return rune;
}
return To(UpperCase, rune);
}
func ToLower(rune int) int {
if rune < 0x80 { // quick ASCII check
if 'A' <= rune && rune <= 'Z' {
- rune += 'a'-'A'
+ rune += 'a'-'A';
}
- return rune
+ return rune;
}
return To(LowerCase, rune);
}
func ToTitle(rune int) int {
if rune < 0x80 { // quick ASCII check
if 'a' <= rune && rune <= 'z' { // title case is upper case for ASCII
- rune -= 'a'-'A'
+ rune -= 'a'-'A';
}
- return rune
+ return rune;
}
return To(TitleCase, rune);
}
package unicode_test
import (
- "testing";
- . "unicode";
+ "testing";
+ . "unicode";
)
var upperTest = []int{
}
type caseT struct {
- cas, in, out int
+ cas, in, out int;
}
-var caseTest = []caseT {
+var caseTest = []caseT{
// errors
- caseT{-1, '\n', 0xFFFD},
- caseT{UpperCase, -1, -1},
- caseT{UpperCase, 1<<30, 1<<30},
+ caseT{-1, '\n', 0xFFFD},
+ caseT{UpperCase, -1, -1},
+ caseT{UpperCase, 1<<30, 1<<30},
// ASCII (special-cased so test carefully)
- caseT{UpperCase, '\n', '\n'},
- caseT{UpperCase, 'a', 'A'},
- caseT{UpperCase, 'A', 'A'},
- caseT{UpperCase, '7', '7'},
- caseT{LowerCase, '\n', '\n'},
- caseT{LowerCase, 'a', 'a'},
- caseT{LowerCase, 'A', 'a'},
- caseT{LowerCase, '7', '7'},
- caseT{TitleCase, '\n', '\n'},
- caseT{TitleCase, 'a', 'A'},
- caseT{TitleCase, 'A', 'A'},
- caseT{TitleCase, '7', '7'},
+ caseT{UpperCase, '\n', '\n'},
+ caseT{UpperCase, 'a', 'A'},
+ caseT{UpperCase, 'A', 'A'},
+ caseT{UpperCase, '7', '7'},
+ caseT{LowerCase, '\n', '\n'},
+ caseT{LowerCase, 'a', 'a'},
+ caseT{LowerCase, 'A', 'a'},
+ caseT{LowerCase, '7', '7'},
+ caseT{TitleCase, '\n', '\n'},
+ caseT{TitleCase, 'a', 'A'},
+ caseT{TitleCase, 'A', 'A'},
+ caseT{TitleCase, '7', '7'},
// Latin-1: easy to read the tests!
- caseT{UpperCase, 0x80, 0x80},
- caseT{UpperCase, 'Å', 'Å'},
- caseT{UpperCase, 'å', 'Å'},
- caseT{LowerCase, 0x80, 0x80},
- caseT{LowerCase, 'Å', 'å'},
- caseT{LowerCase, 'å', 'å'},
- caseT{TitleCase, 0x80, 0x80},
- caseT{TitleCase, 'Å', 'Å'},
- caseT{TitleCase, 'å', 'Å'},
+ caseT{UpperCase, 0x80, 0x80},
+ caseT{UpperCase, 'Å', 'Å'},
+ caseT{UpperCase, 'å', 'Å'},
+ caseT{LowerCase, 0x80, 0x80},
+ caseT{LowerCase, 'Å', 'å'},
+ caseT{LowerCase, 'å', 'å'},
+ caseT{TitleCase, 0x80, 0x80},
+ caseT{TitleCase, 'Å', 'Å'},
+ caseT{TitleCase, 'å', 'Å'},
// 0131;LATIN SMALL LETTER DOTLESS I;Ll;0;L;;;;;N;;;0049;;0049
- caseT{UpperCase, 0x0131, 'I'},
- caseT{LowerCase, 0x0131, 0x0131},
- caseT{TitleCase, 0x0131, 'I'},
+ caseT{UpperCase, 0x0131, 'I'},
+ caseT{LowerCase, 0x0131, 0x0131},
+ caseT{TitleCase, 0x0131, 'I'},
// 0133;LATIN SMALL LIGATURE IJ;Ll;0;L;<compat> 0069 006A;;;;N;LATIN SMALL LETTER I J;;0132;;0132
- caseT{UpperCase, 0x0133, 0x0132},
- caseT{LowerCase, 0x0133, 0x0133},
- caseT{TitleCase, 0x0133, 0x0132},
+ caseT{UpperCase, 0x0133, 0x0132},
+ caseT{LowerCase, 0x0133, 0x0133},
+ caseT{TitleCase, 0x0133, 0x0132},
// 212A;KELVIN SIGN;Lu;0;L;004B;;;;N;DEGREES KELVIN;;;006B;
- caseT{UpperCase, 0x212A, 0x212A},
- caseT{LowerCase, 0x212A, 'k'},
- caseT{TitleCase, 0x212A, 0x212A},
+ caseT{UpperCase, 0x212A, 0x212A},
+ caseT{LowerCase, 0x212A, 'k'},
+ caseT{TitleCase, 0x212A, 0x212A},
// From an UpperLower sequence
// A640;CYRILLIC CAPITAL LETTER ZEMLYA;Lu;0;L;;;;;N;;;;A641;
- caseT{UpperCase, 0xA640, 0xA640},
- caseT{LowerCase, 0xA640, 0xA641},
- caseT{TitleCase, 0xA640, 0xA640},
+ caseT{UpperCase, 0xA640, 0xA640},
+ caseT{LowerCase, 0xA640, 0xA641},
+ caseT{TitleCase, 0xA640, 0xA640},
// A641;CYRILLIC SMALL LETTER ZEMLYA;Ll;0;L;;;;;N;;;A640;;A640
- caseT{UpperCase, 0xA641, 0xA640},
- caseT{LowerCase, 0xA641, 0xA641},
- caseT{TitleCase, 0xA641, 0xA640},
+ caseT{UpperCase, 0xA641, 0xA640},
+ caseT{LowerCase, 0xA641, 0xA641},
+ caseT{TitleCase, 0xA641, 0xA640},
// A64E;CYRILLIC CAPITAL LETTER NEUTRAL YER;Lu;0;L;;;;;N;;;;A64F;
- caseT{UpperCase, 0xA64E, 0xA64E},
- caseT{LowerCase, 0xA64E, 0xA64F},
- caseT{TitleCase, 0xA64E, 0xA64E},
+ caseT{UpperCase, 0xA64E, 0xA64E},
+ caseT{LowerCase, 0xA64E, 0xA64F},
+ caseT{TitleCase, 0xA64E, 0xA64E},
// A65F;CYRILLIC SMALL LETTER YN;Ll;0;L;;;;;N;;;A65E;;A65E
- caseT{UpperCase, 0xA65F, 0xA65E},
- caseT{LowerCase, 0xA65F, 0xA65F},
- caseT{TitleCase, 0xA65F, 0xA65E},
+ caseT{UpperCase, 0xA65F, 0xA65E},
+ caseT{LowerCase, 0xA65F, 0xA65F},
+ caseT{TitleCase, 0xA65F, 0xA65E},
// From another UpperLower sequence
// 0139;LATIN CAPITAL LETTER L WITH ACUTE;Lu;0;L;004C 0301;;;;N;LATIN CAPITAL LETTER L ACUTE;;;013A;
- caseT{UpperCase, 0x0139, 0x0139},
- caseT{LowerCase, 0x0139, 0x013A},
- caseT{TitleCase, 0x0139, 0x0139},
+ caseT{UpperCase, 0x0139, 0x0139},
+ caseT{LowerCase, 0x0139, 0x013A},
+ caseT{TitleCase, 0x0139, 0x0139},
// 013F;LATIN CAPITAL LETTER L WITH MIDDLE DOT;Lu;0;L;<compat> 004C 00B7;;;;N;;;;0140;
- caseT{UpperCase, 0x013f, 0x013f},
- caseT{LowerCase, 0x013f, 0x0140},
- caseT{TitleCase, 0x013f, 0x013f},
+ caseT{UpperCase, 0x013f, 0x013f},
+ caseT{LowerCase, 0x013f, 0x0140},
+ caseT{TitleCase, 0x013f, 0x013f},
// 0148;LATIN SMALL LETTER N WITH CARON;Ll;0;L;006E 030C;;;;N;LATIN SMALL LETTER N HACEK;;0147;;0147
- caseT{UpperCase, 0x0148, 0x0147},
- caseT{LowerCase, 0x0148, 0x0148},
- caseT{TitleCase, 0x0148, 0x0147},
+ caseT{UpperCase, 0x0148, 0x0147},
+ caseT{LowerCase, 0x0148, 0x0148},
+ caseT{TitleCase, 0x0148, 0x0147},
// Last block in the 5.1.0 table
// 10400;DESERET CAPITAL LETTER LONG I;Lu;0;L;;;;;N;;;;10428;
- caseT{UpperCase, 0x10400, 0x10400},
- caseT{LowerCase, 0x10400, 0x10428},
- caseT{TitleCase, 0x10400, 0x10400},
+ caseT{UpperCase, 0x10400, 0x10400},
+ caseT{LowerCase, 0x10400, 0x10428},
+ caseT{TitleCase, 0x10400, 0x10400},
// 10427;DESERET CAPITAL LETTER EW;Lu;0;L;;;;;N;;;;1044F;
- caseT{UpperCase, 0x10427, 0x10427},
- caseT{LowerCase, 0x10427, 0x1044F},
- caseT{TitleCase, 0x10427, 0x10427},
+ caseT{UpperCase, 0x10427, 0x10427},
+ caseT{LowerCase, 0x10427, 0x1044F},
+ caseT{TitleCase, 0x10427, 0x10427},
// 10428;DESERET SMALL LETTER LONG I;Ll;0;L;;;;;N;;;10400;;10400
- caseT{UpperCase, 0x10428, 0x10400},
- caseT{LowerCase, 0x10428, 0x10428},
- caseT{TitleCase, 0x10428, 0x10400},
+ caseT{UpperCase, 0x10428, 0x10400},
+ caseT{LowerCase, 0x10428, 0x10428},
+ caseT{TitleCase, 0x10428, 0x10400},
// 1044F;DESERET SMALL LETTER EW;Ll;0;L;;;;;N;;;10427;;10427
- caseT{UpperCase, 0x1044F, 0x10427},
- caseT{LowerCase, 0x1044F, 0x1044F},
- caseT{TitleCase, 0x1044F, 0x10427},
+ caseT{UpperCase, 0x1044F, 0x10427},
+ caseT{LowerCase, 0x1044F, 0x1044F},
+ caseT{TitleCase, 0x1044F, 0x10427},
// First one not in the 5.1.0 table
// 10450;SHAVIAN LETTER PEEP;Lo;0;L;;;;;N;;;;;
- caseT{UpperCase, 0x10450, 0x10450},
- caseT{LowerCase, 0x10450, 0x10450},
- caseT{TitleCase, 0x10450, 0x10450},
+ caseT{UpperCase, 0x10450, 0x10450},
+ caseT{LowerCase, 0x10450, 0x10450},
+ caseT{TitleCase, 0x10450, 0x10450},
}
func TestIsLetter(t *testing.T) {
func caseString(c int) string {
switch c {
case UpperCase:
- return "UpperCase"
+ return "UpperCase";
case LowerCase:
- return "LowerCase"
+ return "LowerCase";
case TitleCase:
- return "TitleCase"
+ return "TitleCase";
}
- return "ErrorCase"
+ return "ErrorCase";
}
func TestTo(t *testing.T) {
func TestToUpperCase(t *testing.T) {
for _, c := range caseTest {
if c.cas != UpperCase {
- continue
+ continue;
}
r := ToUpper(c.in);
if c.out != r {
func TestToLowerCase(t *testing.T) {
for _, c := range caseTest {
if c.cas != LowerCase {
- continue
+ continue;
}
r := ToLower(c.in);
if c.out != r {
func TestToTitleCase(t *testing.T) {
for _, c := range caseTest {
if c.cas != TitleCase {
- continue
+ continue;
}
r := ToTitle(c.in);
if c.out != r {
func TestLetterOptimizations(t *testing.T) {
for i := 0; i < 0x100; i++ {
if Is(Letter, i) != IsLetter(i) {
- t.Errorf("IsLetter(U+%04X) disagrees with Is(Letter)", i)
+ t.Errorf("IsLetter(U+%04X) disagrees with Is(Letter)", i);
}
if Is(Upper, i) != IsUpper(i) {
- t.Errorf("IsUpper(U+%04X) disagrees with Is(Upper)", i)
+ t.Errorf("IsUpper(U+%04X) disagrees with Is(Upper)", i);
}
if Is(Lower, i) != IsLower(i) {
- t.Errorf("IsLower(U+%04X) disagrees with Is(Lower)", i)
+ t.Errorf("IsLower(U+%04X) disagrees with Is(Lower)", i);
}
if Is(Title, i) != IsTitle(i) {
- t.Errorf("IsTitle(U+%04X) disagrees with Is(Title)", i)
+ t.Errorf("IsTitle(U+%04X) disagrees with Is(Title)", i);
}
if Is(White_Space, i) != IsSpace(i) {
- t.Errorf("IsSpace(U+%04X) disagrees with Is(White_Space)", i)
+ t.Errorf("IsSpace(U+%04X) disagrees with Is(White_Space)", i);
}
if To(UpperCase, i) != ToUpper(i) {
- t.Errorf("ToUpper(U+%04X) disagrees with To(Upper)", i)
+ t.Errorf("ToUpper(U+%04X) disagrees with To(Upper)", i);
}
if To(LowerCase, i) != ToLower(i) {
- t.Errorf("ToLower(U+%04X) disagrees with To(Lower)", i)
+ t.Errorf("ToLower(U+%04X) disagrees with To(Lower)", i);
}
if To(TitleCase, i) != ToTitle(i) {
- t.Errorf("ToTitle(U+%04X) disagrees with To(Title)", i)
+ t.Errorf("ToTitle(U+%04X) disagrees with To(Title)", i);
}
}
}
// 3) A Pointer can be converted to a uintptr.
// Pointer therefore allows a program to defeat the type system and read and write
// arbitrary memory. It should be used with extreme care.
-type Pointer *ArbitraryType
+type Pointer *ArbitraryType
// Sizeof returns the size in bytes occupied by the value v. The size is that of the
// "top level" of the value only. For instance, if v is a slice, it returns the size of
// the slice descriptor, not the size of the memory referenced by the slice.
-func Sizeof(v ArbitraryType) int
+func Sizeof(v ArbitraryType) int
// Offsetof returns the offset within the struct of the field represented by v,
// which must be of the form struct_value.field. In other words, it returns the
// number of bytes between the start of the struct and the start of the field.
-func Offsetof(v ArbitraryType) int
+func Offsetof(v ArbitraryType) int
// Alignof returns the alignment of the value v. It is the minimum value m such
// that the address of a variable with the type of v will always always be zero mod m.
// If v is of the form obj.f, it returns the alignment of field f within struct object obj.
-func Alignof(v ArbitraryType) int
+func Alignof(v ArbitraryType) int
// Typeof returns the type of an interface value, a runtime.Type.
-func Typeof(i interface {}) (typ interface {})
+func Typeof(i interface{}) (typ interface{})
// Reflect unpacks an interface value into its type and the address of a copy of the
// internal value.
-func Reflect(i interface {}) (typ interface {}, addr uintptr)
+func Reflect(i interface{}) (typ interface{}, addr uintptr)
// Unreflect inverts Reflect: Given a type and a pointer, it returns an empty interface value
// with those contents.
-func Unreflect(typ interface {}, addr uintptr) (ret interface {})
+func Unreflect(typ interface{}, addr uintptr) (ret interface{})
package utf8_test
import (
- "bytes";
- "strings";
- "testing";
- . "utf8";
+ "bytes";
+ "strings";
+ "testing";
+ . "utf8";
)
type Utf8Map struct {
- rune int;
- str string;
+ rune int;
+ str string;
}
-var utf8map = []Utf8Map {
- Utf8Map{ 0x0000, "\x00" },
- Utf8Map{ 0x0001, "\x01" },
- Utf8Map{ 0x007e, "\x7e" },
- Utf8Map{ 0x007f, "\x7f" },
- Utf8Map{ 0x0080, "\xc2\x80" },
- Utf8Map{ 0x0081, "\xc2\x81" },
- Utf8Map{ 0x00bf, "\xc2\xbf" },
- Utf8Map{ 0x00c0, "\xc3\x80" },
- Utf8Map{ 0x00c1, "\xc3\x81" },
- Utf8Map{ 0x00c8, "\xc3\x88" },
- Utf8Map{ 0x00d0, "\xc3\x90" },
- Utf8Map{ 0x00e0, "\xc3\xa0" },
- Utf8Map{ 0x00f0, "\xc3\xb0" },
- Utf8Map{ 0x00f8, "\xc3\xb8" },
- Utf8Map{ 0x00ff, "\xc3\xbf" },
- Utf8Map{ 0x0100, "\xc4\x80" },
- Utf8Map{ 0x07ff, "\xdf\xbf" },
- Utf8Map{ 0x0800, "\xe0\xa0\x80" },
- Utf8Map{ 0x0801, "\xe0\xa0\x81" },
- Utf8Map{ 0xfffe, "\xef\xbf\xbe" },
- Utf8Map{ 0xffff, "\xef\xbf\xbf" },
- Utf8Map{ 0x10000, "\xf0\x90\x80\x80" },
- Utf8Map{ 0x10001, "\xf0\x90\x80\x81" },
- Utf8Map{ 0x10fffe, "\xf4\x8f\xbf\xbe" },
- Utf8Map{ 0x10ffff, "\xf4\x8f\xbf\xbf" },
+var utf8map = []Utf8Map{
+ Utf8Map{0x0000, "\x00"},
+ Utf8Map{0x0001, "\x01"},
+ Utf8Map{0x007e, "\x7e"},
+ Utf8Map{0x007f, "\x7f"},
+ Utf8Map{0x0080, "\xc2\x80"},
+ Utf8Map{0x0081, "\xc2\x81"},
+ Utf8Map{0x00bf, "\xc2\xbf"},
+ Utf8Map{0x00c0, "\xc3\x80"},
+ Utf8Map{0x00c1, "\xc3\x81"},
+ Utf8Map{0x00c8, "\xc3\x88"},
+ Utf8Map{0x00d0, "\xc3\x90"},
+ Utf8Map{0x00e0, "\xc3\xa0"},
+ Utf8Map{0x00f0, "\xc3\xb0"},
+ Utf8Map{0x00f8, "\xc3\xb8"},
+ Utf8Map{0x00ff, "\xc3\xbf"},
+ Utf8Map{0x0100, "\xc4\x80"},
+ Utf8Map{0x07ff, "\xdf\xbf"},
+ Utf8Map{0x0800, "\xe0\xa0\x80"},
+ Utf8Map{0x0801, "\xe0\xa0\x81"},
+ Utf8Map{0xfffe, "\xef\xbf\xbe"},
+ Utf8Map{0xffff, "\xef\xbf\xbf"},
+ Utf8Map{0x10000, "\xf0\x90\x80\x80"},
+ Utf8Map{0x10001, "\xf0\x90\x80\x81"},
+ Utf8Map{0x10fffe, "\xf4\x8f\xbf\xbe"},
+ Utf8Map{0x10ffff, "\xf4\x8f\xbf\xbf"},
}
// strings.Bytes with one extra byte at end
func makeBytes(s string) []byte {
s += "\x00";
b := strings.Bytes(s);
- return b[0:len(s)-1];
+ return b[0 : len(s)-1];
}
func TestFullRune(t *testing.T) {
if !FullRuneInString(s) {
t.Errorf("FullRuneInString(%q) (rune %04x) = false, want true", s, m.rune);
}
- b1 := b[0:len(b)-1];
+ b1 := b[0 : len(b)-1];
if FullRune(b1) {
t.Errorf("FullRune(%q) = true, want false", b1);
}
if rune != m.rune || size != len(b) {
t.Errorf("DecodeRune(%q) = 0x%04x, %d want 0x%04x, %d", b, rune, size, m.rune, len(b));
}
- s = m.str+"\x00";
+ s = m.str + "\x00";
rune, size = DecodeRuneInString(s);
if rune != m.rune || size != len(b) {
t.Errorf("DecodeRuneInString(%q) = 0x%04x, %d want 0x%04x, %d", s, rune, size, m.rune, len(b));
if wantsize >= len(b) {
wantsize = 0;
}
- rune, size = DecodeRune(b[0:len(b)-1]);
+ rune, size = DecodeRune(b[0 : len(b)-1]);
if rune != RuneError || size != wantsize {
- t.Errorf("DecodeRune(%q) = 0x%04x, %d want 0x%04x, %d", b[0:len(b)-1], rune, size, RuneError, wantsize);
+ t.Errorf("DecodeRune(%q) = 0x%04x, %d want 0x%04x, %d", b[0 : len(b)-1], rune, size, RuneError, wantsize);
}
- s = m.str[0:len(m.str)-1];
+ s = m.str[0 : len(m.str) - 1];
rune, size = DecodeRuneInString(s);
if rune != RuneError || size != wantsize {
t.Errorf("DecodeRuneInString(%q) = 0x%04x, %d want 0x%04x, %d", s, rune, size, RuneError, wantsize);
}
type RuneCountTest struct {
- in string;
- out int;
+ in string;
+ out int;
}
-var runecounttests = []RuneCountTest {
- RuneCountTest{ "abcd", 4 },
- RuneCountTest{ "☺☻☹", 3 },
- RuneCountTest{ "1,2,3,4", 7 },
- RuneCountTest{ "\xe2\x00", 2 },
+
+var runecounttests = []RuneCountTest{
+ RuneCountTest{"abcd", 4},
+ RuneCountTest{"☺☻☹", 3},
+ RuneCountTest{"1,2,3,4", 7},
+ RuneCountTest{"\xe2\x00", 2},
}
+
func TestRuneCount(t *testing.T) {
for i := 0; i < len(runecounttests); i++ {
tt := runecounttests[i];